]>
git.saurik.com Git - wxWidgets.git/blob - samples/ipc/server.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: IPC sample: server
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 // Settings common to both executables: determines whether
32 // we're using TCP/IP or real DDE.
35 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
36 #include "mondrian.xpm"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
47 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
48 EVT_MENU (SERVER_EXIT
, MyFrame::OnExit
)
49 EVT_LISTBOX(SERVER_LISTBOX
, MyFrame::OnListBoxClick
)
52 BEGIN_EVENT_TABLE(IPCDialogBox
, wxDialog
)
53 EVT_BUTTON(SERVER_QUIT_BUTTON
, IPCDialogBox::OnQuit
)
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 MyConnection
*the_connection
= NULL
;
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
72 // Create the main frame window
73 (new MyFrame(NULL
, _T("Server")))->Show(true);
75 // service name (DDE classes) or port number (TCP/IP based classes)
76 wxString service
= IPC_SERVICE
;
81 // Create a new server
82 m_server
= new MyServer
;
83 m_server
->Create(service
);
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // Define my frame constructor
100 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
101 : wxFrame(frame
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(350, 250))
105 #endif // wxUSE_STATUSBAR
108 SetIcon(wxICON(mondrian
));
111 wxMenu
*file_menu
= new wxMenu
;
113 file_menu
->Append(SERVER_EXIT
, _T("&Quit\tCtrl-Q"));
115 wxMenuBar
*menu_bar
= new wxMenuBar
;
117 menu_bar
->Append(file_menu
, _T("&File"));
119 // Associate the menu bar with the frame
120 SetMenuBar(menu_bar
);
123 wxListBox
*list
= new wxListBox(this, SERVER_LISTBOX
);
124 list
->Append(_T("Apple"));
125 list
->Append(_T("Pear"));
126 list
->Append(_T("Orange"));
127 list
->Append(_T("Banana"));
128 list
->Append(_T("Fruit"));
131 // Set the client process's listbox to this item
132 void MyFrame::OnListBoxClick(wxCommandEvent
& WXUNUSED(event
))
134 wxListBox
* listBox
= (wxListBox
*) FindWindow(SERVER_LISTBOX
);
137 wxString value
= listBox
->GetStringSelection();
139 /* Because the_connection only holds one connection, in this sample only
140 one connection can receive advise messages */
143 the_connection
->Advise(IPC_ADVISE_NAME
, (wxChar
*)value
.c_str());
148 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 IPCDialogBox::IPCDialogBox(wxWindow
*parent
, const wxString
& title
,
158 const wxPoint
& pos
, const wxSize
& size
,
159 MyConnection
*connection
)
160 : wxDialog(parent
, wxID_ANY
, title
, pos
, size
)
162 m_connection
= connection
;
163 (void)new wxButton(this, SERVER_QUIT_BUTTON
, _T("Quit this connection"),
168 IPCDialogBox::~IPCDialogBox( )
170 // wxWidgets exit code destroys dialog before destroying the connection in
171 // OnExit, so make sure connection won't try to delete the dialog later.
173 m_connection
->dialog
= NULL
;
176 void IPCDialogBox::OnQuit(wxCommandEvent
& WXUNUSED(event
))
178 m_connection
->Disconnect();
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 wxConnectionBase
*MyServer::OnAcceptConnection(const wxString
& topic
)
188 if ( topic
== IPC_TOPIC
)
189 return new MyConnection();
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 MyConnection::MyConnection()
202 dialog
= new IPCDialogBox(wxTheApp
->GetTopWindow(), _T("Connection"),
203 wxDefaultPosition
, wxDefaultSize
, this);
205 the_connection
= this;
208 MyConnection::~MyConnection()
214 dialog
->m_connection
= NULL
;
217 the_connection
= NULL
;
221 bool MyConnection::OnExecute(const wxString
& WXUNUSED(topic
),
224 wxIPCFormat
WXUNUSED(format
))
226 wxLogStatus(wxT("Execute command: %s"), data
);
230 bool MyConnection::OnPoke(const wxString
& WXUNUSED(topic
),
231 const wxString
& item
,
234 wxIPCFormat
WXUNUSED(format
))
236 wxLogStatus(wxT("Poke command: %s = %s"), item
.c_str(), data
);
240 wxChar
*MyConnection::OnRequest(const wxString
& WXUNUSED(topic
),
241 const wxString
& WXUNUSED(item
),
242 int * WXUNUSED(size
),
243 wxIPCFormat
WXUNUSED(format
))
245 return _T("Here, have your data, client!");
248 bool MyConnection::OnStartAdvise(const wxString
& WXUNUSED(topic
),
249 const wxString
& WXUNUSED(item
))