client.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: client.cpp
  3. // Purpose: DDE sample: client
  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. #include "wx/datetime.h"
  28. #include "client.h"
  29. #ifndef wxHAS_IMAGES_IN_RESOURCES
  30. #include "../sample.xpm"
  31. #endif
  32. // ----------------------------------------------------------------------------
  33. // wxWin macros
  34. // ----------------------------------------------------------------------------
  35. IMPLEMENT_APP(MyApp)
  36. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  37. EVT_MENU(wxID_EXIT, MyFrame::OnExit)
  38. EVT_CLOSE( MyFrame::OnClose )
  39. EVT_BUTTON( ID_START, MyFrame::OnStart )
  40. EVT_CHOICE( ID_SERVERNAME, MyFrame::OnServername )
  41. EVT_CHOICE( ID_HOSTNAME, MyFrame::OnHostname )
  42. EVT_CHOICE( ID_TOPIC, MyFrame::OnTopic )
  43. EVT_BUTTON( ID_DISCONNECT, MyFrame::OnDisconnect )
  44. EVT_BUTTON( ID_STARTADVISE, MyFrame::OnStartAdvise )
  45. EVT_BUTTON( ID_STOPADVISE, MyFrame::OnStopAdvise )
  46. EVT_BUTTON( ID_POKE, MyFrame::OnPoke )
  47. EVT_BUTTON( ID_EXECUTE, MyFrame::OnExecute )
  48. EVT_BUTTON( ID_REQUEST, MyFrame::OnRequest )
  49. wxEND_EVENT_TABLE()
  50. // ----------------------------------------------------------------------------
  51. // globals
  52. // ----------------------------------------------------------------------------
  53. // ============================================================================
  54. // implementation
  55. // ============================================================================
  56. // ----------------------------------------------------------------------------
  57. // MyApp
  58. // ----------------------------------------------------------------------------
  59. // The `main program' equivalent, creating the windows and returning the
  60. // main frame
  61. bool MyApp::OnInit()
  62. {
  63. if ( !wxApp::OnInit() )
  64. return false;
  65. // Create the main frame window
  66. m_frame = new MyFrame(NULL, wxT("Client"));
  67. m_frame->Show(true);
  68. return true;
  69. }
  70. int MyApp::OnExit()
  71. {
  72. return 0;
  73. }
  74. // Define my frame constructor
  75. MyFrame::MyFrame(wxFrame *frame, const wxString& title)
  76. : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(400, 300))
  77. {
  78. // Give it an icon
  79. SetIcon(wxICON(sample));
  80. // Make a menubar
  81. wxMenu *file_menu = new wxMenu;
  82. file_menu->Append(wxID_EXIT, wxT("&Quit\tCtrl-Q"));
  83. wxMenuBar *menu_bar = new wxMenuBar;
  84. menu_bar->Append(file_menu, wxT("&File"));
  85. // Associate the menu bar with the frame
  86. SetMenuBar(menu_bar);
  87. // set a dialog background
  88. SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
  89. // add the controls to the frame
  90. wxString strs4[] =
  91. {
  92. IPC_SERVICE, wxT("...")
  93. };
  94. wxString strs5[] =
  95. {
  96. IPC_HOST, wxT("...")
  97. };
  98. wxString strs6[] =
  99. {
  100. IPC_TOPIC, wxT("...")
  101. };
  102. wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
  103. wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );
  104. wxGridSizer *item2 = new wxGridSizer( 4, 0, 0 );
  105. wxButton *item3 = new wxButton( this, ID_START, wxT("Connect to server"), wxDefaultPosition, wxDefaultSize, 0 );
  106. item2->Add( item3, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  107. wxChoice *item5 = new wxChoice( this, ID_HOSTNAME, wxDefaultPosition, wxSize(100,-1), 2, strs5, 0 );
  108. item2->Add( item5, 0, wxALIGN_CENTER|wxALL, 5 );
  109. wxChoice *item4 = new wxChoice( this, ID_SERVERNAME, wxDefaultPosition, wxSize(100,-1), 2, strs4, 0 );
  110. item2->Add( item4, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  111. wxChoice *item6 = new wxChoice( this, ID_TOPIC, wxDefaultPosition, wxSize(100,-1), 2, strs6, 0 );
  112. item2->Add( item6, 0, wxALIGN_CENTER|wxALL, 5 );
  113. wxButton *item7 = new wxButton( this, ID_DISCONNECT, wxT("Disconnect "), wxDefaultPosition, wxDefaultSize, 0 );
  114. item2->Add( item7, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  115. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  116. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  117. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  118. wxButton *item8 = new wxButton( this, ID_STARTADVISE, wxT("StartAdvise"), wxDefaultPosition, wxDefaultSize, 0 );
  119. item2->Add( item8, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  120. wxButton *item9 = new wxButton( this, ID_STOPADVISE, wxT("StopAdvise"), wxDefaultPosition, wxDefaultSize, 0 );
  121. item2->Add( item9, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  122. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  123. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  124. wxButton *item10 = new wxButton( this, ID_EXECUTE, wxT("Execute"), wxDefaultPosition, wxDefaultSize, 0 );
  125. item2->Add( item10, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  126. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  127. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  128. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  129. wxButton *item11 = new wxButton( this, ID_POKE, wxT("Poke"), wxDefaultPosition, wxDefaultSize, 0 );
  130. item2->Add( item11, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  131. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  132. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  133. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  134. wxButton *item12 = new wxButton( this, ID_REQUEST, wxT("Request"), wxDefaultPosition, wxDefaultSize, 0 );
  135. item2->Add( item12, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  136. item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
  137. item1->Add( item2, 1, wxALIGN_CENTER|wxALL, 5 );
  138. item0->Add( item1, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  139. wxStaticBox *item14 = new wxStaticBox( this, -1, wxT("Client log") );
  140. wxStaticBoxSizer *item13 = new wxStaticBoxSizer( item14, wxVERTICAL );
  141. wxTextCtrl *item15 = new wxTextCtrl( this, ID_LOG, wxEmptyString, wxDefaultPosition, wxSize(500,140), wxTE_MULTILINE );
  142. item13->Add( item15, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  143. item0->Add( item13, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  144. this->SetSizer( item0 );
  145. item0->SetSizeHints( this );
  146. // status
  147. m_client = NULL;
  148. GetServername()->SetSelection(0);
  149. GetHostname()->SetSelection(0);
  150. GetTopic()->SetSelection(0);
  151. wxLogTextCtrl *logWindow = new wxLogTextCtrl(GetLog());
  152. delete wxLog::SetActiveTarget(logWindow);
  153. wxLogMessage(wxT("Click on Connect to connect to the server"));
  154. EnableControls();
  155. }
  156. void MyFrame::EnableControls()
  157. {
  158. GetStart()->Enable(m_client == NULL);
  159. GetServername()->Enable(m_client == NULL);
  160. GetHostname()->Enable(m_client == NULL);
  161. GetTopic()->Enable(m_client == NULL);
  162. const bool isConnected = (m_client != NULL && m_client->IsConnected());
  163. GetDisconnect()->Enable(m_client != NULL && isConnected);
  164. GetStartAdvise()->Enable(m_client != NULL && isConnected);
  165. GetStopAdvise()->Enable(m_client != NULL && isConnected);
  166. GetExecute()->Enable(m_client != NULL && isConnected);
  167. GetPoke()->Enable(m_client != NULL && isConnected);
  168. GetRequest()->Enable(m_client != NULL && isConnected);
  169. }
  170. void MyFrame::OnClose(wxCloseEvent& event)
  171. {
  172. wxDELETE(m_client);
  173. event.Skip();
  174. }
  175. void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
  176. {
  177. Close();
  178. }
  179. void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
  180. {
  181. // Connect to the client
  182. wxString servername = GetServername()->GetStringSelection();
  183. wxString hostname = GetHostname()->GetStringSelection();
  184. wxString topic = GetTopic()->GetStringSelection();
  185. m_client = new MyClient;
  186. bool retval = m_client->Connect(hostname, servername, topic);
  187. wxLogMessage(wxT("Client host=\"%s\" port=\"%s\" topic=\"%s\" %s"),
  188. hostname.c_str(), servername.c_str(), topic.c_str(),
  189. retval ? wxT("connected") : wxT("failed to connect"));
  190. if (!retval)
  191. {
  192. wxDELETE(m_client);
  193. }
  194. EnableControls();
  195. }
  196. void MyFrame::OnServername( wxCommandEvent& WXUNUSED(event) )
  197. {
  198. if (GetServername()->GetStringSelection() == wxT("..."))
  199. {
  200. wxString s = wxGetTextFromUser(wxT("Specify the name of the server"),
  201. wxT("Server Name"), wxEmptyString, this);
  202. if (!s.IsEmpty() && s != IPC_SERVICE)
  203. {
  204. GetServername()->Insert(s, 0);
  205. GetServername()->SetSelection(0);
  206. }
  207. }
  208. }
  209. void MyFrame::OnHostname( wxCommandEvent& WXUNUSED(event) )
  210. {
  211. if (GetHostname()->GetStringSelection() == wxT("..."))
  212. {
  213. wxString s = wxGetTextFromUser(wxT("Specify the name of the host (ignored under DDE)"),
  214. wxT("Host Name"), wxEmptyString, this);
  215. if (!s.IsEmpty() && s != IPC_HOST)
  216. {
  217. GetHostname()->Insert(s, 0);
  218. GetHostname()->SetSelection(0);
  219. }
  220. }
  221. }
  222. void MyFrame::OnTopic( wxCommandEvent& WXUNUSED(event) )
  223. {
  224. if (GetTopic()->GetStringSelection() == wxT("..."))
  225. {
  226. wxString s = wxGetTextFromUser(wxT("Specify the name of the topic"),
  227. wxT("Topic Name"), wxEmptyString, this);
  228. if (!s.IsEmpty() && s != IPC_TOPIC)
  229. {
  230. GetTopic()->Insert(s, 0);
  231. GetTopic()->SetSelection(0);
  232. }
  233. }
  234. }
  235. void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event))
  236. {
  237. Disconnect();
  238. }
  239. void MyFrame::Disconnect()
  240. {
  241. wxDELETE(m_client);
  242. EnableControls();
  243. }
  244. void MyFrame::OnStartAdvise(wxCommandEvent& WXUNUSED(event))
  245. {
  246. m_client->GetConnection()->StartAdvise(wxT("something"));
  247. }
  248. void MyFrame::OnStopAdvise(wxCommandEvent& WXUNUSED(event))
  249. {
  250. m_client->GetConnection()->StopAdvise(wxT("something"));
  251. }
  252. void MyFrame::OnExecute(wxCommandEvent& WXUNUSED(event))
  253. {
  254. if (m_client->IsConnected())
  255. {
  256. wxString s = wxT("Date");
  257. m_client->GetConnection()->Execute(s);
  258. m_client->GetConnection()->Execute((const char *)s.c_str(), s.length() + 1);
  259. char bytes[3];
  260. bytes[0] = '1';
  261. bytes[1] = '2';
  262. bytes[2] = '3';
  263. m_client->GetConnection()->Execute(bytes, WXSIZEOF(bytes));
  264. }
  265. }
  266. void MyFrame::OnPoke(wxCommandEvent& WXUNUSED(event))
  267. {
  268. if (m_client->IsConnected())
  269. {
  270. wxString s = wxDateTime::Now().Format();
  271. m_client->GetConnection()->Poke(wxT("Date"), s);
  272. s = wxDateTime::Now().FormatTime() + wxT(" ") + wxDateTime::Now().FormatDate();
  273. m_client->GetConnection()->Poke(wxT("Date"), (const char *)s.c_str(), s.length() + 1);
  274. char bytes[3];
  275. bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
  276. m_client->GetConnection()->Poke(wxT("bytes[3]"), bytes, 3, wxIPC_PRIVATE);
  277. }
  278. }
  279. void MyFrame::OnRequest(wxCommandEvent& WXUNUSED(event))
  280. {
  281. if (m_client->IsConnected())
  282. {
  283. size_t size;
  284. m_client->GetConnection()->Request(wxT("Date"));
  285. m_client->GetConnection()->Request(wxT("Date+len"), &size);
  286. m_client->GetConnection()->Request(wxT("bytes[3]"), &size, wxIPC_PRIVATE);
  287. }
  288. }
  289. // ----------------------------------------------------------------------------
  290. // MyClient
  291. // ----------------------------------------------------------------------------
  292. MyClient::MyClient() : wxClient()
  293. {
  294. m_connection = NULL;
  295. }
  296. bool MyClient::Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic)
  297. {
  298. // suppress the log messages from MakeConnection()
  299. wxLogNull nolog;
  300. m_connection = (MyConnection *)MakeConnection(sHost, sService, sTopic);
  301. return m_connection != NULL;
  302. }
  303. wxConnectionBase *MyClient::OnMakeConnection()
  304. {
  305. return new MyConnection;
  306. }
  307. void MyClient::Disconnect()
  308. {
  309. if (m_connection)
  310. {
  311. m_connection->Disconnect();
  312. wxDELETE(m_connection);
  313. wxGetApp().GetFrame()->EnableControls();
  314. wxLogMessage(wxT("Client disconnected from server"));
  315. }
  316. }
  317. MyClient::~MyClient()
  318. {
  319. Disconnect();
  320. }
  321. // ----------------------------------------------------------------------------
  322. // MyConnection
  323. // ----------------------------------------------------------------------------
  324. bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, const void *data,
  325. size_t size, wxIPCFormat format)
  326. {
  327. Log(wxT("OnAdvise"), topic, item, data, size, format);
  328. return true;
  329. }
  330. bool MyConnection::OnDisconnect()
  331. {
  332. wxLogMessage(wxT("OnDisconnect()"));
  333. wxGetApp().GetFrame()->Disconnect();
  334. return true;
  335. }
  336. bool MyConnection::DoExecute(const void *data, size_t size, wxIPCFormat format)
  337. {
  338. Log(wxT("Execute"), wxEmptyString, wxEmptyString, data, size, format);
  339. bool retval = wxConnection::DoExecute(data, size, format);
  340. if (!retval)
  341. {
  342. wxLogMessage(wxT("Execute failed!"));
  343. }
  344. return retval;
  345. }
  346. const void *MyConnection::Request(const wxString& item, size_t *size, wxIPCFormat format)
  347. {
  348. const void *data = wxConnection::Request(item, size, format);
  349. Log(wxT("Request"), wxEmptyString, item, data, size ? *size : wxNO_LEN, format);
  350. return data;
  351. }
  352. bool MyConnection::DoPoke(const wxString& item, const void *data, size_t size, wxIPCFormat format)
  353. {
  354. Log(wxT("Poke"), wxEmptyString, item, data, size, format);
  355. return wxConnection::DoPoke(item, data, size, format);
  356. }