]>
git.saurik.com Git - wxWidgets.git/blob - samples/ipc/server.cpp
3db49585e08e576de4f9e38fdfdc28cd972398cb
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: IPC sample: server
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 // Settings common to both executables: determines whether
32 // we're using TCP/IP or real DDE.
35 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
36 #include "mondrian.xpm"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
47 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
48 EVT_MENU (SERVER_EXIT
, MyFrame::OnExit
)
49 EVT_LISTBOX(SERVER_LISTBOX
, MyFrame::OnListBoxClick
)
52 BEGIN_EVENT_TABLE(IPCDialogBox
, wxDialog
)
53 EVT_BUTTON(SERVER_QUIT_BUTTON
, IPCDialogBox::OnQuit
)
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 char ipc_buffer
[4000];
61 MyConnection
*the_connection
= NULL
;
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
73 // Create the main frame window
74 (new MyFrame(NULL
, "Server"))->Show(TRUE
);
76 // service name (DDE classes) or port number (TCP/IP based classes)
77 wxString service
= "4242";
82 // Create a new server
83 m_server
= new MyServer
;
84 m_server
->Create(service
);
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // Define my frame constructor
101 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
102 : wxFrame(frame
, -1, title
)
109 SetIcon(wxICON(mondrian
));
112 wxMenu
*file_menu
= new wxMenu
;
114 file_menu
->Append(SERVER_EXIT
, "&Exit");
116 wxMenuBar
*menu_bar
= new wxMenuBar
;
118 menu_bar
->Append(file_menu
, "&File");
120 // Associate the menu bar with the frame
121 SetMenuBar(menu_bar
);
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");
136 // Set the client process's listbox to this item
137 void MyFrame::OnListBoxClick(wxCommandEvent
& WXUNUSED(event
))
139 wxListBox
* listBox
= (wxListBox
*) panel
->FindWindow(SERVER_LISTBOX
);
142 wxString value
= listBox
->GetStringSelection();
145 the_connection
->Advise("Item", (wxChar
*)value
.c_str());
150 void MyFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
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
)
164 m_connection
= connection
;
165 (void)new wxButton(this, SERVER_QUIT_BUTTON
, "Quit this connection",
170 void IPCDialogBox::OnQuit(wxCommandEvent
& event
)
172 m_connection
->Disconnect();
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 wxConnectionBase
*MyServer::OnAcceptConnection(const wxString
& topic
)
182 if (strcmp(topic
, "STDIO") != 0 && strcmp(topic
, "IPC TEST") == 0)
183 return new MyConnection(ipc_buffer
, WXSIZEOF(ipc_buffer
));
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 MyConnection::MyConnection(char *buf
, int size
)
193 : wxConnection(buf
, size
)
195 dialog
= new IPCDialogBox(wxTheApp
->GetTopWindow(), "Connection",
196 wxPoint(100, 100), wxSize(500, 500), this);
198 the_connection
= this;
201 MyConnection::~MyConnection()
206 the_connection
= NULL
;
210 bool MyConnection::OnExecute(const wxString
& WXUNUSED(topic
),
213 wxIPCFormat
WXUNUSED(format
))
215 wxLogStatus("Execute command: %s", data
);
219 bool MyConnection::OnPoke(const wxString
& WXUNUSED(topic
),
220 const wxString
& item
,
223 wxIPCFormat
WXUNUSED(format
))
225 wxLogStatus("Poke command: %s = %s", item
.c_str(), data
);
229 char *MyConnection::OnRequest(const wxString
& WXUNUSED(topic
),
230 const wxString
& WXUNUSED(item
),
231 int * WXUNUSED(size
),
232 wxIPCFormat
WXUNUSED(format
))
234 return "Here, have your data, client!";
237 bool MyConnection::OnStartAdvise(const wxString
& WXUNUSED(topic
),
238 const wxString
& WXUNUSED(item
))