view src/net.cpp @ 16:b651aa41b9d4 default tip

hhsinfo method (server)
author pyon@macmini
date Mon, 15 Jul 2019 07:03:05 +0900
parents c262e17de9b1
children
line wrap: on
line source

// Filename   : net.cpp
// Last Change: 2019-05-24 金 16:02:39.
//

#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()
{
}

bool RsHttp::Get( wxString url, wxString file )
{
	bool ret = false;
	wxHTTP get;
	get.SetFlags( wxSOCKET_WAITALL|wxSOCKET_BLOCK );
	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 );
		ret = true;
	} else {
		wxMessageBox( wxT( "Re:Searcher Error: cannot get data." ) );
	}

	wxDELETE( httpStream );
	get.Close();
	return ret;
}

bool RsHttp::GetDB( bool a, bool h, bool i )
{
	bool ret = false;
	if ( a ) ret = Get( wxT( "/db/auth.db"), wxT( "auth.db") );
	if ( h ) ret = Get( wxT( "/db/hhs.db"),  wxT( "hhs.db") );
	wxDateTime now = wxDateTime::Now();
	if ( i || now.GetDay() % 13 == 0 || now.GetDay() % 28 == 0 ) 
		Get( wxT( "/db/index.db"), wxT( "index.db" ) );
	return ret;
}

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, wxString flag )
{
	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" );
	url += wxT( "?q=" ) + flag;

	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

		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();
}