summaryrefslogtreecommitdiff
path: root/comm/OLD/Server.cc
blob: 9bc6cec4cc8d6ce11ac0a3d513bd982dd1e40507 (plain)
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
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1993
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Servers
 *
 *	$Id$
 *	$CurLog$
 */

#include "Server.h"

/*?class UchServer
The class \typ{UchServer} derives from \typ{UchStream}.
It is associated to a channel set to monitor the connected clients in parallel.
Whenever a new client connects to the server, the virtual function \fun{CreateClient} is called.
This function must return a pointer to an object of class \typ{UchClient} (or a derived class of \typ{UchClient}).

Because the server and the clients may be on different machines with different architectures,
the byte swapping is always done by the server, by convention. (NOTE: byte swapping is not currently implemented)
?*/

/*?class UchClient
Class \typ{UchClient} derives from \typ{UchMsgStream}.
It is still an abstract class because the virtual functions \fun{NewMessage} and \fun{ConvertAnswer}
of \typ{UchMsgStream} are not defined in \typ{UchClient}.
Instances of \fun{UchClient} only know that they may belong to an instance of \typ{UchServer}.
Thus the virtual function \fun{Delete} is redefined to achieve a clean removal from the server's list.
?*/

//	server ports
//		constructors,
//		destructor
//		copy
//

/*?nodoc?*/
UchServer :: UchServer (const UchServer& sp)
: UchStream (sp),
  Clients ()
{
}

/*?
Construct a server port bound to address \var{a}.
When using Internet domain addresses, \var{a} should refer to
the wildcard address so that clients can connect from any machine.
This can be done with the instruction \com{new UchInetAddress(ANYADDR)}.
?*/
UchServer :: UchServer (UchBaseMultiplexer& m, UchAddress* a)
: UchStream (a, 0),
  Clients ()
{
	if (Listen () < 0) {
		SysError (ErrWarn, "UchServer::UchServer");
	} else {
		SetMode (IORead);
		m.Add (this);
	}
}

/*?nodoc?*/
UchServer :: ~UchServer ()
{
	UchClient* cl = 0;
	
	while (cl = Clients.First ())
		cl->Delete ();
}

/*?nodoc?*/
UchChannel*
UchServer :: Copy () const
{
	return new UchServer (*this);
}

/*?
Remove a client from the set of clients connected to the server.
This is normally done automatically whenever the client disconnects itself,
as a side effect of destroying the client.
?*/
void
UchServer :: RemoveClient (UchClient* cl)
{
	if (Clients.Remove (cl)) {
		cl->MyServer = 0;
		HandleRemove (cl);
	} else
		Error (ErrWarn, "UchServer::RemoveClient", "not owned by this server");
}


/*?
This function removes the server from its channel set.
This has the effect of ignoring any incoming connections.
This function will also make \fun{Run} exit, if there is no other channel in the channel set of the server.
Unless you added your own channels to this channel set,
this will be the case if there is no client currently connected.
This is the only way to exit properly from \fun{Run}.
?*/
void
UchServer :: Unlisten ()
{
	if (Mpx)
		Mpx->Remove (*this);
}


//	handle connections on server port
//

/*?nodoc?*/
void
UchServer :: HandleRead ()
{
	int fd = Accept ();
	if (fd < 0) {
		SysError (ErrWarn, "UchServer::HandleRead: Accept");
		return;
	}
	UchClient* cl = CreateClient (fd);
	
	Clients.Append (cl);
	if (Mpx)
		Mpx->Add (cl);
}

//	Default virtual function for handling new connections
//	If a class MY_CLIENT is derived from UchClient,
//	a class MY_SERVER must be derived from UchServer,
//	with MY_SERVER::CreateClient
//	doing at least a return new MY_CLIENT (fd)
//
/*?
This virtual function is called whenever a new client connects to the server.
The default action is to return \com{new UchClient(ch)}.
This needs to be redefined only if you create a derived class of \typ{UchClient},
say \typ{MY\_CLIENT}, in which case it should be redefined in a derived class
of \typ{UchServer} to return \com{new MY\_CLIENT(ch)}.
?*/
UchClient*
UchServer :: CreateClient (int fd)
{
	return new UchClient (*this, fd);
}

/*?
This virtual function is called whenever a client is removed (\fun{RemoveClient}).
It is a hook for the application to take whatever action; the default action is to do nothing.
When this function is called, the client is already removed from the client list of the server.
?*/
void
UchServer :: HandleRemove (UchClient*)
{
	// nothing
}

