Mercurial > mercurial > hgweb_rsearcher.cgi
diff src/net.cpp @ 8:82f9af6aa7e4
add net.cpp
author | pyon@macmini |
---|---|
date | Tue, 30 Oct 2018 17:26:02 +0900 |
parents | |
children | ae89ce4793d8 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/net.cpp Tue Oct 30 17:26:02 2018 +0900 @@ -0,0 +1,106 @@ +// Filename : net.cpp +// Last Change: 2018-10-30 火 11:08:25. +// + +#include <wx/datetime.h> +#include <wx/stream.h> +#include <wx/zstream.h> +#include <wx/tarstrm.h> +#include <wx/mstream.h> +#include "net.h" + +RsHttp::RsHttp() +{ +} + +RsHttp::~RsHttp() +{ +} + +void RsHttp::Get( wxString url, wxString file ) +{ + wxHTTP get; + while ( !get.Connect( m_server, m_port ) ) + wxSleep( 1 ); + + wxInputStream *httpStream = get.GetInputStream( url ); + if ( get.GetError() == wxPROTO_NOERR ) { + wxFileOutputStream out_stream( file ); + httpStream->Read( out_stream ); + } else { + wxMessageBox( wxT( "Re:Searcher Error: get err" ) ); + } + + wxDELETE( httpStream ); + get.Close(); +} + +void RsHttp::GetDB( void ) +{ + Get( wxT( "/db/auth.db"), wxT( "auth.db") ); + Get( wxT( "/db/hhs.db"), wxT( "hhs.db") ); + wxDateTime now = wxDateTime::Now(); + if ( now.GetDay() % 14 == 0 ) + Get( wxT( "/db/index.db"), wxT( "index.db" ) ); +} + +int RsHttp::GetImagesSize( wxString hhs, wxString date ) +{ + wxHTTP get; + while ( !get.Connect( m_server, m_port ) ) + wxSleep( 1 ); + + wxString url = wxT( "/images/" ) + date + wxT( "/" ) + hhs + wxT( ".tgz" ); + + int size = -1; + wxInputStream *http_istream = get.GetInputStream( url ); + if ( get.GetError() == wxPROTO_NOERR ) { + size = http_istream->GetSize(); + } else { + wxMessageBox( wxT( "Re:Searcher Error: Cannot get file size." ) ); + } + + wxDELETE( http_istream ); + get.Close(); + return size; +} + +void RsHttp::GetImages( wxString hhs, wxString date ) +{ + wxHTTP get; + get.SetTimeout( 30 ); + get.SetFlags( wxSOCKET_WAITALL|wxSOCKET_BLOCK ); + while ( !get.Connect( m_server, m_port ) ) + wxSleep( 1 ); + + wxString url = wxT( "/images/" ) + date + wxT( "/" ) + hhs + wxT( ".tgz" ); + + wxInputStream *http_istream = get.GetInputStream( url ); + if ( get.GetError() == wxPROTO_NOERR ) { + //int size = http_istream->GetSize(); + wxZlibInputStream zlib_istream( http_istream ); // 0: no cache : bad + + /* + wxMemoryOutputStream mm_ostream; // 1: ok + http_istream->Read( mm_ostream ); + wxMemoryInputStream mm_istream( mm_ostream ); + wxZlibInputStream zlib_istream( mm_istream ); + */ + + wxTarEntry* entry; + wxTarInputStream tar_istream( zlib_istream ); + int i = 1; + while ( ( entry = tar_istream.GetNextEntry() ) != NULL ) { + //wxString name = entry->GetName(); + wxFileOutputStream file_ostream( wxString::Format( wxT( ".cache/%s_%d" ), date, i++ ) ); + file_ostream.Write( tar_istream ); + file_ostream.Close(); + } + } else { + wxMessageBox( wxT( "Re:Searcher Error: get err" ) ); + } + + //wxDELETE( http_istream ); + get.Close(); +} +