8
|
1 // Filename : net.cpp
|
10
|
2 // Last Change: 2018-11-08 木 10:08:45.
|
8
|
3 //
|
|
4
|
|
5 #include <wx/datetime.h>
|
|
6 #include <wx/stream.h>
|
|
7 #include <wx/zstream.h>
|
|
8 #include <wx/tarstrm.h>
|
|
9 #include <wx/mstream.h>
|
|
10 #include "net.h"
|
|
11
|
|
12 RsHttp::RsHttp()
|
|
13 {
|
|
14 }
|
|
15
|
|
16 RsHttp::~RsHttp()
|
|
17 {
|
|
18 }
|
|
19
|
9
|
20 bool RsHttp::Get( wxString url, wxString file )
|
8
|
21 {
|
9
|
22 bool ret = false;
|
8
|
23 wxHTTP get;
|
9
|
24 get.SetFlags( wxSOCKET_WAITALL|wxSOCKET_BLOCK );
|
8
|
25 while ( !get.Connect( m_server, m_port ) )
|
|
26 wxSleep( 1 );
|
|
27
|
|
28 wxInputStream *httpStream = get.GetInputStream( url );
|
|
29 if ( get.GetError() == wxPROTO_NOERR ) {
|
|
30 wxFileOutputStream out_stream( file );
|
|
31 httpStream->Read( out_stream );
|
9
|
32 ret = true;
|
8
|
33 } else {
|
9
|
34 wxMessageBox( wxT( "Re:Searcher Error: cannot get data." ) );
|
8
|
35 }
|
|
36
|
|
37 wxDELETE( httpStream );
|
|
38 get.Close();
|
9
|
39 return ret;
|
8
|
40 }
|
|
41
|
9
|
42 bool RsHttp::GetDB( void )
|
8
|
43 {
|
9
|
44 bool ret = false;
|
|
45 ret = Get( wxT( "/db/auth.db"), wxT( "auth.db") );
|
|
46 ret = Get( wxT( "/db/hhs.db"), wxT( "hhs.db") );
|
8
|
47 wxDateTime now = wxDateTime::Now();
|
|
48 if ( now.GetDay() % 14 == 0 )
|
|
49 Get( wxT( "/db/index.db"), wxT( "index.db" ) );
|
9
|
50 return ret;
|
8
|
51 }
|
|
52
|
|
53 int RsHttp::GetImagesSize( wxString hhs, wxString date )
|
|
54 {
|
|
55 wxHTTP get;
|
|
56 while ( !get.Connect( m_server, m_port ) )
|
|
57 wxSleep( 1 );
|
|
58
|
|
59 wxString url = wxT( "/images/" ) + date + wxT( "/" ) + hhs + wxT( ".tgz" );
|
|
60
|
|
61 int size = -1;
|
|
62 wxInputStream *http_istream = get.GetInputStream( url );
|
|
63 if ( get.GetError() == wxPROTO_NOERR ) {
|
|
64 size = http_istream->GetSize();
|
|
65 } else {
|
|
66 wxMessageBox( wxT( "Re:Searcher Error: Cannot get file size." ) );
|
|
67 }
|
|
68
|
|
69 wxDELETE( http_istream );
|
|
70 get.Close();
|
|
71 return size;
|
|
72 }
|
|
73
|
|
74 void RsHttp::GetImages( wxString hhs, wxString date )
|
|
75 {
|
|
76 wxHTTP get;
|
|
77 get.SetTimeout( 30 );
|
|
78 get.SetFlags( wxSOCKET_WAITALL|wxSOCKET_BLOCK );
|
|
79 while ( !get.Connect( m_server, m_port ) )
|
|
80 wxSleep( 1 );
|
|
81
|
|
82 wxString url = wxT( "/images/" ) + date + wxT( "/" ) + hhs + wxT( ".tgz" );
|
|
83
|
|
84 wxInputStream *http_istream = get.GetInputStream( url );
|
|
85 if ( get.GetError() == wxPROTO_NOERR ) {
|
|
86 //int size = http_istream->GetSize();
|
|
87 wxZlibInputStream zlib_istream( http_istream ); // 0: no cache : bad
|
|
88
|
|
89 wxTarEntry* entry;
|
|
90 wxTarInputStream tar_istream( zlib_istream );
|
|
91 int i = 1;
|
|
92 while ( ( entry = tar_istream.GetNextEntry() ) != NULL ) {
|
|
93 //wxString name = entry->GetName();
|
|
94 wxFileOutputStream file_ostream( wxString::Format( wxT( ".cache/%s_%d" ), date, i++ ) );
|
|
95 file_ostream.Write( tar_istream );
|
|
96 file_ostream.Close();
|
|
97 }
|
|
98 } else {
|
|
99 wxMessageBox( wxT( "Re:Searcher Error: get err" ) );
|
|
100 }
|
|
101
|
|
102 //wxDELETE( http_istream );
|
|
103 get.Close();
|
|
104 }
|
|
105
|
10
|
106 void RsHttp::GetImages2Memory( wxString hhs, wxString date )
|
|
107 {
|
|
108 wxHTTP get;
|
|
109 get.SetTimeout( 30 );
|
|
110 get.SetFlags( wxSOCKET_WAITALL|wxSOCKET_BLOCK );
|
|
111 while ( !get.Connect( m_server, m_port ) )
|
|
112 wxSleep( 1 );
|
|
113
|
|
114 wxString url = wxT( "/images/" ) + date + wxT( "/" ) + hhs + wxT( ".tgz" );
|
|
115
|
|
116 wxInputStream *http_istream = get.GetInputStream( url );
|
|
117 if ( get.GetError() == wxPROTO_NOERR ) {
|
|
118 //int size = http_istream->GetSize();
|
|
119 wxZlibInputStream zlib_istream( http_istream ); // 0: no cache : bad
|
|
120
|
|
121 wxTarEntry* entry;
|
|
122 wxTarInputStream tar_istream( zlib_istream );
|
|
123 int i = 1;
|
|
124 while ( ( entry = tar_istream.GetNextEntry() ) != NULL ) {
|
|
125 //wxString name = entry->GetName();
|
|
126 wxFileOutputStream file_ostream( wxString::Format( wxT( ".cache/%s_%d" ), date, i++ ) );
|
|
127 file_ostream.Write( tar_istream );
|
|
128 file_ostream.Close();
|
|
129 }
|
|
130 } else {
|
|
131 wxMessageBox( wxT( "Re:Searcher Error: get err" ) );
|
|
132 }
|
|
133
|
|
134 //wxDELETE( http_istream );
|
|
135 get.Close();
|
|
136 }
|
|
137
|