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