changeset 8:82f9af6aa7e4

add net.cpp
author pyon@macmini
date Tue, 30 Oct 2018 17:26:02 +0900
parents 29829e98d510
children ae89ce4793d8
files doc/Todo go/client.go go/server.go include/auth.h include/id.h include/net.h include/rsearcher.h src/auth.cpp src/net.cpp src/rsearcher.cpp
diffstat 10 files changed, 218 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/doc/Todo	Fri Oct 26 21:05:11 2018 +0900
+++ b/doc/Todo	Tue Oct 30 17:26:02 2018 +0900
@@ -6,16 +6,8 @@
     # GET /release/grsearcher.exe, /release/crypto.exe, /release/client1.exe
     copy client1.exe client.exe
 
-* 毎回 clientで auth.db と hhs.db を取得
-	> client -u 192.168.0.1:3910
-    # GET /db/auth.db, /db/hhs.db, /db/index.db (10day)
-	+ GUI(grsearcher) 終了時に auth.db と hhs.db は削除
-	* auth.db は任意の、hhs.db は月一のアップデートだが、漏洩防止のため毎回削除
-    * 毎月14日と28日に index.db を取得
-
 * client で画像取得
 	> client.exe 192.168.0.1:3910 0800012345 20170105
-	# date:00000000 で最新審査会を取得する構想は廃止
 
 * client でアップロード
 	> client.exe -a 192.168.0.1:3910 localfile
@@ -87,3 +79,10 @@
 * server は rc で起動する(cron は電源入れ忘れがあるからダメ)
 	+ Handbook の 11.2
 
+* DB情報の取得を gui で
+	> client -u 192.168.0.1:3910 と同等機能
+    # GET /db/auth.db, /db/hhs.db, /db/index.db (14day) 
+	+ GUI(grsearcher) 終了時に auth.db と hhs.db は削除
+	* auth.db は任意の、hhs.db は月一のアップデートだが、漏洩防止のため毎回削除
+    * 毎月14日と28日に index.db を取得
+
--- a/go/client.go	Fri Oct 26 21:05:11 2018 +0900
+++ b/go/client.go	Tue Oct 30 17:26:02 2018 +0900
@@ -1,7 +1,7 @@
 /*
  client.go  : client-program.
- Version    : 1.1
- Last Change: 2018-10-24 豌エ 17:21:52.
+ Version    : 1.2
+ Last Change: 2018-10-30 轣ォ 09:13:24.
 
  install to: rsearcher_root/
 
@@ -16,9 +16,10 @@
                + doc/
                + .cache/* ( temporary )
 
- $ client.exe -r 192.168.0.1:3910   # get new release
- $ client.exe -u 192.168.0.1:3910   # get new dbs
+ $ client.exe -r 192.168.0.1:3910                   # get new release
+ $ client.exe -u 192.168.0.1:3910                   # get new dbs
  $ client.exe 192.168.0.1:3910 0800012345 20171231  # get images
+ $ client.exe -a 192.168.0.1:3910 upfile.txt        # upload file
 */
 package main
 
@@ -41,22 +42,25 @@
 var version string
 
 func init() {
-	version = "1.1"
+	version = "1.2"
 }
 
 func main() {
     var upgrade  = flag.Bool( "r", false, "" )  // get grsearcher.exe, crypto.exe client.exe
     var update   = flag.Bool( "u", false, "" )  // get auth.db, hhs.db, index.db(14,28)
     var upload   = flag.Bool( "a", false, "" )  // post somefile
+    var cnttime  = flag.Bool( "t", false, "" )  // count time
     var printver = flag.Bool( "v", false, "" )  // print version
 	flag.Parse()
 
 	// print version
 	if *printver {
-		fmt.Println( "client ( version ", version, ")" )
+		fmt.Println( "client [ ver", version, "]" )
 		os.Exit( 0 )
 	}
 
+	st := time.Now()	// start time
+
 	// upload
     if *upload {
         if flag.NArg() != 2 {
@@ -80,6 +84,9 @@
 		message, _ := ioutil.ReadAll( res.Body )
 		fmt.Println( string( message ) )
 
+		if *cnttime {
+			fmt.Println( "---", time.Now().Sub( st ) )
+		}
 		os.Exit( 0 )
 	}
 
@@ -102,6 +109,9 @@
         if _, err := get_file( host, "/release/client.exe", "client1.exe" ); err != nil {
             log.Fatal( err )
         }
+		if *cnttime {
+			fmt.Println( "---", time.Now().Sub( st ) )
+		}
         os.Exit( 0 )
     }
 
@@ -127,6 +137,10 @@
                 log.Fatal( err )
             }
         }
+
+		if *cnttime {
+			fmt.Println( "---", time.Now().Sub( st ) )
+		}
 		os.Exit( 0 )
 	}
 
@@ -158,6 +172,10 @@
     if err := untgz_images( d, b ); err != nil {
 		log.Fatal( err )
     }
