]>
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 | ||
35 | #if defined(__WXGTK__) || defined(__WXMOTIF__) | |
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 VZ |
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 | ||
7921cf2b JS |
59 | char ipc_buffer[4000]; |
60 | MyConnection *the_connection = NULL; | |
4b89c618 VZ |
61 | |
62 | // ============================================================================ | |
63 | // implementation | |
64 | // ============================================================================ | |
65 | ||
66 | // ---------------------------------------------------------------------------- | |
67 | // MyApp | |
68 | // ---------------------------------------------------------------------------- | |
7921cf2b JS |
69 | |
70 | bool MyApp::OnInit() | |
71 | { | |
4b89c618 VZ |
72 | // Create the main frame window |
73 | (new MyFrame(NULL, "Server"))->Show(); | |
7921cf2b | 74 | |
4b89c618 VZ |
75 | // create the server object |
76 | wxString server_name = "4242"; | |
77 | if (argc > 1) | |
78 | server_name = argv[1]; | |
7921cf2b | 79 | |
4b89c618 VZ |
80 | // Create a new server |
81 | m_server = new MyServer; | |
82 | m_server->Create(server_name); | |
7921cf2b | 83 | |
4b89c618 VZ |
84 | return TRUE; |
85 | } | |
7921cf2b | 86 | |
4b89c618 VZ |
87 | int MyApp::OnExit() |
88 | { | |
89 | delete m_server; | |
90 | ||
91 | return 0; | |
92 | } | |
7921cf2b | 93 | |
4b89c618 VZ |
94 | // ---------------------------------------------------------------------------- |
95 | // MyFrame | |
96 | // ---------------------------------------------------------------------------- | |
7921cf2b | 97 | |
4b89c618 VZ |
98 | // Define my frame constructor |
99 | MyFrame::MyFrame(wxFrame *frame, const wxString& title) | |
100 | : wxFrame(frame, -1, title) | |
101 | { | |
102 | panel = NULL; | |
7921cf2b | 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 | |
4b89c618 | 112 | file_menu->Append(SERVER_QUIT, "&Exit"); |
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 | |
4b89c618 VZ |
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"); | |
7921cf2b | 129 | |
4b89c618 VZ |
130 | panel->Fit(); |
131 | Fit(); | |
7921cf2b JS |
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 | { | |
4b89c618 | 143 | the_connection->Advise("Item", (wxChar *)value.c_str()); |
7921cf2b JS |
144 | } |
145 | } | |
146 | } | |
147 | ||
4b89c618 VZ |
148 | // ---------------------------------------------------------------------------- |
149 | // IPCDialogBox | |
150 | // ---------------------------------------------------------------------------- | |
7921cf2b | 151 | |
4b89c618 VZ |
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) | |
7921cf2b | 156 | { |
4b89c618 VZ |
157 | m_connection = connection; |
158 | (void)new wxButton(this, SERVER_QUIT_BUTTON, "Quit this connection", | |
159 | wxPoint(5, 5)); | |
160 | Fit(); | |
7921cf2b JS |
161 | } |
162 | ||
163 | void IPCDialogBox::OnQuit(wxCommandEvent& event) | |
164 | { | |
4b89c618 VZ |
165 | m_connection->Disconnect(); |
166 | delete m_connection; | |
7921cf2b JS |
167 | } |
168 | ||
4b89c618 VZ |
169 | // ---------------------------------------------------------------------------- |
170 | // MyServer | |
171 | // ---------------------------------------------------------------------------- | |
172 | ||
7921cf2b JS |
173 | wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic) |
174 | { | |
4b89c618 VZ |
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; | |
7921cf2b JS |
179 | } |
180 | ||
4b89c618 VZ |
181 | // ---------------------------------------------------------------------------- |
182 | // MyConnection | |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
185 | MyConnection::MyConnection(char *buf, int size) | |
186 | : wxConnection(buf, size) | |
7921cf2b | 187 | { |
4b89c618 VZ |
188 | dialog = new IPCDialogBox(wxTheApp->GetTopWindow(), "Connection", |
189 | wxPoint(100, 100), wxSize(500, 500), this); | |
190 | dialog->Show(TRUE); | |
191 | the_connection = this; | |
7921cf2b JS |
192 | } |
193 | ||
4b89c618 | 194 | MyConnection::~MyConnection() |
7921cf2b | 195 | { |
4b89c618 VZ |
196 | if (the_connection) |
197 | { | |
198 | dialog->Destroy(); | |
199 | the_connection = NULL; | |
200 | } | |
7921cf2b JS |
201 | } |
202 | ||
4b89c618 VZ |
203 | bool MyConnection::OnExecute(const wxString& WXUNUSED(topic), |
204 | char *data, | |
205 | int WXUNUSED(size), | |
206 | wxIPCFormat WXUNUSED(format)) | |
7921cf2b | 207 | { |
4b89c618 VZ |
208 | wxLogStatus("Execute command: %s", data); |
209 | return TRUE; | |
7921cf2b JS |
210 | } |
211 | ||
4b89c618 VZ |
212 | bool MyConnection::OnPoke(const wxString& WXUNUSED(topic), |
213 | const wxString& item, | |
214 | char *data, | |
215 | int WXUNUSED(size), | |
216 | wxIPCFormat WXUNUSED(format)) | |
7921cf2b | 217 | { |
6097c3a2 | 218 | wxLogStatus("Poke command: %s = %s", item.c_str(), data); |
4b89c618 | 219 | return TRUE; |
7921cf2b JS |
220 | } |
221 | ||
4b89c618 VZ |
222 | char *MyConnection::OnRequest(const wxString& WXUNUSED(topic), |
223 | const wxString& WXUNUSED(item), | |
224 | int * WXUNUSED(size), | |
225 | wxIPCFormat WXUNUSED(format)) | |
7921cf2b | 226 | { |
4b89c618 | 227 | return "Here, have your data, client!"; |
7921cf2b JS |
228 | } |
229 | ||
4b89c618 VZ |
230 | bool MyConnection::OnStartAdvise(const wxString& WXUNUSED(topic), |
231 | const wxString& WXUNUSED(item)) | |
7921cf2b | 232 | { |
4b89c618 | 233 | return TRUE; |
7921cf2b JS |
234 | } |
235 |