1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
%
% The Unix Channel
%
% by Michel Beaudouin-Lafon
%
% Copyright 1990-1993
% Laboratoire de Recherche en Informatique (LRI)
%
% User's Manual main file - to be updated
%
% $Id$
% $CurLog$
%
\documentstyle[11pt,mydoc,twoside]{doc}
\pagestyle{ENTETE}
\makeindex
\namedoc{Unix Channel}
\def\uch{{\sc Uch}}
\begin{document}
\maketitle
\cleardoublepage
\tableofcontents
\chapter{Introduction}
%--------------------------------
The Unix Channel library provides a set of classes to build distributed applications
under Unix using sockets.
The first level of the library, described in chapter \ref{General io}, defines basic
classes: messages to be exchanged, buffers to hold data (usually messages),
file descriptors that represent communication channels, and channel sets for
multiplexing input/output.
The second level, described in chapters \ref{Addresses} and \ref{Basics}, defines
classes to build networks of processes exchanging messages.
The different classes implement different transport protocols: datagrams (fast but
unreliable), byte streams (reliable but without message boundaries), and streams
(reliable delivery of messages).
The third level is a specialized set of classes for implementing distributed
applications using the client/server architecture. Chapter \ref{Servers} describes
the server side, while chapter \ref{Clients} describes the server side.
An application can be both a server and the client of other applications.
Finally chapter \ref{Portserv} describes a set of classes to communicate
addresses between the components of a distributed application.
Most of the time th eoperating system allocates the channel addresses;
a cumbersome part of the protocol often is for a client to know the physical address
of a server from a logical name.
The port server described in chapter \ref{Portserv} implements a service that maps
logical names to physical addresses with a context.
The present documentation assumes some knowledge of the network facilities
available under Unix, as described in the document
``{\em A Socket-Based Interprocess Communication Tutorial}''.
This document is part of the Unix documentation.
Some knowledge of the {\em CCU} library is also needed.
This library is described in the document
``{\em CCU, the CENA C++ Utilities library}''.
\uch\ uses that library mainly for {\em smart pointer} classes.
Given a class \typ{A}, a smart pointer class to \typ{A}, usually named \typ{pA},
implements a class that behaves like pointers to objects of class \typ{A} (\typ{A*}),
with the following additional feature:
the pointed to object maintains a count of the number of smart pointers
pointing to it; when this reference count reaches zero and the object was
allocated dynamically (with \fun{operator new}), it is automatically destroyed.
\section{Using The Unix Channel}
\uch\ is provided as 4 files~:
\begin{enumerate}
\item
the header files \com{chan.h} and \com{uch.h}, usually installed in \com{/usr/include/CC},
contains the definitions for classes, functions and constants provided by \uch.
\com{chan.h} contains the classes described in chapter \ref{General io} and can be used
in simple applications.
\com{uch.h} contains all the classes and is to be used by advanced applications.
One of these files should be included in any file using the library.
As most of \uch\ functions are member functions, this file can
be used as a quick reference.
\item
\samepage
{the archive files \com{libUch.a} and \com{libChan.a}, which are usually installed in \com{/usr/lib}
or in \com{/usr/loc\-al/lib}, contain the library procedures. One of these libraries must be loaded
with the object files which use \uch. This is usually performed
by adding the flag \com{-lUch} or \com{-lChan} in the command line for your C++ compiler.
\uch\ uses the utilities library, so \com{libutils++.a} must be included when loading an application.
\com{libUch.a} contain the object files for the whole library, while \com{libChan.a}
contains only the object files for the classes described in chapter \ref{General io},
corresponding to the header file \com{chan.h}.
For instance, you can type~:
\begin{center}
\com{CC -o demo main.C -lUch -lCcu}
\end{center}}
\end{enumerate}
\chapter{General io}
\label{General io}
This chapter describes the very basic classes of \uch.
Unix input/output is byte oriented: byte buffers can be read and written in file descriptors
with the system calls \fun{read} and \fun{write}.
\uch\ implements message oriented input/output,
although byte io can be used if needed.
The class \typ{UchMessage} is the abstract base class for the messages
to be exchanged between applications. A \typ{UchMessage} only needs to
know how to convert itself to and from a set of bytes in a \typ{MsgBuffer}.
The class \typ{MsgBuffer} is used throughout \uch\ to buffer input and output.
Buffering output can dramatically improve performances (especially if the
messages are short), while buffering input is necessary to implement message
delivery (a message can be received in several pieces that need to be gathered
before actual delivery to the application).
The following types are defined for machine independent handling of data:
\begin{itemize}
\item The type \typ{^{byte}} is defined as an 8-bit quantity. Buffers contain bytes.
\item The type \typ{^{sword}} refers to a 16-bit quantity.
\item The type \typ {^{lword}} refers to a 32-bit quantity.
\end{itemize}.
The class \typ{UchFilDes} encapsulates the Unix notion of file descriptor.
The class \typ{UchChannel} derives from \typ{UchFilDes}, adding virtual functions
for implementing multiplexing and communication protocols.
The class \typ{UchMultiplexer} implements a set of channels for multiplexing
input and output.
These classes are not sufficient to write distributed applications because
there is no way to establish a communication between two processes.
However they can be used for traditional input/output to terminals and files.
This is illustrated in section \ref{General io example}.
\section{Messages}
The basic entities used to transfer information are messages and buffers. Low level
mechanisms ensure the transmission of data and store it in a buffer. That raw data
is then extracted from the buffer, and used to initialize the fields of a message.
The emission of messages works in a similar way: the contents of a message are
copied to a buffer before being sent.
\subsection{Messages}
#iclass UchMessage
\subsection{Buffers}
#iclass UchMsgBuffer
\section{Files and channels}
#iclass UchFilDes
#iclass UchChannel
\section{Multiplexing channels}
#iclass UchBaseMultiplexer
#iclass UchMultiplexer
\subsection{Adding timers}
#iclass UchBaseTimeOut
#iclass UchTimeOut
\subsection{Safe signal handling}
#iclass UchBaseSignalHandler
#iclass UchSignalHandler
\section{Example}
\label{General io example}.
[to be done]
\chapter{Addresses}
\label{Addresses}
Processes can communicate once a channel has been established
between them. One of the process has to create the channel, and thus has to
know the other process. More precisely, a process connects to an {\em address}
on which the other process is listening.
This chapter describes classes that represent such addresses.
Addresses can be established in different {\em domains}.
Think of e-mail addresses and surface mail addresses: they are
completely different although both can be used to reach the same person.
Two domains are implemented: Unix domain addresses, and internet domain addresses.
A Unix domain address is represented by a file in the file system.
A process only needs to open the file to connect to the process that created the address.
An internet domain address is made of a host number and a port number.
It makes it possible for processes on different machines to communicate.
Unfortunately the port numbers of internet addresses are usually assigned by
the system, so that a distant process cannot know this port number to
establish the communication.
The port server described in chapter \ref{Portserv} overcomes this problem.
It is often used to create internet addresses from logical names,
instead of the class \typ{UchInetAddress} described in this chapter.
#class UchAddress
#class UchUnixAddress
#class UchInetAddress
\chapter{Basic communications}
%--------------------------------
\label{Basics}
This chapter describes the main classes for developing distributed applications.
The communication between the processes is done by using Unix sockets.
The class \typ{UchSocket} encapsulates such sockets, but it is an abstract base class.
The classes \typ{UchDatagram} and \typ{UchStream} implement the two main
protocols available under Unix, i.e. datagrams and streams.
These protocols differ in the way the connection can be established and the way
the data is transmitted between the processes.
In particular, streams transfer bytes and do not know about messages.
The class \typ{UchMsgStream} extends the stream protocol to transfer messages
and not bytes.
This is the class that will be used most of the time to build distributed applications,
as illustrated in section \ref{Basics example}
\fig{FIGURES/socketclasses}{Hierarchy of classes}
Figure \ref{fig:socketclasses} shows the derivation tree for the classes described
in this chapter. All derivations are public. This means that the member functions
of a base class are available in its derived classes, although they appear in the
documentation only for the base class.
#class UchSocket
#class UchDatagram
#class UchStream
#class UchMsgStream
#class UchDGRAM
\section{Example}
\label{Basics example}
[to be done]
\chapter{Servers}
%--------------------------------
\label{Servers}
This chapter describes the classes \typ{UchServer} and \typ{UchClient} to be used
to implement a server.
A server is a process that accepts connections from several clients,
and processes their requests.
When implementing a server, there is one object of class \typ{UchServer},
and one object of class \typ{UchClient} for each connected client.
Each such \typ{UchClient} object is connected to another process where
a corresponding \typ{Service} object exists (see figure \ref{fig:clientserver}).
We call these processes the clients of the server:
a \typ{UchClient} represents a connected client in the server,
while a \typ{Service} represents the server in a client.
\fig{FIGURES/clientserver}{Objects in a client-server system}
The class \typ{Service} is useful only if your application follows an
event/request protocol.
If this is not the case, you can create your own class, derived from
class \typ{MsgStream}.
The class \typ{Service} and the associated classes for managing
events are described in chapter \ref{Clients}.
#class UchServer
#class UchClient
\chapter{Clients}
\label{Clients}
An object of a class derived from \typ{UchMsgStream} must exist in a client process to communicate
with its server (implemented by an object of class \typ{UchClient} in the server process).
The class \typ{UchService} described here can be used when the protocol between the
server and its clients is of the event/request type.
\fig{FIGURES/evreq}{Event / request protocol model.}
In the event/request model (see figure \ref{fig:evreq}),
a client communicates with a server by sending requests.
These requests are generally asynchronous, thus allowing buffered i/o.
Some requests may need an answer from the server: those requests are synchronous by nature.
The server sends events to its clients;
events are asynchronous by nature: a server never waits for an answer from a client,
for this would potentially block it.
Incoming requests are stored in an event queue for later processing by the client:
usually the client reads an event in its top-level loop, and responds to it;
this may involve sending requests to the server, that in turn will generate events.
A single client may want to be connected to several servers.
The class \typ{UchService} makes this possible by allowing to share an event queue
between several services.
Thus incoming events are multiplexed, and the top level loop of the client is of the
same form as above.
Each event contains the server that sent it so that it can be easily dispatched by the client.
Events are messages. They are implemented by the virtual base class \typ{EventMsg}.
The event queue of a service is implemented by the class \typ{UchEvtMsgQueue}.
#class UchService
#class UchEventMsg
#class UchGenEvtMsg
#class UchEvtMsgQueue
\chapter{Port registration server}
\label{Portserv}
A client and a server need to agree on an address to establish a communication.
Internet addresses are generally allocated by the system by passing a null port number;
thus, there is no simple way for the clients to know on which address the server is listening.
The standard solution to this problem is to use the services database of Unix,
to associate a given port number to a service name.
This database is not designed to be updated easily (it is the job of the system administrator),
so it is not possible for a server to register its address in this database each time the server is launched.
Instead, for a given service, the database holds the service name and port number of a daemon,
and this daemon maintains the address of the server currently implementing this service.
Several servers may use the same daemon, for instance if a given service
needs one server per different user.
The port server is such a daemon.
The port server is a separate process that stores associations between keys and addresses.
Each such association represents a server that is available.
The key can encode the service name, user, etc...
Servers register themselves by entering their address with a key and an identification
(usually their process number);
they can (they should) remove their entry from the port server whenever they die.
In order to connect to a server, a client inquires the address to the port server by sending it the key.
It is also possible to ask the port server a set of keys based on a regular expression.
The port server itself is run by the following command:
\begin{ccode}
portserv [ service [ file [ savetime ] ] ]
\end{ccode}
\var{service} is the name of the service as stored in the services database (default is ``\com{portserv}'').
\var{file} is the name of a file to store the state of the port server.
Reloading such a file is not currently implemented.
If \var{file} is ``$-$'', standard output is used, if it is ``$--$'', standard error is used.
If \var{file} is not specified, the saving feature is disabled.
If \var{savetime} is specified, it determines how many seconds after a modification
the new state is saved. If it is not specified, the state is saved immediately after each modification.
The save time is intended for port servers that are heavily used in order to reduce file accesses.
#class UchPortServer
\section{Testing the port server}
\com{porttest} is a simple program that can be used to interact with a port server.
Each call to \com{porttest} connects to a port server and executes an operation on it,
depending on the arguments of the command.
The port server that \com{porttest} connects to has name ``\com{portserv}'',
and is on the local host by default.
If the first argument is of the form \com{=}\var{service},
it connects to a service named \var{service} instead of \com{portserv}.
If the first or second argument is of the form \com{:}\var{host},
it connects to a portserv on the machine \var{host} instead of the local host.
The different commands are the following:
\begin{itemize}
\item
\com{porttest [=service] [:host]}\\
Ask the port server to dump its state on its standard error.
\item
\com{porttest [=service] [:host] quit}\\
Make the port server exit.
\item
\com{porttest [=service] [:host] key}\\
Inquire \var{key}. If found, it is printed with its associated address,
else an error message is printed.
\item
\com{porttest [=service] [:host] ?rekey}\\
Inquire all keys matching \var{rekey}.
\item
\com{porttest [=service] [:host] key portnumber}\\
Register \var{key} with an address made of port \var{portnumber} on the local host.
\com{porttest} provides its own identification for registration.
\item
\com{porttest [=service] [:host] key -portnumber}\\
Remove \var{key}, if bound to the address made of port \var{portnumber} on the local host.
\end{itemize}
\section{Sample usage}
[to be done]
\chapter{Error management}
#iclass UchError
\newpage
\appendix
\chapter{Class List}
This chapter contains the list of the classes defined in \uch.
The first section contains the inheritance tree of \uch\ classes.
The second section contains for each class the ordered list of its base classes.
The section number indicated after each class refers to the
documentation of that class. The class names that appear in italic
are defined in other libraries. Classes defined in \uch\ but
which are not documented do not appear in the lists.
\section{Inheritance Tree}
This section contains the set of classes defined in \uch.
Each base class is followed by the indented list of its subclasses.
\input{inhtree.tex}
\newpage
\section{Inheritance List}
This section contains the set of classes defined in \uch.
Each class is followed by its base class, recursively.
Thus, from a given class, one can follow the inheritance link
and thus refer to the documentation for the inherited methods.
\begin{inhlist}{XXXXXXXXXXXXXXXX}
\input{inhlist.tex}
\end{inhlist}
\begin{theindex}
\indexinc
\end{theindex}
\end{document}
|