changeset 43:c41a01e76de6 draft

Added some basic IPC functionality using wxServer, wxClient and wxConnection. Added the -blockamount command line option for an example of usage. git-svn-id: https://bitcoin.svn.sourceforge.net/svnroot/bitcoin/trunk@56 1a98c847-1fd6-4fd8-948a-caf3550aa51b
author sirius-m <sirius-m@1a98c847-1fd6-4fd8-948a-caf3550aa51b>
date Thu, 04 Feb 2010 15:31:46 +0000
parents 3834ed238a28
children a8e228236ff5
files headers.h ipc.cpp ipc.h makefile makefile.unix.wx2.8 makefile.unix.wx2.9 ui.cpp
diffstat 7 files changed, 101 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/headers.h
+++ b/headers.h
@@ -24,6 +24,8 @@
 #include <wx/taskbar.h>
 #include <wx/stdpaths.h>
 #include <wx/utils.h>
+#include <wx/ipc.h>
+#include <wx/ipcbase.h>
 #include <openssl/ecdsa.h>
 #include <openssl/evp.h>
 #include <openssl/rand.h>
@@ -102,6 +104,7 @@
 #include "market.h"
 #include "uibase.h"
 #include "ui.h"
+#include "ipc.h"
 
 #include "xpm/addressbook16.xpm"
 #include "xpm/addressbook20.xpm"
new file mode 100644
--- /dev/null
+++ b/ipc.cpp
@@ -0,0 +1,33 @@
+/*
+ * Inter-process calling functionality
+ */
+
+#include "headers.h"
+
+wxConnectionBase * CServer::OnAcceptConnection (const wxString &topic) {
+    return new CServerConnection;
+}
+
+wxConnectionBase * CClient::OnMakeConnection () {
+    return new CClientConnection;
+}
+
+// For request based handling
+const void * CServerConnection::OnRequest (const wxString &topic, const wxString &item, size_t *size, wxIPCFormat format) {
+    const char * output;
+
+    if (item == "blockamount") {
+        stringstream stream;
+        stream << nBestHeight + 1;
+        output = stream.str().c_str();
+    }
+    else
+        output = "Unknown identifier";
+    
+    return output;
+}
+
+// For event based handling
+bool CClientConnection::OnAdvise (const wxString &topic, const wxString &item, const void *data, size_t size, wxIPCFormat format) {
+    return false;
+}
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/ipc.h
@@ -0,0 +1,28 @@
+#ifndef _IPC_H
+#define	_IPC_H
+
+class CServer : public wxServer {
+public:
+    wxConnectionBase * OnAcceptConnection (const wxString &topic);
+};
+
+class CClient : public wxClient {
+public:
+    wxConnectionBase * OnMakeConnection ();
+};
+
+class CServerConnection : public wxConnection {
+public:
+    const void * OnRequest (const wxString &topic, const wxString &item, size_t *size, wxIPCFormat format);
+};
+
+class CClientConnection : public wxConnection {
+public:
+    CClientConnection() : wxConnection() {}
+    ~CClientConnection() {}
+    
+    bool OnAdvise (const wxString &topic, const wxString &item, const void *data, size_t size, wxIPCFormat format);
+};
+
+#endif	/* _IPC_H */
+
--- a/makefile
+++ b/makefile
@@ -67,10 +67,12 @@
 obj/ui_res.o: ui.rc  rc/bitcoin.ico rc/check.ico rc/send16.bmp rc/send16mask.bmp rc/send16masknoshadow.bmp rc/send20.bmp rc/send20mask.bmp rc/addressbook16.bmp rc/addressbook16mask.bmp rc/addressbook20.bmp rc/addressbook20mask.bmp
 	windres $(WXDEFS) $(INCLUDEPATHS) -o $@ -i $<
 
+obj/ipc.o: ipc.cpp                  $(HEADERS)
+	g++ -c $(CFLAGS) -o $@ $<
 
 
 OBJS=obj/util.o obj/script.o obj/db.o obj/net.o obj/main.o obj/market.o \
-        obj/ui.o obj/uibase.o obj/sha.o obj/irc.o obj/ui_res.o
+        obj/ui.o obj/uibase.o obj/sha.o obj/irc.o obj/ui_res.o obj/ipc.o
 
 bitcoin.exe: headers.h.gch $(OBJS)
 	-kill /f bitcoin.exe
