X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/70d26c3f4ffb24d50457d405c9595fd23f9e5b7c..9923c37dccb4e5dbe6387ceb3ab0a8202fadc231:/samples/ipc/client.cpp diff --git a/samples/ipc/client.cpp b/samples/ipc/client.cpp index 616ef99d80..f4ad6551f2 100644 --- a/samples/ipc/client.cpp +++ b/samples/ipc/client.cpp @@ -9,212 +9,213 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif #ifndef WX_PRECOMP -#include "wx/wx.h" + #include "wx/wx.h" #endif // Settings common to both executables: determines whether // we're using TCP/IP or real DDE. - #include "ddesetup.h" -#if defined(__WXGTK__) || defined(__WXMOTIF__) +#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) #include "mondrian.xpm" #endif #include "client.h" -MyFrame *frame = NULL; +// ---------------------------------------------------------------------------- +// wxWin macros +// ---------------------------------------------------------------------------- IMPLEMENT_APP(MyApp) -char ipc_buffer[4000]; +BEGIN_EVENT_TABLE(MyFrame, wxFrame) + EVT_MENU(CLIENT_QUIT, MyFrame::OnExit) + EVT_MENU(CLIENT_EXECUTE, MyFrame::OnExecute) + EVT_MENU(CLIENT_POKE, MyFrame::OnPoke) + EVT_MENU(CLIENT_REQUEST, MyFrame::OnRequest) +END_EVENT_TABLE() + +// ---------------------------------------------------------------------------- +// globals +// ---------------------------------------------------------------------------- + wxListBox *the_list = NULL; MyConnection *the_connection = NULL; -MyClient *my_client ; +MyClient *my_client; + +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// MyApp +// ---------------------------------------------------------------------------- // The `main program' equivalent, creating the windows and returning the // main frame bool MyApp::OnInit() { - // Create the main frame window - frame = new MyFrame(NULL, "Client", wxPoint(400, 0), wxSize(400, 300)); - - // Give it an icon - frame->SetIcon(wxICON(mondrian)); - - // Make a menubar - wxMenu *file_menu = new wxMenu; - - file_menu->Append(CLIENT_EXECUTE, "Execute"); - file_menu->Append(CLIENT_REQUEST, "Request"); - file_menu->Append(CLIENT_POKE, "Poke"); - file_menu->Append(CLIENT_QUIT, "Quit"); - - wxMenuBar *menu_bar = new wxMenuBar; - - menu_bar->Append(file_menu, "File"); - - // Associate the menu bar with the frame - frame->SetMenuBar(menu_bar); - - // Make a panel - frame->panel = new wxPanel(frame, -1, wxPoint(0, 0), wxSize(400, 250)); - the_list = new wxListBox(frame->panel, CLIENT_LISTBOX, wxPoint(5, 5), wxSize(150, 120)); - the_list->Append("Apple"); - the_list->Append("Pear"); - the_list->Append("Orange"); - the_list->Append("Banana"); - the_list->Append("Fruit"); - - frame->panel->Fit(); - frame->Fit(); - - wxString server = "4242"; - -#if wxUSE_DDE_FOR_SAMPLE - wxString hostName = wxGetHostName(); -#else - wxString hostName = "localhost"; -#endif - - if (argc > 1) - server = argv[1]; - if (argc > 2) - hostName = argv[2]; - - // Create a new client - my_client = new MyClient; - the_connection = (MyConnection *)my_client->MakeConnection(hostName, server, "IPC TEST"); - - if (!the_connection) - { - wxMessageBox("Failed to make connection to server", "Client Demo Error"); -#ifdef __WXMSW__ -// extern void wxPrintDDEError(); -// wxPrintDDEError(); -#endif - return FALSE; - } - if (!the_connection->StartAdvise("Item")) - wxMessageBox("StartAdvise failed", "Client Demo Error"); - - frame->Show(TRUE); + // service name (DDE classes) or port number (TCP/IP based classes) + wxString service = IPC_SERVICE; + + // ignored under DDE, host name in TCP/IP based classes + wxString hostName = _T("localhost"); + + if (argc > 1) + service = argv[1]; + if (argc > 2) + hostName = argv[2]; + + // Create a new client + my_client = new MyClient; + + // suppress the log messages from MakeConnection() + { + wxLogNull nolog; + the_connection = (MyConnection *) + my_client->MakeConnection(hostName, service, IPC_TOPIC); + + while ( !the_connection ) + { + if ( wxMessageBox(_T("Failed to make connection to server.\nRetry?"), + _T("Client Demo Error"), + wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES ) + { + // no server + return FALSE; + } + + the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, _T("IPC TEST")); + } + } + + if (!the_connection->StartAdvise(IPC_ADVISE_NAME)) + wxMessageBox(_T("StartAdvise failed"), _T("Client Demo Error")); + + // Create the main frame window + (new MyFrame(NULL, _T("Client")))->Show(TRUE); - return TRUE; + return TRUE; } int MyApp::OnExit() { - if (my_client) - delete my_client ; + // will delete the connection too + // Update: Seems it didn't delete the_connection, because there's a leak. + // Deletion is now explicitly done a few lines up. + // another Update: in fact it's because OnDisconnect should delete it, but + // it wasn't + delete my_client; + + return 0; } -BEGIN_EVENT_TABLE(MyFrame, wxFrame) - EVT_MENU(CLIENT_QUIT, MyFrame::OnExit) - EVT_MENU(CLIENT_EXECUTE, MyFrame::OnExecute) - EVT_MENU(CLIENT_POKE, MyFrame::OnPoke) - EVT_MENU(CLIENT_REQUEST, MyFrame::OnRequest) - EVT_CLOSE(MyFrame::OnCloseWindow) -END_EVENT_TABLE() - // Define my frame constructor -MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size): - wxFrame(frame, -1, title, pos, size) +MyFrame::MyFrame(wxFrame *frame, const wxString& title) + : wxFrame(frame, -1, title, wxDefaultPosition, wxSize(300, 200)) { - panel = NULL; -} + // Give it an icon + SetIcon(wxICON(mondrian)); -void MyFrame::OnExecute(wxCommandEvent& event) -{ - if (the_connection) - if (!the_connection->Execute("Hello from the client!")) - wxMessageBox("Execute failed", "Client Demo Error"); -} + // Make a menubar + wxMenu *file_menu = new wxMenu; -void MyFrame::OnPoke(wxCommandEvent& event) -{ - if (the_connection) - if (!the_connection->Poke("An item", "Some data to poke at the server!")) - wxMessageBox("Poke failed", "Client Demo Error"); -} + file_menu->Append(CLIENT_EXECUTE, _T("&Execute\tCtrl-E")); + file_menu->Append(CLIENT_REQUEST, _T("&Request\tCtrl-R")); + file_menu->Append(CLIENT_POKE, _T("&Poke\tCtrl-P")); + file_menu->Append(CLIENT_QUIT, _T("&Quit\tCtrl-Q")); -void MyFrame::OnRequest(wxCommandEvent& event) -{ - if (the_connection) - { - char *data = the_connection->Request("An item"); - if (data) - wxMessageBox(data, "Client: Request", wxOK); - else - wxMessageBox("Request failed", "Client Demo Error"); - } -} + wxMenuBar *menu_bar = new wxMenuBar; -void MyFrame::OnExit(wxCommandEvent& event) -{ - if (the_connection) - the_connection->Disconnect(); + menu_bar->Append(file_menu, _T("&File")); - this->Destroy(); + // Associate the menu bar with the frame + SetMenuBar(menu_bar); + + // Make a listbox which shows the choices made in the server + the_list = new wxListBox(this, CLIENT_LISTBOX, wxPoint(5, 5)); + the_list->Append(_T("Apple")); + the_list->Append(_T("Pear")); + the_list->Append(_T("Orange")); + the_list->Append(_T("Banana")); + the_list->Append(_T("Fruit")); } -// Define the behaviour for the frame closing -void MyFrame::OnCloseWindow(wxCloseEvent& event) +void MyFrame::OnExecute(wxCommandEvent& WXUNUSED(event)) { - if (the_connection) - { - the_connection->Disconnect(); - } - this->Destroy(); + if (the_connection) + if (!the_connection->Execute(_T("Hello from the client!"))) + wxMessageBox(_T("Execute failed"), _T("Client Demo Error")); } -MyClient::MyClient(void) +void MyFrame::OnPoke(wxCommandEvent& WXUNUSED(event)) { + if (the_connection) + if (!the_connection->Poke(_T("An item"), _T("Some data to poke at the server!"))) + wxMessageBox(_T("Poke failed"), _T("Client Demo Error")); } -wxConnectionBase *MyClient::OnMakeConnection(void) +void MyFrame::OnRequest(wxCommandEvent& WXUNUSED(event)) { - return new MyConnection; + if (the_connection) + { + wxChar *data = the_connection->Request(_T("An item")); + if (data) + wxMessageBox(data, _T("Client: Request"), wxOK); + else + wxMessageBox(_T("Request failed"), _T("Client Demo Error")); + } } -MyConnection::MyConnection(void):wxConnection(ipc_buffer, 3999) +void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) { + Close(); } -MyConnection::~MyConnection(void) +wxConnectionBase *MyClient::OnMakeConnection() { - the_connection = NULL; + return new MyConnection; } -bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format) +bool MyConnection::OnAdvise(const wxString& WXUNUSED(topic), const wxString& WXUNUSED(item), wxChar *data, int WXUNUSED(size), wxIPCFormat WXUNUSED(format)) { - if (the_list) - { - int n = the_list->FindString(data); - if (n > -1) - the_list->SetSelection(n); - } - return TRUE; + if (the_list) + { + int n = the_list->FindString(data); + if (n > -1) + the_list->SetSelection(n); + } + return TRUE; } bool MyConnection::OnDisconnect() { - frame->Destroy(); + // when connection is terminated, quit whole program + wxWindow *win = wxTheApp->GetTopWindow(); + if ( win ) + win->Destroy(); + // delete self the_connection = NULL; - delete this; - - return TRUE; + return wxConnection::OnDisconnect(); }