renaming and moving samples around
[wxWidgets.git] / samples / ipc / server.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: server.cpp
3 // Purpose: DDE 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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 // Settings common to both executables: determines whether
24 // we're using TCP/IP or real DDE.
25
26 #include "ddesetup.h"
27
28 #if defined(__WXGTK__) || defined(__WXMOTIF__)
29 #include "mondrian.xpm"
30 #endif
31
32 #include "server.h"
33
34 MyFrame *frame = NULL;
35
36 IMPLEMENT_APP(MyApp)
37
38 char ipc_buffer[4000];
39 MyConnection *the_connection = NULL;
40 MyServer *my_server ;
41
42 bool MyApp::OnInit()
43 {
44 // Create the main frame window
45 frame = new MyFrame(NULL, "Server", wxDefaultPosition, wxSize(400, 500));
46
47 frame->CreateStatusBar();
48
49 // Give it an icon
50 frame->SetIcon(wxICON(mondrian));
51
52 // Make a menubar
53 wxMenu *file_menu = new wxMenu;
54
55 file_menu->Append(SERVER_QUIT, "&Exit");
56
57 wxMenuBar *menu_bar = new wxMenuBar;
58
59 menu_bar->Append(file_menu, "&File");
60
61 // Associate the menu bar with the frame
62 frame->SetMenuBar(menu_bar);
63
64 // Make a panel
65 frame->panel = new wxPanel(frame, 0, 0, 400, 250);
66 wxListBox *list = new wxListBox(frame->panel, SERVER_LISTBOX,
67 wxPoint(5, 5), wxSize(150, 120));
68 list->Append("Apple");
69 list->Append("Pear");
70 list->Append("Orange");
71 list->Append("Banana");
72 list->Append("Fruit");
73
74 frame->panel->Fit();
75 frame->Fit();
76
77 wxString server_name = "4242";
78 if (argc > 1)
79 server_name = argv[1];
80
81 // Create a new server
82 my_server = new MyServer;
83 my_server->Create(server_name);
84 frame->Show(TRUE);
85
86 return TRUE;
87 }
88
89 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
90 EVT_MENU(SERVER_QUIT, MyFrame::OnExit)
91 EVT_CLOSE(MyFrame::OnCloseWindow)
92 EVT_LISTBOX(SERVER_LISTBOX, MyFrame::OnListBoxClick)
93 END_EVENT_TABLE()
94
95 // Define my frame constructor
96 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
97 wxFrame(frame, -1, title, pos, size)
98 {
99 panel = NULL;
100 }
101
102 void MyFrame::OnExit(wxCommandEvent& event)
103 {
104 if (my_server)
105 delete my_server;
106 this->Destroy();
107 }
108
109 // Set the client process's listbox to this item
110 void MyFrame::OnListBoxClick(wxCommandEvent& event)
111 {
112 wxListBox* listBox = (wxListBox*) panel->FindWindow(SERVER_LISTBOX);
113 if (listBox)
114 {
115 wxString value = listBox->GetStringSelection();
116 if (the_connection)
117 {
118 the_connection->Advise("Item", (char*) (const char*) value);
119 }
120 }
121 }
122
123 void MyFrame::OnCloseWindow(wxCloseEvent& event)
124 {
125 if (my_server)
126 delete my_server;
127 this->Destroy();
128 }
129
130 BEGIN_EVENT_TABLE(IPCDialogBox, wxDialog)
131 EVT_BUTTON(SERVER_QUIT_BUTTON, IPCDialogBox::OnQuit)
132 END_EVENT_TABLE()
133
134 IPCDialogBox::IPCDialogBox(wxFrame *parent, const wxString& title,
135 const wxPoint& pos, const wxSize& size, MyConnection *the_connection):
136 wxDialog(parent, -1, title, pos, size)
137 {
138 connection = the_connection;
139 (void)new wxButton(this, SERVER_QUIT_BUTTON, "Quit this connection", wxPoint(5, 5));
140 Fit();
141 }
142
143 void IPCDialogBox::OnQuit(wxCommandEvent& event)
144 {
145 connection->Disconnect();
146 delete connection;
147 }
148
149 wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
150 {
151 if (strcmp(topic, "STDIO") != 0 && strcmp(topic, "IPC TEST") == 0)
152 return new MyConnection(ipc_buffer, 4000);
153 else
154 return NULL;
155 }
156
157 MyConnection::MyConnection(char *buf, int size):wxConnection(buf, size)
158 {
159 dialog = new IPCDialogBox(frame, "Connection", wxPoint(100, 100), wxSize(500, 500), this);
160 dialog->Show(TRUE);
161 the_connection = this;
162 }
163
164 MyConnection::~MyConnection(void)
165 {
166 if (the_connection)
167 {
168 dialog->Destroy();
169 the_connection = NULL;
170 }
171 }
172
173 bool MyConnection::OnExecute(const wxString& topic, char *data, int size, wxIPCFormat format)
174 {
175 char buf[300];
176 sprintf(buf, "Execute command: %s", data);
177 frame->SetStatusText(buf);
178 return TRUE;
179 }
180
181 bool MyConnection::OnPoke(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
182 {
183 char buf[300];
184 sprintf(buf, "Poke command: %s", data);
185 frame->SetStatusText(buf);
186 return TRUE;
187 }
188
189 char *MyConnection::OnRequest(const wxString& topic, const wxString& item, int *size, wxIPCFormat format)
190 {
191 return "Here, have your data, client!";
192 }
193
194 bool MyConnection::OnStartAdvise(const wxString& topic, const wxString& item)
195 {
196 return TRUE;
197 }
198