--- a/makefile.unix.wx2.8
+++ b/makefile.unix.wx2.8
@@ -75,11 +75,12 @@
 obj/irc.o: irc.cpp                  $(HEADERS)
 	g++ -c $(CFLAGS) -o $@ $<
 
-
+obj/ipc.o: ipc.cpp                  $(HEADERS)
+	g++ -c $(CFLAGS) -o $@ $<
 
 
 OBJS=obj/util.o obj/script.o obj/db.o obj/net.o obj/main.o obj/market.o \
-        obj/ui.o obj/uibase.o obj/sha.o obj/irc.o
+        obj/ui.o obj/uibase.o obj/sha.o obj/irc.o obj/ipc.o
 
 bitcoin: headers.h.gch $(OBJS)
 	g++ $(CFLAGS) -o $@ $(LIBPATHS) $(OBJS) $(LIBS)
--- a/makefile.unix.wx2.9
+++ b/makefile.unix.wx2.9
@@ -31,7 +31,7 @@
    -l wx_gtk2u$(D)-2.9 \
  -Wl,-Bdynamic \
    -l crypto \
-   -l gtk-x11-2.0 -l gthread-2.0 -l SM
+   -l gtk-x11-2.0 -l gthread-2.0 -l SM \
 
 WXDEFS=-D__WXGTK__ -DNOPCH
 CFLAGS=-O0 -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(WXDEFS) $(INCLUDEPATHS)
@@ -75,11 +75,12 @@
 obj/irc.o: irc.cpp                  $(HEADERS)
 	g++ -c $(CFLAGS) -o $@ $<
 
-
+obj/ipc.o: ipc.cpp                  $(HEADERS)
+	g++ -c $(CFLAGS) -o $@ $<
 
 
 OBJS=obj/util.o obj/script.o obj/db.o obj/net.o obj/main.o obj/market.o \
-        obj/ui.o obj/uibase.o obj/sha.o obj/irc.o
+        obj/ui.o obj/uibase.o obj/sha.o obj/irc.o obj/ipc.o
 
 bitcoin: headers.h.gch $(OBJS)
 	g++ $(CFLAGS) -o $@ $(LIBPATHS) $(OBJS) $(LIBS)
--- a/ui.cpp
+++ b/ui.cpp
@@ -21,6 +21,7 @@
 
 CMainFrame* pframeMain = NULL;
 CMyTaskBarIcon* ptaskbaricon = NULL;
+CServer* pserver = NULL;
 map<string, string> mapAddressBook;
 bool fRandSendTest = false;
 void RandSend();
@@ -384,6 +385,8 @@
     pframeMain = NULL;
     delete ptaskbaricon;
     ptaskbaricon = NULL;
+    delete pserver;
+    pserver = NULL;
 }
 
 void ExitTimeout(void* parg)
@@ -1687,8 +1690,8 @@
 #if !wxUSE_UNICODE
     // Workaround until upgrade to wxWidgets supporting UTF-8
     wxString str = m_staticTextMain->GetLabel();
-    if (str.Find('Â') != wxNOT_FOUND)
-        str.Remove(str.Find('Â'), 1);
+    if (str.Find('�') != wxNOT_FOUND)
+        str.Remove(str.Find('�'), 1);
     m_staticTextMain->SetLabel(str);
 #endif
 #ifndef __WXMSW__
@@ -3548,6 +3551,26 @@
         return false;
     }
 
+    if (mapArgs.count("-blockamount")) {
+        CClient client;
+        wxString hostname = "localhost";
+        wxString server = GetDataDir() + "service";
+        CClientConnection * pconnection = (CClientConnection *)client.MakeConnection(hostname, server, "ipc test");
+        string output = "";
+        if (pconnection) {
+            char * pbuffer = (char *)pconnection->Request("blockamount");
+            while (*pbuffer != '\n') {
+                output += *pbuffer;
+                pbuffer++;
+            }
+        }
+        else {
+            output = "Cannot access Bitcoin. Are you sure the program is running?\n";
+        }
+        fprintf(stderr, "%s", output.c_str());
+        return false;
+    }
+
     if (mapArgs.count("-datadir"))
         strlcpy(pszSetDataDir, mapArgs["-datadir"].c_str(), sizeof(pszSetDataDir));
 
@@ -3755,6 +3778,8 @@
     if (fFirstRun)
         SetStartOnSystemStartup(true);
 
+    pserver = new CServer;
+    pserver->Create(GetDataDir() + "service");
 
     //
     // Tests