+
+	if *cnttime {
+		fmt.Println( "---", time.Now().Sub( st ) )
+	}
 }
 
 /* Subroutines */
--- a/go/server.go	Fri Oct 26 21:05:11 2018 +0900
+++ b/go/server.go	Tue Oct 30 17:26:02 2018 +0900
@@ -1,7 +1,7 @@
 /*
  server.go  : server-program.
  Version    : 1.1
- Last Change: 2018-10-12 驥 14:37:48.
+ Last Change: 2018-10-29 譛 14:58:19.
 
  install to: server_root/
 
@@ -23,15 +23,19 @@
 	"time"
 )
 
-var server string
-var port   string
-var server_root string
-var version string
+var (
+	version string
+	server string
+	port   string
+	server_root string
+	logfile string
+)
 
 func init() {
+	version = "1.2"	// log version
 	port = ":3910"
     server_root = filepath.Dir( os.Args[0] )
-	version = "1.1"
+	logfile = filepath.Join( server_root, "rsearcher.log" )
 }
 
 func main() {
@@ -58,13 +62,21 @@
 
 func handler( w http.ResponseWriter, r *http.Request ) {
 	file := filepath.Join( server_root, filepath.FromSlash( r.URL.Path ) )
+
 	fmt.Println( "[access]", r.RemoteAddr, "|", time.Now().Format( "2006-01-02 15:04" ), "|", file )
+	write_log( "[access] " + r.RemoteAddr + " " + file )
+
 	f, err := os.Open( file )
 	if err != nil {
 		http.NotFound( w, r )
 		return
 	}
 	defer f.Close()
+
+	fi, _ := f.Stat()
+
+	w.Header().Set( "Content-Type",   "rsearcher/octet-stream" )
+	w.Header().Set( "Content-Length", fmt.Sprintf( "%d", fi.Size() ) )
 	io.Copy( w, f )
 }
 
@@ -86,3 +98,14 @@
 	w.Write( []byte( fmt.Sprintf( "%d bytes are recieved.\n", n ) ) )
 }
 
+func write_log( msg string ) {
+	f, err := os.OpenFile( logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644 )
+	if err != nil {
+		log.Fatal( err )
+	}
+	defer f.Close()
+
+	logger := log.New( f, "", log.LstdFlags )
+	logger.Println( msg )
+}
+
--- a/include/auth.h	Fri Oct 26 21:05:11 2018 +0900
+++ b/include/auth.h	Tue Oct 30 17:26:02 2018 +0900
@@ -1,5 +1,5 @@
 // Filename   : auth.h
-// Last Change: 2018-10-23 火 09:02:05.
+// Last Change: 2018-10-29 月 09:11:18.
 //
 
 #ifndef __AUTH_H__
@@ -47,7 +47,7 @@
 
 		void SetServer( wxString server ) { m_server = server; };
         void CreateControls( void );
-		bool GetDB( void );
+		//bool GetDB( void );
 		bool LoadDB( void );
 		bool IsValidUser( void );
 		wxString GetUser( void ) { return m_user; };
--- a/include/id.h	Fri Oct 26 21:05:11 2018 +0900
+++ b/include/id.h	Tue Oct 30 17:26:02 2018 +0900
@@ -1,5 +1,5 @@
 // Filename   : id.h
-// Last Change: 2018-10-26 金 10:43:22.
+// Last Change: 2018-10-30 火 11:07:41.
 //
 
 #ifndef __ID_H__
@@ -7,8 +7,8 @@
 
 #include <wx/wx.h>
 
-#define RSVER "1.1"
-#define RSRELEASE "2018.10.29"
+#define RSVER "1.2"
+#define RSRELEASE "2018.10.30"
 
 enum {
 	ID_MAIN = wxID_HIGHEST + 1,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/net.h	Tue Oct 30 17:26:02 2018 +0900
@@ -0,0 +1,29 @@
+// Filename   : net.h
+// Last Change: 2018-10-29 譛 15:31:38.
+//
+
+#ifndef __NET_H__
+#define __NET_H__
+
+#include <wx/msgdlg.h> 
+#include <wx/wfstream.h>
+#include <wx/protocol/http.h>
+
+class RsHttp {
+	private:
+		wxString m_server;
+		int      m_port;
+
+	public:
+		RsHttp();
+		~RsHttp();
+
+		void SetServer( wxString server, int port ) { m_server = server; m_port = port; };
+		void Get( wxString url, wxString file );
+		void GetDB();
+		int  GetImagesSize( wxString hhs, wxString date );
+		void GetImages( wxString hhs, wxString date );
+};
+
+#endif //__NET_H__
+
--- a/include/rsearcher.h	Fri Oct 26 21:05:11 2018 +0900
+++ b/include/rsearcher.h	Tue Oct 30 17:26:02 2018 +0900
@@ -1,5 +1,5 @@
 // Filename   : rsearcher.h
-// Last Change: 2018-10-26 金 15:10:47.
+// Last Change: 2018-10-30 火 08:48:18.
 //
 
 #ifndef __RSEARCH_H__
@@ -145,7 +145,7 @@
 		void SetAccelerator( void );
 		void Cmd( wxString cmd );
 		void Search( void );
-		void PasteSeaarch( void );
+		void PasteSearch( void );
 		void LoadDB( void );
         bool LoadBitmap( wxScrolledWindow* sc, MyStaticBitmap* sb, wxString file );
         bool LoadBitmaps( wxString date, bool reload );
--- a/src/auth.cpp	Fri Oct 26 21:05:11 2018 +0900
+++ b/src/auth.cpp	Tue Oct 30 17:26:02 2018 +0900
@@ -1,5 +1,5 @@
 // Filename   : auth.cpp
-// Last Change: 2018-10-23 轣ォ 09:05:53.
+// Last Change: 2018-10-29 譛 09:09:57.
 //
 
 #include "id.h"
@@ -118,6 +118,7 @@
     m_textCtrlId->SetFocus();
 }
 
+/*
 bool AuthDialog::GetDB( void )
 {   // http get ( auth.db, hhs.db, index.db )
 	wxArrayString args;
@@ -126,6 +127,7 @@
 	args.Add( m_server );
 	wxExecute( wxJoin( args, ' ', '\\' ), wxEXEC_SYNC|wxEXEC_HIDE_CONSOLE );
 }
+*/
 
 bool AuthDialog::LoadDB( void )
 {
--- /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();
+}
+
--- a/src/rsearcher.cpp	Fri Oct 26 21:05:11 2018 +0900
+++ b/src/rsearcher.cpp	Tue Oct 30 17:26:02 2018 +0900
@@ -1,5 +1,5 @@
 // Filename   : rsearcher.cpp
