]> git.saurik.com Git - wxWidgets.git/blob - samples/ipc/server.cpp
Added PREEXTRALIBS (ugh)
[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_LISTBOX(SERVER_LISTBOX, MyFrame::OnListBoxClick)
49 END_EVENT_TABLE()
50
51 BEGIN_EVENT_TABLE(IPCDialogBox, wxDialog)
52 EVT_BUTTON(SERVER_QUIT_BUTTON, IPCDialogBox::OnQuit)
53 END_EVENT_TABLE()
54
55 // ----------------------------------------------------------------------------
56 // global variables
57 // ----------------------------------------------------------------------------
58
59 char ipc_buffer[4000];
60 MyConnection *the_connection = NULL;
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // MyApp
68 // ----------------------------------------------------------------------------
69
70 bool MyApp::OnInit()
71 {
72 // Create the main frame window
73 (new MyFrame(NULL, "Server"))->Show(TRUE);
74
75 // create the server object
76 wxString server_name = "4242";
77 if (argc > 1)
78 server_name = argv[1];
79
80 // Create a new server
81 m_server = new MyServer;
82 m_server->Create(server_name);
83
84 return TRUE;
85 }
86
87 int MyApp::OnExit()
88 {
89 delete m_server;
90
91 return 0;
92 }
93
94 // ----------------------------------------------------------------------------
95 // MyFrame
96 // ----------------------------------------------------------------------------
97
98 // Define my frame constructor
99 MyFrame::MyFrame(wxFrame *frame, const wxString& title)
100 : wxFrame(frame, -1, title)
101 {
102 panel = NULL;
103
104 CreateStatusBar();
105
106 // Give it an icon
107 SetIcon(wxICON(mondrian));
108
109 // Make a menubar
110 wxMenu *file_menu = new wxMenu;
111
112 file_menu->Append(SERVER_QUIT, "&Exit");
113
114 wxMenuBar *menu_bar = new wxMenuBar;
115
116 menu_bar->Append(file_menu, "&File");
117
118 // Associate the menu bar with the frame
119 SetMenuBar(menu_bar);
120
121 // Make a panel
122 panel = new wxPanel(this);
123 wxListBox *list = new wxListBox(panel, SERVER_LISTBOX, wxPoint(5, 5));
124 list->Append("Apple");
125 list->Append("Pear");
126 list->Append("Orange");
127 list->Append("Banana");
128 list->Append("Fruit");
129
130 panel->Fit();
131 Fit();
132 }
133
134 // Set the client process's listbox to this item
135 void MyFrame::OnListBoxClick(wxCommandEvent& event)
136 {
137 wxListBox* listBox = (wxListBox*) panel->FindWindow(SERVER_LISTBOX);
138 if (listBox)
139 {
140 wxString value = listBox->GetStringSelection();
141 if (the_connection)
142 {
143 the_connection->Advise("Item", (wxChar *)value.c_str());
144 }
145 }
146 }
147
148 // ----------------------------------------------------------------------------
149 // IPCDialogBox
150 // ----------------------------------------------------------------------------
151
152 IPCDialogBox::IPCDialogBox(wxWindow *parent, const wxString& title,
153 const wxPoint& pos, const wxSize& size,
154 MyConnection *connection)
155 : wxDialog(parent, -1, title, pos, size)
156 {
157 m_connection = connection;
158 (void)new wxButton(this, SERVER_QUIT_BUTTON, "Quit this connection",
159 wxPoint(5, 5));
160 Fit();
161 }
162
163 void IPCDialogBox::OnQuit(wxCommandEvent& event)
164 {
165 m_connection->Disconnect();
166 delete m_connection;
167 }
168
169 // ----------------------------------------------------------------------------
170 // MyServer
171 // ----------------------------------------------------------------------------
172
173 wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
174 {
175 if (strcmp(topic, "STDIO") != 0 && strcmp(topic, "IPC TEST") == 0)
176 return new MyConnection(ipc_buffer, WXSIZEOF(ipc_buffer));
177 else
178 return NULL;
179 }
180
181 // ----------------------------------------------------------------------------
182 // MyConnection
183 // ----------------------------------------------------------------------------
184
185 MyConnection::MyConnection(char *buf, int size)
186 : wxConnection(buf, size)
187 {
188 dialog = new IPCDialogBox(wxTheApp->GetTopWindow(), "Connection",
189 wxPoint(100, 100), wxSize(500, 500), this);
190 dialog->Show(TRUE);
191 the_connection = this;
192 }
193
194 MyConnection::~MyConnection()
195 {
196 if (the_connection)
197 {
198 dialog->Destroy();
199 the_connection = NULL;
200 }
201 }
202
203 bool MyConnection::OnExecute(const wxString& WXUNUSED(topic),
204 char *data,
205 int WXUNUSED(size),
206 wxIPCFormat WXUNUSED(format))
207 {
208 wxLogStatus("Execute command: %s", data);
209 return TRUE;
210 }
211
212 bool MyConnection::OnPoke(const wxString& WXUNUSED(topic),
213 const wxString& item,
214 char *data,
215 int WXUNUSED(size),
216 wxIPCFormat WXUNUSED(format))
217 {
218 wxLogStatus("Poke command: %s = %s", item.c_str(), data);
219 return TRUE;
220 }
221
222 char *MyConnection::OnRequest(const wxString& WXUNUSED(topic),
223 const wxString& WXUNUSED(item),
224 int * WXUNUSED(size),
225 wxIPCFormat WXUNUSED(format))
226 {
227 return "Here, have your data, client!";
228 }
229
230 bool MyConnection::OnStartAdvise(const wxString& WXUNUSED(topic),
231 const wxString& WXUNUSED(item))
232 {
233 return TRUE;
234 }
235