]>
git.saurik.com Git - wxWidgets.git/blob - samples/ipc/server.cpp
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(__WXMOTIF__)
36 #include "mondrian.xpm"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
47 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
48 EVT_LISTBOX(SERVER_LISTBOX
, MyFrame::OnListBoxClick
)
51 BEGIN_EVENT_TABLE(IPCDialogBox
, wxDialog
)
52 EVT_BUTTON(SERVER_QUIT_BUTTON
, IPCDialogBox::OnQuit
)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 char ipc_buffer
[4000];
60 MyConnection
*the_connection
= NULL
;
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
72 // Create the main frame window
73 (new MyFrame(NULL
, "Server"))->Show(TRUE
);
75 // create the server object
76 wxString server_name
= "4242";
78 server_name
= argv
[1];
80 // Create a new server
81 m_server
= new MyServer
;
82 m_server
->Create(server_name
);
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // Define my frame constructor
99 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
)
100 : wxFrame(frame
, -1, title
)
107 SetIcon(wxICON(mondrian
));
110 wxMenu
*file_menu
= new wxMenu
;
112 file_menu
->Append(SERVER_QUIT
, "&Exit");
114 wxMenuBar
*menu_bar
= new wxMenuBar
;
116 menu_bar
->Append(file_menu
, "&File");
118 // Associate the menu bar with the frame
119 SetMenuBar(menu_bar
);
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");
134 // Set the client process's listbox to this item
135 void MyFrame::OnListBoxClick(wxCommandEvent
& event
)
137 wxListBox
* listBox
= (wxListBox
*) panel
->FindWindow(SERVER_LISTBOX
);
140 wxString value
= listBox
->GetStringSelection();
143 the_connection
->Advise("Item", (wxChar
*)value
.c_str());
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
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
)
157 m_connection
= connection
;
158 (void)new wxButton(this, SERVER_QUIT_BUTTON
, "Quit this connection",
163 void IPCDialogBox::OnQuit(wxCommandEvent
& event
)
165 m_connection
->Disconnect();
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 wxConnectionBase
*MyServer::OnAcceptConnection(const wxString
& topic
)
175 if (strcmp(topic
, "STDIO") != 0 && strcmp(topic
, "IPC TEST") == 0)
176 return new MyConnection(ipc_buffer
, WXSIZEOF(ipc_buffer
));
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 MyConnection::MyConnection(char *buf
, int size
)
186 : wxConnection(buf
, size
)
188 dialog
= new IPCDialogBox(wxTheApp
->GetTopWindow(), "Connection",
189 wxPoint(100, 100), wxSize(500, 500), this);
191 the_connection
= this;
194 MyConnection::~MyConnection()
199 the_connection
= NULL
;
203 bool MyConnection::OnExecute(const wxString
& WXUNUSED(topic
),
206 wxIPCFormat
WXUNUSED(format
))
208 wxLogStatus("Execute command: %s", data
);
212 bool MyConnection::OnPoke(const wxString
& WXUNUSED(topic
),
213 const wxString
& item
,
216 wxIPCFormat
WXUNUSED(format
))
218 wxLogStatus("Poke command: %s = %s", item
.c_str(), data
);
222 char *MyConnection::OnRequest(const wxString
& WXUNUSED(topic
),
223 const wxString
& WXUNUSED(item
),
224 int * WXUNUSED(size
),
225 wxIPCFormat
WXUNUSED(format
))
227 return "Here, have your data, client!";
230 bool MyConnection::OnStartAdvise(const wxString
& WXUNUSED(topic
),
231 const wxString
& WXUNUSED(item
))