8
|
1 // Filename : net.cpp
|
|
2 // Last Change: 2018-10-30 火 11:08:25.
|
|
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
|
|
20 void RsHttp::Get( wxString url, wxString file )
|
|
21 {
|
|
22 wxHTTP get;
|
|
23 while ( !get.Connect( m_server, m_port ) )
|
|
24 wxSleep( 1 );
|
|
25
|
|
26 wxInputStream *httpStream = get.GetInputStream( url );
|
|
27 if ( get.GetError() == wxPROTO_NOERR ) {
|
|
28 wxFileOutputStream out_stream( file );
|
|
29 httpStream->Read( out_stream );
|
|
30 } else {
|
|
31 wxMessageBox( wxT( "Re:Searcher Error: get err" ) );
|
|
32 }
|
|
33
|
|
34 wxDELETE( httpStream );
|
|
35 get.Close();
|
|
36 }
|
|
37
|
|
38 void RsHttp::GetDB( void )
|
|
39 {
|
|
40 Get( wxT( "/db/auth.db"), wxT( "auth.db") );
|
|
41 Get( wxT( "/db/hhs.db"), wxT( "hhs.db") );
|
|
42 wxDateTime now = wxDateTime::Now();
|
|
43 if ( now.GetDay() % 14 == 0 )
|
|
44 Get( wxT( "/db/index.db"), wxT( "index.db" ) );
|
|
45 }
|
|
46
|
|
47 int RsHttp::GetImagesSize( wxString hhs, wxString date )
|
|
48 {
|
|
49 wxHTTP get;
|
|
50 while ( !get.Connect( m_server, m_port ) )
|
|
51 wxSleep( 1 );
|
|
52
|
|
53 wxString url = wxT( "/images/" ) + date + wxT( "/" ) + hhs + wxT( ".tgz" );
|
|
54
|
|
55 int size = -1;
|
|
56 wxInputStream *http_istream = get.GetInputStream( url );
|
|
57 if ( get.GetError() == wxPROTO_NOERR ) {
|
|
58 size = http_istream->GetSize();
|
|
59 } else {
|
|
60 wxMessageBox( wxT( "Re:Searcher Error: Cannot get file size." ) );
|
|
61 }
|
|
62
|
|
63 wxDELETE( http_istream );
|
|
64 get.Close();
|
|
65 return size;
|
|
66 }
|
|
67
|
|
68 void RsHttp::GetImages( wxString hhs, wxString date )
|
|
69 {
|
|
70 wxHTTP get;
|
|
71 get.SetTimeout( 30 );
|
|
72 get.SetFlags( wxSOCKET_WAITALL|wxSOCKET_BLOCK );
|
|
73 while ( !get.Connect( m_server, m_port ) )
|
|
74 wxSleep( 1 );
|
|
75
|
|
76 wxString url = wxT( "/images/" ) + date + wxT( "/" ) + hhs + wxT( ".tgz" );
|
|
77
|
|
78 wxInputStream *http_istream = get.GetInputStream( url );
|
|
79 if ( get.GetError() == wxPROTO_NOERR ) {
|
|
80 //int size = http_istream->GetSize();
|
|
81 wxZlibInputStream zlib_istream( http_istream ); // 0: no cache : bad
|
|
82
|
|
83 /*
|
|
84 wxMemoryOutputStream mm_ostream; // 1: ok
|
|
85 http_istream->Read( mm_ostream );
|
|
86 wxMemoryInputStream mm_istream( mm_ostream );
|
|
87 wxZlibInputStream zlib_istream( mm_istream );
|
|
88 */
|
|
89
|
|
90 wxTarEntry* entry;
|
|
91 wxTarInputStream tar_istream( zlib_istream );
|
|
92 int i = 1;
|
|
93 while ( ( entry = tar_istream.GetNextEntry() ) != NULL ) {
|
|
94 //wxString name = entry->GetName();
|
|
95 wxFileOutputStream file_ostream( wxString::Format( wxT( ".cache/%s_%d" ), date, i++ ) );
|
|
96 file_ostream.Write( tar_istream );
|
|
97 file_ostream.Close();
|
|
98 }
|
|
99 } else {
|
|
100 wxMessageBox( wxT( "Re:Searcher Error: get err" ) );
|
|
101 }
|
|
102
|
|
103 //wxDELETE( http_istream );
|
|
104 get.Close();
|
|
105 }
|
|
106
|