server.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: server.cpp
  3. // Purpose: IPC sample: server
  4. // Author: Julian Smart
  5. // Modified by: Jurgen Doornik
  6. // Created: 25/01/99
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #ifndef WX_PRECOMP
  22. #include "wx/wx.h"
  23. #endif
  24. // Settings common to both executables: determines whether
  25. // we're using TCP/IP or real DDE.
  26. #include "ipcsetup.h"
  27. #ifndef wxHAS_IMAGES_IN_RESOURCES
  28. #include "../sample.xpm"
  29. #endif
  30. #include "server.h"
  31. #include "wx/textdlg.h"
  32. #include "wx/datetime.h"
  33. // ----------------------------------------------------------------------------
  34. // wxWin macros
  35. // ----------------------------------------------------------------------------
  36. IMPLEMENT_APP(MyApp)
  37. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  38. EVT_CLOSE( MyFrame::OnClose )
  39. EVT_BUTTON( ID_START, MyFrame::OnStart )
  40. EVT_CHOICE( ID_SERVERNAME, MyFrame::OnServerName )
  41. EVT_BUTTON( ID_DISCONNECT, MyFrame::OnDisconnect )
  42. EVT_BUTTON( ID_ADVISE, MyFrame::OnAdvise )
  43. wxEND_EVENT_TABLE()
  44. // ============================================================================
  45. // implementation
  46. // ============================================================================
  47. // ----------------------------------------------------------------------------
  48. // MyApp
  49. // ----------------------------------------------------------------------------
  50. bool MyApp::OnInit()
  51. {
  52. if ( !wxApp::OnInit() )
  53. return false;
  54. // Create the main frame window
  55. m_frame = new MyFrame(NULL, "Server");
  56. m_frame->Show(true);
  57. return true;
  58. }
  59. // ----------------------------------------------------------------------------
  60. // MyFrame
  61. // ----------------------------------------------------------------------------
  62. // Define my frame constructor
  63. MyFrame::MyFrame(wxFrame *frame, const wxString& title)
  64. : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(400, 300))
  65. {
  66. #if wxUSE_STATUSBAR
  67. CreateStatusBar();
  68. #endif // wxUSE_STATUSBAR
  69. SetIcon(wxICON(sample));
  70. m_server = NULL;
  71. wxPanel * const panel = new wxPanel(this);
  72. wxBoxSizer * const sizerMain = new wxBoxSizer( wxVERTICAL );
  73. wxFlexGridSizer * const sizerCmds = new wxFlexGridSizer( 2, 0, 0 );
  74. sizerCmds->AddGrowableCol( 1 );
  75. wxButton *btn;
  76. btn = new wxButton(panel, ID_START, "&Start Server");
  77. sizerCmds->Add(btn, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
  78. const wxString choices[] = { IPC_SERVICE, "..." };
  79. wxChoice * const choice = new wxChoice
  80. (
  81. panel,
  82. ID_SERVERNAME,
  83. wxDefaultPosition, wxSize(100, -1),
  84. WXSIZEOF(choices), choices
  85. );
  86. sizerCmds->Add(choice, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
  87. btn = new wxButton(panel, ID_DISCONNECT, "&Disconnect Client");
  88. sizerCmds->Add(btn, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
  89. sizerCmds->AddSpacer(20);
  90. btn = new wxButton( panel, ID_ADVISE, "&Advise");
  91. sizerCmds->Add(btn, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
  92. sizerCmds->AddSpacer(20);
  93. sizerMain->Add(sizerCmds, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
  94. wxStaticBoxSizer * const
  95. sizerLog = new wxStaticBoxSizer(wxVERTICAL, panel, "Server &log");
  96. wxTextCtrl * const textLog = new wxTextCtrl
  97. (
  98. panel,
  99. wxID_ANY,
  100. "",
  101. wxDefaultPosition, wxSize(500, 140),
  102. wxTE_MULTILINE
  103. );
  104. sizerLog->Add(textLog, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
  105. sizerMain->Add(sizerLog, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
  106. panel->SetSizer(sizerMain);
  107. sizerMain->SetSizeHints(panel);
  108. SetClientSize(panel->GetSize());
  109. GetServername()->SetSelection(0);
  110. wxLogTextCtrl *logWindow = new wxLogTextCtrl(textLog);
  111. delete wxLog::SetActiveTarget(logWindow);
  112. wxLogMessage("Click on Start to start the server");
  113. UpdateUI();
  114. }
  115. void MyFrame::UpdateUI()
  116. {
  117. GetStart()->Enable(m_server == NULL);
  118. GetServername()->Enable(m_server == NULL);
  119. GetAdvise()->Enable(m_server && m_server->CanAdvise());
  120. GetDisconnect()->Enable(m_server && m_server->IsConnected());
  121. }
  122. void MyFrame::OnClose(wxCloseEvent& event)
  123. {
  124. wxDELETE(m_server);
  125. event.Skip();
  126. }
  127. void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
  128. {
  129. // Create a new server
  130. m_server = new MyServer;
  131. wxString servername = GetServername()->GetStringSelection();
  132. if (m_server->Create(servername))
  133. {
  134. wxLogMessage("Server %s started", servername);
  135. #if wxUSE_DDE_FOR_IPC
  136. wxLogMessage("Server uses DDE");
  137. #else // !wxUSE_DDE_FOR_IPC
  138. wxLogMessage("Server uses TCP");
  139. #endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC
  140. }
  141. else
  142. {
  143. wxLogMessage("Server %s failed to start", servername);
  144. wxDELETE(m_server);
  145. }
  146. UpdateUI();
  147. }
  148. void MyFrame::OnServerName( wxCommandEvent& WXUNUSED(event) )
  149. {
  150. if ( GetServername()->GetStringSelection() == "..." )
  151. {
  152. wxString s = wxGetTextFromUser
  153. (
  154. "Specify the name of the server",
  155. "Server Name",
  156. "",
  157. this
  158. );
  159. if ( !s.empty() && s != IPC_SERVICE )
  160. {
  161. GetServername()->Insert(s, 0);
  162. GetServername()->SetSelection(0);
  163. }
  164. }
  165. }
  166. void MyFrame::Disconnect()
  167. {
  168. m_server->Disconnect();
  169. UpdateUI();
  170. }
  171. void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event))
  172. {
  173. Disconnect();
  174. }
  175. void MyFrame::OnAdvise(wxCommandEvent& WXUNUSED(event))
  176. {
  177. m_server->Advise();
  178. }
  179. // ----------------------------------------------------------------------------
  180. // MyServer
  181. // ----------------------------------------------------------------------------
  182. MyServer::MyServer() : wxServer()
  183. {
  184. m_connection = NULL;
  185. }
  186. MyServer::~MyServer()
  187. {
  188. Disconnect();
  189. }
  190. wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
  191. {
  192. wxLogMessage("OnAcceptConnection(\"%s\")", topic);
  193. if ( topic == IPC_TOPIC )
  194. {
  195. m_connection = new MyConnection();
  196. wxGetApp().GetFrame()->UpdateUI();
  197. wxLogMessage("Connection accepted");
  198. return m_connection;
  199. }
  200. //else: unknown topic
  201. wxLogMessage("Unknown topic, connection refused");
  202. return NULL;
  203. }
  204. void MyServer::Disconnect()
  205. {
  206. if ( m_connection )
  207. {
  208. wxDELETE(m_connection);
  209. wxGetApp().GetFrame()->UpdateUI();
  210. wxLogMessage("Disconnected client");
  211. }
  212. }
  213. void MyServer::Advise()
  214. {
  215. if ( CanAdvise() )
  216. {
  217. const wxDateTime now = wxDateTime::Now();
  218. m_connection->Advise(m_connection->m_advise, now.Format());
  219. const wxString s = now.FormatTime() + " " + now.FormatDate();
  220. m_connection->Advise(m_connection->m_advise, s.mb_str(), wxNO_LEN);
  221. #if wxUSE_DDE_FOR_IPC
  222. wxLogMessage("DDE Advise type argument cannot be wxIPC_PRIVATE. "
  223. "The client will receive it as wxIPC_TEXT, "
  224. " and receive the correct no of bytes, "
  225. "but not print a correct log entry.");
  226. #endif
  227. char bytes[3] = { '1', '2', '3' };
  228. m_connection->Advise(m_connection->m_advise, bytes, 3, wxIPC_PRIVATE);
  229. }
  230. }
  231. // ----------------------------------------------------------------------------
  232. // MyConnection
  233. // ----------------------------------------------------------------------------
  234. bool
  235. MyConnection::OnExecute(const wxString& topic,
  236. const void *data,
  237. size_t size,
  238. wxIPCFormat format)
  239. {
  240. Log("OnExecute", topic, "", data, size, format);
  241. return true;
  242. }
  243. bool
  244. MyConnection::OnPoke(const wxString& topic,
  245. const wxString& item,
  246. const void *data,
  247. size_t size,
  248. wxIPCFormat format)
  249. {
  250. Log("OnPoke", topic, item, data, size, format);
  251. return wxConnection::OnPoke(topic, item, data, size, format);
  252. }
  253. const void *
  254. MyConnection::OnRequest(const wxString& topic,
  255. const wxString& item,
  256. size_t *size,
  257. wxIPCFormat format)
  258. {
  259. *size = 0;
  260. wxString s,
  261. afterDate;
  262. if ( item.StartsWith("Date", &afterDate) )
  263. {
  264. const wxDateTime now = wxDateTime::Now();
  265. if ( afterDate.empty() )
  266. {
  267. s = now.Format();
  268. *size = wxNO_LEN;
  269. }
  270. else if ( afterDate == "+len" )
  271. {
  272. s = now.FormatTime() + " " + now.FormatDate();
  273. *size = strlen(s.mb_str()) + 1;
  274. }
  275. }
  276. else if ( item == "bytes[3]" )
  277. {
  278. s = "123";
  279. *size = 3;
  280. }
  281. if ( !*size )
  282. {
  283. wxLogMessage("Unknown request for \"%s\"", item);
  284. return NULL;
  285. }
  286. // store the data pointer to which we return in a member variable to ensure
  287. // that the pointer remains valid even after we return
  288. m_requestData = s.mb_str();
  289. const void * const data = m_requestData;
  290. Log("OnRequest", topic, item, data, *size, format);
  291. return data;
  292. }
  293. bool MyConnection::OnStartAdvise(const wxString& topic, const wxString& item)
  294. {
  295. wxLogMessage("OnStartAdvise(\"%s\", \"%s\")", topic, item);
  296. wxLogMessage("Returning true");
  297. m_advise = item;
  298. wxGetApp().GetFrame()->UpdateUI();
  299. return true;
  300. }
  301. bool MyConnection::OnStopAdvise(const wxString& topic, const wxString& item)
  302. {
  303. wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic, item);
  304. wxLogMessage("Returning true");
  305. m_advise.clear();
  306. wxGetApp().GetFrame()->UpdateUI();
  307. return true;
  308. }
  309. bool
  310. MyConnection::DoAdvise(const wxString& item,
  311. const void *data,
  312. size_t size,
  313. wxIPCFormat format)
  314. {
  315. Log("Advise", "", item, data, size, format);
  316. return wxConnection::DoAdvise(item, data, size, format);
  317. }
  318. bool MyConnection::OnDisconnect()
  319. {
  320. wxLogMessage("OnDisconnect()");
  321. wxGetApp().GetFrame()->Disconnect();
  322. return true;
  323. }