-// Last Change: 2018-10-26 金 15:12:02.
+// Last Change: 2018-10-30 火 11:19:28.
 //
 
 #include <wx/arrstr.h> 
@@ -302,7 +302,10 @@
 
 void MainFrame::OnPasteSearch( wxCommandEvent& WXUNUSED(event) )
 {
-	PasteSeaarch();
+	m_textCtrlName->SetValue( wxEmptyString );
+	m_textCtrlAddr->SetValue( wxEmptyString );
+	m_dataViewListCtrl->DeleteAllItems();
+	PasteSearch();
 }
 
 void MainFrame::OnPrint( wxCommandEvent& WXUNUSED(event) )
@@ -489,9 +492,10 @@
 
 void MainFrame::Cmd( wxString cmd )
 {
-	m_dataViewListCtrl->DeleteAllItems();
 	m_textCtrlName->SetValue( wxEmptyString );
 	m_textCtrlAddr->SetValue( wxEmptyString );
+	m_dataViewListCtrl->DeleteAllItems();
+	wxGetApp().RemoveFile( wxT( ".cache/*" )  );
 	LoadBitmaps( wxEmptyString, false );
 
     wxRegEx reHhs( wxT( "^0[1238][0-9]{8}$" ) );
@@ -518,7 +522,7 @@
     }
 
     if ( cmd.IsSameAs( wxT( "*" ), false ) ) {
-		PasteSeaarch();
+		PasteSearch();
 		return;
 	}
 
@@ -640,15 +644,15 @@
 
 void MainFrame::GetImages( wxString hhs, wxString date )
 {
-	GetImages2( hhs, date); return;	// here comment out
+	//GetImages2( hhs, date); return;	// here comment out ( by client.exe & return )
 
-	int estimate = 5;
+	int estimate = http.GetImagesSize( hhs, date ) / 1000000;
     wxProgressDialog pd( wxT( "Connecting Server" ), wxT( "Start..." ), estimate, NULL, wxPD_APP_MODAL|wxPD_ELAPSED_TIME|wxPD_REMAINING_TIME|wxPD_AUTO_HIDE );
     pd.SetSize( wxSize( 320, 140 ) );
 	
 	http.GetImages( hhs, date );
 	for ( int i = 0; i < estimate; i++ ) {
-		wxSleep( 1 );
+		wxMilliSleep( 1 );
 		pd.Update( i, wxT( "Now Loading..." ) );
 	}
 }
@@ -740,7 +744,7 @@
 		pd.Update( i, wxString::Format( wxT( "Now loding ... ( %0.1f %% )" ), (float)i ) );
 	}
 	if ( errors.GetCount() > 0 ) {
-		wxMessageBox( errors[0] );
+		wxMessageBox( wxT( "crypto error: " )+ errors[0] );
 		return;
 	}
 	for ( int i = 0; i < output.GetCount(); i++ ) {
@@ -749,7 +753,7 @@
 	}
 }
 
-void MainFrame::PasteSeaarch( void )
+void MainFrame::PasteSearch( void )
 {
 	wxString s;
 	if ( wxTheClipboard->Open() ) {