]> git.saurik.com Git - wxWidgets.git/blob - samples/ipc/server.cpp
changed version number
[wxWidgets.git] / samples / ipc / server.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: server.cpp
3 // Purpose: IPC sample: server
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 25/01/99
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 // Settings common to both executables: determines whether
32 // we're using TCP/IP or real DDE.
33 #include "ddesetup.h"
34
35 #if defined(__WXGTK__) || defined(__WXMOTIF__)
36 #include "mondrian.xpm"
37 #endif
38
39 #include "server.h"
40
41 // ----------------------------------------------------------------------------
42 // wxWin macros
43 // ----------------------------------------------------------------------------
44
45 IMPLEMENT_APP(MyApp)
46
47 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
48 EVT_MENU (SERVER_EXIT, MyFrame::OnExit)
49 EVT_LISTBOX(SERVER_LISTBOX, MyFrame::OnListBoxClick)
50 END_EVENT_TABLE()
51
52 BEGIN_EVENT_TABLE(IPCDialogBox, wxDialog)
53 EVT_BUTTON(SERVER_QUIT_BUTTON, IPCDialogBox::OnQuit)
54 END_EVENT_TABLE()
55
56 // ----------------------------------------------------------------------------
57 // global variables
58 // ----------------------------------------------------------------------------
59
60 char ipc_buffer[4000];
61 MyConnection *the_connection = NULL;
62
63 // ============================================================================
64 // implementation
65 // ============================================================================
66
67 // ----------------------------------------------------------------------------
68 // MyApp
69 // ----------------------------------------------------------------------------
70
71 bool MyApp::OnInit()
72 {
73 // Create the main frame window
74 (new MyFrame(NULL, "Server"))->Show(TRUE);
75
76 // service name (DDE classes) or port number (TCP/IP based classes)
77 wxString service = "4242";
78
79 if (argc > 1)
80 service = argv[1];
81
82 // Create a new server
83 m_server = new MyServer;
84 m_server->Create(service);
85
86 return TRUE;
87 }
88
89 int MyApp::OnExit()
90 {
91 delete m_server;
92
93 return 0;
94 }
95
96 // ----------------------------------------------------------------------------
97 // MyFrame
98 // ----------------------------------------------------------------------------
99
100 // Define my frame constructor
101 MyFrame::MyFrame(wxFrame *frame, const wxString& title)
102 : wxFrame(frame, -1, title)
103 {
104 panel = NULL;
105
106 CreateStatusBar();
107
108 // Give it an icon
109 SetIcon(wxICON(mondrian));
110
111 // Make a menubar
112 wxMenu *file_menu = new wxMenu;
113
114 file_menu->Append(SERVER_EXIT, "&Exit");
115
116 wxMenuBar *menu_bar = new wxMenuBar;
117
118 menu_bar->Append(file_menu, "&File");
119
120 // Associate the menu bar with the frame
121 SetMenuBar(menu_bar);
122
123 // Make a panel
124 panel = new wxPanel(this);
125 wxListBox *list = new wxListBox(panel, SERVER_LISTBOX, wxPoint(5, 5));
126 list->Append("Apple");
127 list->Append("Pear");
128 list->Append("Orange");
129 list->Append("Banana");
130 list->Append("Fruit");
131
132 panel->Fit();
133 Fit();
134 }
135
136 // Set the client process's listbox to this item
137 void MyFrame::OnListBoxClick(wxCommandEvent& WXUNUSED(event))
138 {
139 wxListBox* listBox = (wxListBox*) panel->FindWindow(SERVER_LISTBOX);
140 if (listBox)
141 {
142 wxString value = listBox->GetStringSelection();
143 if (the_connection)
144 {
145 the_connection->Advise("Item", (wxChar *)value.c_str());
146 }
147 }
148 }
149
150 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
151 {
152 Close(TRUE);
153 }
154
155 // ----------------------------------------------------------------------------
156 // IPCDialogBox
157 // ----------------------------------------------------------------------------
158
159 IPCDialogBox::IPCDialogBox(wxWindow *parent, const wxString& title,
160 const wxPoint& pos, const wxSize& size,
161 MyConnection *connection)
162 : wxDialog(parent, -1, title, pos, size)
163 {
164 m_connection = connection;
165 (void)new wxButton(this, SERVER_QUIT_BUTTON, "Quit this connection",
166 wxPoint(5, 5));
167 Fit();
168 }
169
170 void IPCDialogBox::OnQuit(wxCommandEvent& event)
171 {
172 m_connection->Disconnect();
173 delete m_connection;
174 }
175
176 // ----------------------------------------------------------------------------
177 // MyServer
178 // ----------------------------------------------------------------------------
179
180 wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
181 {
182 if (strcmp(topic, "STDIO") != 0 && strcmp(topic, "IPC TEST") == 0)
183 return new MyConnection(ipc_buffer, WXSIZEOF(ipc_buffer));
184 else
185 return NULL;
186 }
187
188 // ----------------------------------------------------------------------------
189 // MyConnection
190 // ----------------------------------------------------------------------------
191
192 MyConnection::MyConnection(char *buf, int size)
193 : wxConnection(buf, size)
194 {
195 dialog = new IPCDialogBox(wxTheApp->GetTopWindow(), "Connection",
196 wxPoint(100, 100), wxSize(500, 500), this);
197 dialog->Show(TRUE);
198 the_connection = this;
199 }
200
201 MyConnection::~MyConnection()
202 {
203 if (the_connection)
204 {
205 dialog->Destroy();
206 the_connection = NULL;
207 }
208 }
209
210 bool MyConnection::OnExecute(const wxString& WXUNUSED(topic),
211 char *data,
212 int WXUNUSED(size),
213 wxIPCFormat WXUNUSED(format))
214 {
215 wxLogStatus("Execute command: %s", data);
216 return TRUE;
217 }
218
219 bool MyConnection::OnPoke(const wxString& WXUNUSED(topic),
220 const wxString& item,
221 char *data,
222 int WXUNUSED(size),
223 wxIPCFormat WXUNUSED(format))
224 {
225 wxLogStatus("Poke command: %s = %s", item.c_str(), data);
226 return TRUE;
227 }
228
229 char *MyConnection::OnRequest(const wxString& WXUNUSED(topic),
230 const wxString& WXUNUSED(item),
231 int * WXUNUSED(size),
232 wxIPCFormat WXUNUSED(format))
233 {
234 return "Here, have your data, client!";
235 }
236
237 bool MyConnection::OnStartAdvise(const wxString& WXUNUSED(topic),
238 const wxString& WXUNUSED(item))
239 {
240 return TRUE;
241 }
242