]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tipc.tex
mention Watcom makefile for wxBase
[wxWidgets.git] / docs / latex / wx / tipc.tex
CommitLineData
a660d684
KB
1\section{Interprocess communication overview}\label{ipcoverview}
2
f010ad48
JS
3Classes: \helpref{wxServer}{wxddeserver},
4\helpref{wxConnection}{wxddeconnection},
5\helpref{wxClient}{wxddeclient}
6%\helpref{wxTCPServer}{wxtcpserver}, \helpref{wxTCPConnection}{wxtcpconnection},
7%\helpref{wxTCPClient}{wxtcpclient}
a660d684 8
f010ad48
JS
9wxWindows has a number of different classes to help with
10interprocess communication and network programming. This section
11only discusses one family of classes -- the DDE-like protocol --
12but here's a list of other useful classes:
a660d684 13
e2a6f233 14\begin{itemize}\itemsep=0pt
f010ad48
JS
15\item \helpref{wxSocketEvent}{wxsocketevent},
16\helpref{wxSocketBase}{wxsocketbase},
17\helpref{wxSocketClient}{wxsocketclient},
e2a6f233 18\helpref{wxSocketServer}{wxsocketserver}: classes for the low-level TCP/IP API.
f010ad48 19\item \helpref{wxProtocol}{wxprotocol}, \helpref{wxURL}{wxurl}, \helpref{wxFTP}{wxftp}, \helpref{wxHTTP}{wxhttp}: classes
e2a6f233
JS
20for programming popular Internet protocols.
21\end{itemize}
22
f010ad48
JS
23wxWindows' DDE-like protocol is a high-level protocol based on
24Windows DDE. There are two implementations of this DDE-like
25protocol: one using real DDE running on Windows only, and another
26using TCP/IP (sockets) that runs on most platforms. Since the API
27and virtually all of the behaviour is the same apart from the
28names of the classes, you should find it easy to switch between
29the two implementations.
e2a6f233 30
f010ad48
JS
31Notice that by including {\tt <wx/ipc.h>} you may define
32convenient synonyms for the IPC classes: {\tt wxServer} for either
33{\tt wxDDEServer} or {\tt wxTCPServer} depending on whether
34DDE-based or socket-based implementation is used and the same
35thing for {\tt wxClient} and {\tt wxConnection}.
d5172a58 36
f010ad48
JS
37By default, DDE implementation is used under Windows. DDE works
38only within one computer. If you want to use IPC between
39different workstations you should define {\tt
40wxUSE\_DDE\_FOR\_IPC} as $0$ before including this header -- this
41will force using TCP/IP implementation even under Windows.
d5172a58 42
f010ad48
JS
43The following description refers to wx... but remember that the
44equivalent wxTCP... and wxDDE... classes can be used in much the
45same way.
e2a6f233 46
f010ad48 47Three classes are central to the DDE-like API:
a660d684
KB
48
49\begin{enumerate}\itemsep=0pt
f010ad48 50\item wxClient. This represents the client application, and is used
a660d684 51only within a client program.
f010ad48 52\item wxServer. This represents the server application, and is used
a660d684 53only within a server program.
f010ad48
JS
54\item wxConnection. This represents the connection from the
55client to the server - both the client and the server use an
56instance of this class, one per connection. Most DDE transactions
57operate on this object.
a660d684
KB
58\end{enumerate}
59
f010ad48
JS
60Messages between applications are usually identified by three
61variables: connection object, topic name and item name. A data
62string is a fourth element of some messages. To create a
63connection (a conversation in Windows parlance), the client
64application uses wxClient::MakeConnection to send a message to the
65server object, with a string service name to identify the server
66and a topic name to identify the topic for the duration of the
67connection. Under Unix, the service name may be either an integer
68port identifier in which case an Internet domain socket will be
69used for the communications or a valid file name (which shouldn't
70exist and will be deleted afterwards) in which case a Unix domain
71socket is created.
0dbfd66d
VZ
72
73{\bf SECURITY NOTE:} Using Internet domain sockets if extremely insecure for
74IPC as there is absolutely no access control for them, use Unix domain sockets
75whenever possible!
a660d684 76
f010ad48
JS
77The server then responds and either vetoes the connection or
78allows it. If allowed, both the server and client objects create
79wxConnection objects which persist until the connection is
80closed. The connection object is then used for sending and
81receiving subsequent messages between client and server -
82overriding virtual functions in your class derived from
83wxConnection allows you to handle the DDE messages.
a660d684
KB
84
85To create a working server, the programmer must:
86
87\begin{enumerate}\itemsep=0pt
f010ad48
JS
88\item Derive a class from wxConnection, providing handlers for various messages sent to the server
89side of a wxConnection (e.g. OnExecute, OnRequest, OnPoke). Only
90the handlers actually required by the application need to be
91overridden.
92\item Derive a class from wxServer, overriding OnAcceptConnection
93to accept or reject a connection on the basis of the topic
94argument. This member must create and return an instance of the
95derived connection class if the connection is accepted.
96\item Create an instance of your server object and call Create to
a660d684 97activate it, giving it a service name.
a660d684
KB
98\end{enumerate}
99
100To create a working client, the programmer must:
101
102\begin{enumerate}\itemsep=0pt
f010ad48
JS
103\item Derive a class from wxConnection, providing handlers for various
104messages sent to the client side of a wxConnection (e.g.
105OnAdvise). Only the handlers actually required by the application
106need to be overridden.
107\item Derive a class from wxClient, overriding OnMakeConnection to
108create and return an instance of the derived connection class.
a660d684 109\item Create an instance of your client object.
f010ad48
JS
110\item When appropriate, create a new connection using
111\helpref{wxClient::MakeConnection}{wxddeclientmakeconnection},
112with arguments host name (processed in Unix only, use `localhost'
113for local computer), service name, and topic name for this
114connection. The client object will call
115\helpref{OnMakeConnection}{wxddeclientonmakeconnection} to create
116a connection object of the derived class if the connection is
117successful.
118\item Use the wxConnection member functions to send messages to the server.
a660d684
KB
119\end{enumerate}
120
121\subsection{Data transfer}
122
f010ad48
JS
123These are the ways that data can be transferred from one
124application to another. These are methods of wxConnection.
a660d684
KB
125
126\begin{itemize}\itemsep=0pt
127\item {\bf Execute:} the client calls the server with a data string representing
128a command to be executed. This succeeds or fails, depending on the
129server's willingness to answer. If the client wants to find the result
130of the Execute command other than success or failure, it has to explicitly
131call Request.
132\item {\bf Request:} the client asks the server for a particular data string
133associated with a given item string. If the server is unwilling to
134reply, the return value is NULL. Otherwise, the return value is a string
135(actually a pointer to the connection buffer, so it should not be
136deallocated by the application).
137\item {\bf Poke:} The client sends a data string associated with an item
138string directly to the server. This succeeds or fails.
139\item {\bf Advise:} The client asks to be advised of any change in data
140associated with a particular item. If the server agrees, the server will
141send an OnAdvise message to the client along with the item and data.
142\end{itemize}
143
144The default data type is wxCF\_TEXT (ASCII text), and the default data
145size is the length of the null-terminated string. Windows-specific data
146types could also be used on the PC.
147
148\subsection{Examples}
149
150See the sample programs {\it server}\/ and {\it client}\/ in the IPC
151samples directory. Run the server, then the client. This demonstrates
152using the Execute, Request, and Poke commands from the client, together
153with an Advise loop: selecting an item in the server list box causes
154that item to be highlighted in the client list box.
155
a660d684
KB
156\subsection{More DDE details}
157
f010ad48
JS
158A wxClient object initiates the client part of a client-server
159DDE-like (Dynamic Data Exchange) conversation (available in both
e2a6f233 160Windows and Unix).
a660d684
KB
161
162To create a client which can communicate with a suitable server,
f010ad48
JS
163you need to derive a class from wxConnection and another from
164wxClient. The custom wxConnection class will receive
165communications in a `conversation' with a server. and the custom
166wxServer is required so that a user-overridden
167\helpref{wxDDEClient::OnMakeConnection}{wxddeclientonmakeconnection}
168member can return a wxDDEConnection of the required class, when a
169connection is made.
a660d684
KB
170
171For example:
172
173\begin{verbatim}
f010ad48 174class MyConnection: public wxConnection {
a660d684 175 public:
f010ad48 176 MyConnection(void)::wxConnection() {}
a660d684 177 ~MyConnection(void) { }
e2a6f233 178 bool OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
a660d684
KB
179 { wxMessageBox(topic, data); }
180};
181
f010ad48 182class MyClient: public wxClient {
a660d684
KB
183 public:
184 MyClient(void) {}
e2a6f233 185 wxConnectionBase *OnMakeConnection(void) { return new MyConnection; }
a660d684
KB
186};
187
188\end{verbatim}
189
f010ad48
JS
190Here, {\bf MyConnection} will respond to
191\helpref{OnAdvise}{wxddeconnectiononadvise} messages sent by the
192server by displaying a message box.
193
194When the client application starts, it must create an instance of
195the derived wxClient. In the following, command line arguments
196are used to pass the host name (the name of the machine the
197server is running on) and the server name (identifying the server
198process). Calling
199\helpref{wxDDEClient::MakeConnection}{wxddeclientmakeconnection}\rtfsp
200implicitly creates an instance of {\bf MyConnection} if the
201request for a connection is accepted, and the client then
202requests an {\it Advise} loop from the server (an Advise loop is
203where the server calls the client when data has changed).
a660d684
KB
204
205\begin{verbatim}
e2a6f233
JS
206 wxString server = "4242";
207 wxString hostName;
208 wxGetHostName(hostName);
a660d684
KB
209
210 // Create a new client
211 MyClient *client = new MyClient;
e2a6f233 212 connection = (MyConnection *)client->MakeConnection(hostName, server, "IPC TEST");
a660d684 213
e2a6f233 214 if (!connection)
a660d684
KB
215 {
216 wxMessageBox("Failed to make connection to server", "Client Demo Error");
217 return NULL;
218 }
e2a6f233 219 connection->StartAdvise("Item");
a660d684
KB
220\end{verbatim}
221
e2a6f233
JS
222Note that it is no longer necessary to call wxDDEInitialize or wxDDECleanUp, since
223wxWindows will do this itself if necessary.
a660d684 224