HatchKeeper  0.90
The Free Open-Source Egg Incubation Software
ServerClient.cpp
Go to the documentation of this file.
1 /*******************************************************************/
8 extern void ShowFrame();
9 
10 #include <wx/snglinst.h>
11 #include <wx/ipc.h>
12 
13 // the default service name
14 #define IPC_SERVICE "4655"
15 
16 // the hostname
17 #define IPC_HOST "localhost"
18 
19 // the IPC topic
20 #define IPC_TOPIC "HATCHKEEPER_80"
21 
29 class HatchConnection : public wxConnection
30 {
31 public:
32  virtual bool OnExecute(const wxString& topic, const void *data, size_t size, wxIPCFormat format) wxOVERRIDE;
33  virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format) wxOVERRIDE;
34  //virtual bool OnDisconnect() wxOVERRIDE;
35 };
36 
38 bool HatchConnection::OnExecute(const wxString& topic,const void *data,size_t size,wxIPCFormat format)
39 {
40  //Log("OnExecute", topic, "", data, size, format);
41  ShowFrame();
42  return true;
43 }
44 
46 bool HatchConnection::DoExecute(const void *data, size_t size, wxIPCFormat format)
47 {
48  //Log(wxT("Execute"), wxEmptyString, wxEmptyString, data, size, format);
49  bool a = wxConnection::DoExecute(data, size, format);
50  if (!a) {
51  wxLogMessage(wxT("Execute failed!"));
52  }
53  return a;
54 }
55 
63 class HatchServer : public wxServer
64 {
65 public:
66  HatchServer();
67 
68  void Disconnect();
69  bool IsConnected() { return h_connection != NULL; }
71 
72  virtual wxConnectionBase *OnAcceptConnection(const wxString& topic);
73 
74 protected:
76 };
77 
79 HatchServer::HatchServer() : wxServer() {h_connection = NULL;}
80 
83 {
84  if ( h_connection )
85  wxDELETE(h_connection);
86 }
87 
89 wxConnectionBase *HatchServer::OnAcceptConnection(const wxString& topic)
90 {
91  //Check For Valid Connection
92  if ( topic == IPC_TOPIC ) {
94  //wxLogMessage("Connection accepted");
95  return h_connection;
96  }
97 
98  //Else
99  wxLogMessage("Unknown topic, connection refused");
100  return NULL;
101 }
102 
103 
111 class HatchClient: public wxClient
112 {
113 public:
114  HatchClient();
115  bool Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic);
116  void Disconnect();
117  wxConnectionBase *OnMakeConnection() wxOVERRIDE;
118  bool IsConnected() { return h_connection != NULL; };
120 
121 protected:
123 };
124 
126 HatchClient::HatchClient() : wxClient(){h_connection = NULL;}
127 
129 bool HatchClient::Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic)
130 {
131  // suppress the log messages from MakeConnection()
132  wxLogNull nolog;
133 
134  h_connection = (HatchConnection *)MakeConnection(sHost, sService, sTopic);
135  return h_connection != NULL;
136 }
137 
139 wxConnectionBase *HatchClient::OnMakeConnection()
140 {
141  return new HatchConnection;
142 }
143 
146 {
147  if (h_connection) {
148  h_connection->Disconnect();
149  wxDELETE(h_connection);
150  }
151 }
152 
153 //Declare global Server and Client
156 wxSingleInstanceChecker *h_checker;
HatchServer * h_server
wxSingleInstanceChecker * h_checker
HatchClient * h_client
void ShowFrame()
Shows Splash Screen Then Shows And Raises Main Frame.
#define IPC_TOPIC
A localhost Client Used For Single Instance.
HatchConnection * GetConnection()
HatchClient()
Constructor.
wxConnectionBase * OnMakeConnection() wxOVERRIDE
Returns The Connection If Server Connected.
HatchConnection * h_connection
bool Connect(const wxString &sHost, const wxString &sService, const wxString &sTopic)
Atempts To Connect To Server Of Previous Instance.
void Disconnect()
Disconnect From Server.
bool IsConnected()
This Class Shows The Main Window When A Server Connection Is Made.
virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format) wxOVERRIDE
For Client.
virtual bool OnExecute(const wxString &topic, const void *data, size_t size, wxIPCFormat format) wxOVERRIDE
For Server.
A localhost Server Used For Single Instance.
void Disconnect()
Disconnect Connection.
HatchConnection * h_connection
HatchConnection * GetConnection()
bool IsConnected()
HatchServer()
Constructor.
virtual wxConnectionBase * OnAcceptConnection(const wxString &topic)
Checks To Make Sure This Is A Valid Connection.