//	default error functions
//
/*?nextdoc?*/
bool
UchServer :: SysError (errtype how, const char* who, int excl1, int excl2)
{
	return ::SysError (how, who, excl1, excl2);
}

/*?
Each server port class can have its own error handling routines.
Their default action is to call the global functions \fun{Error} and \fun{SysError}.
They can be called from the following functions:
\fun{Error} can be called from
\fun{RemoveClient} when the client does not belong to this server;
\fun{SysError} can be called from
\fun{Setup} when the socket could not be setup correctly,
and from \fun{HandleRead}
when the connection could not be accepted.
?*/
void
UchServer :: Error (errtype how, const char* who, const char* what)
{
	::Error (how, who, what);
}


/*?
Send a message to all clients currently connected to this server.
If \var{flush} is true, the output buffer of each client is flushed.
?*/
void
UchServer :: Broadcast (UchMessage& msg, bool flush)
{
#ifndef CPLUS_BUG19
	CcuListIterOf <UchClient> li (Clients);
	while (++li)
		(*li)->Send (msg, flush);
#else
	CcuListIter li (Clients);
	while (++li)
		((UchClient*)(*li))->Send (msg, flush);
#endif
}

/*?
Send a message to all clients currently connected to this server, except \var{exclude}.
If \var{flush} is true, the output buffer of each client is flushed.
?*/
void
UchServer :: Broadcast (UchMessage& msg, UchClient* excl, bool flush)
{
#ifndef CPLUS_BUG19
	CcuListIterOf <UchClient> li (Clients);
	while (++li)
		if (*li != excl)
			(*li)->Send (msg, flush);
#else
	CcuListIter li (Clients);
	while (++li) {
		UchClient* a = (UchClient*)(*li);
		if (a != excl)
			a->Send (msg, flush);
	}
#endif
}

/*?nextdoc?*/
void
UchServer :: Broadcast (UchMsgBuffer& buf, bool flush)
{
#ifndef CPLUS_BUG19
	CcuListIterOf <UchClient> li (Clients);
	while (++li)
		(*li)->Send (buf, flush);
#else
	CcuListIter li (Clients);
	while (++li)
		((UchClient*)(*li))->Send (buf, flush);
#endif
}

/*?
These functions are similar to the previous ones, except that they take
a buffer instead of a message. The buffer {\em must} contain a converted message.
It is more efficient to broadcast a buffer than a message because there is less
message conversion overhead.
?*/
void
UchServer :: Broadcast (UchMsgBuffer& buf, UchClient* excl, bool flush)
{
#ifndef CPLUS_BUG19
	CcuListIterOf <UchClient> li (Clients);
	while (++li)
		if (*li != excl)
			(*li)->Send (buf, flush);
#else
	CcuListIter li (Clients);
	while (++li) {
		UchClient* a = (UchClient*)(*li);
		if (a != excl)
			a->Send (buf, flush);
	}
#endif
}

/*?nodoc?*/
UchClient :: UchClient (const UchClient& cl)
: UchMsgStream (cl),
  MyServer (0)
{
}

/*?
Construct a new client connected to the server on file descriptor fd.
?*/
UchClient :: UchClient (UchServer& s, int fd)
: UchMsgStream (),
  MyServer (&s)
{
	SetMode (IOReadSelect);
	UchChannel::Open (fd);
}

/*?nodoc?*/
UchClient :: ~UchClient ()
{
	if (MyServer) {
		UchServer* s = MyServer;
		s->Error (ErrWarn, "~UchClient", "client still in a server; deleting anyway ...\n");
		s->RemoveClient (this);
		if (s->Mpx)
			s->Mpx->Remove (*this);	// calls the destructor if no more refs
	}
}

/*?nodoc?*/
UchChannel*
UchClient :: Copy () const
{
	return new UchClient (*this);
}

/*?
This function must be used to delete a client explicitly.
It is not safe to use the operator delete.
This function is called when an end of file is read from the client;
this means that you usually do not need to call it.
?*/
void
UchClient :: Delete ()
{
	if (MyServer) {
		UchServer* s = MyServer;
		s->RemoveClient (this);
		if (s->Mpx)
			s->Mpx->Remove (*this);	// calls the destructor if no more refs
	}
}

#ifdef DOC

/*?
Return the server corresponding to a given client.
?*/
UchServer*
UchClient :: GetServer ()
{
}

#endif /* DOC */