summaryrefslogtreecommitdiff
path: root/comm/OLD/Agent.cc
blob: dadfd3c801bfdb0319efeed39822ae182c45f300 (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
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
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1993
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Agents, by Stephane Chatty
 *
 *	$Id$
 *	$CurLog$
 */

#include "Agent.h"

/*?class UchAgent
The class \typ{UchAgent} derives from \typ{UchStream}.
It is associated to a channel set to monitor the connected agents in parallel.
Whenever a new agent connects, the virtual function \fun{HandleNew} is called.
This function must return a pointer to an object of class \typ{UchRemoteAgent}.

Because the agents may be on different machines with different architectures,
the byte swapping is always done by the ``server'' (ie. the agent that did not initiate
the communication), by convention. (NOTE: byte swapping is not currently implemented)
?*/

/*?class UchRemoteAgent
Class \typ{UchRemoteAgent} 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{UchRemoteAgent}.
Instances of \fun{UchRemoteAgent} only know that they may belong to an instance of \typ{UchAgent}.
Thus the virtual function \fun{Delete} is redefined to achieve a clean removal from the server's list.
?*/

/*?
Construct an empty server port.
?*/
UchAgent :: UchAgent ()
: UchStream (),
  RemoteAgents (),
  Mpx (0)
{
}

/*?nodoc?*/
UchAgent :: UchAgent (const UchAgent& sp)
: UchStream (sp),
  RemoteAgents (),
  Mpx (0)
{
}

/*?
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)}.
?*/
UchAgent :: UchAgent (UchAddress* a)
: UchStream (a, 0),
  RemoteAgents (),
  Mpx (0)
{
}

/*?nodoc?*/
UchAgent :: ~UchAgent ()
{
	UchRemoteAgent* cl = 0;
	
	while (cl = RemoteAgents.First ())
		cl->Delete ();
	Mpx = 0;	// will delete it if necessary
}

/*?nodoc?*/
UchChannel*
UchAgent :: Copy () const
{
	return new UchAgent (*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
UchAgent :: RemoveRemoteAgent (UchRemoteAgent* cl)
{
	if (RemoteAgents.Remove (cl)) {
		cl->MyLocalAgent = 0;
		HandleRemove (cl);
	} else
		Error (ErrWarn, "UchAgent::RemoveRemoteAgent", "not owned by this server");
}

/*?
Bind the socket and listen to it.
Initialize the channel set of the server to \var{cs}, or create a new one if \var{cs} is 0.
The channel set is used by the server to read and process messages from its clients:
whenever a client connects to the server, an instance of \typ{UchRemoteAgent} is added to the channel set.
Whenever data is readable from a client, it is read in an input buffer and processed as soon
as a full message is received.
Whenever a client closes the connection, it is removed from the channel set of the server.
This function returns true if all went well, else it calls \fun{SysError} and returns false.
You may want to pass your own channel set if you are already multiplexing
i/o on that channel set. For instance, you may have several \typ{UchAgent} objects
in your application.
A smart pointer to the channel set is actually kept in the server, so that it can be shared
securely.
?*/
bool
UchAgent :: Setup (UchBaseMultiplexer* cs)
{
	if (Listen () < 0) {
		SysError (ErrWarn, "UchAgent::Setup");
		return false;
	}
	SetMode (IORead);
	if (!cs)
		cs = new UchMultiplexer;
	Mpx = cs;
	Mpx->Add (this);
	
	return true;
}

/*?
This function removes the agent's registration channel 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.
Unless you added your own channels to this channel set,
this will be the case if there is no remote agent currently connected.
This is the only way to exit properly from \fun{Run}.
?*/
void
UchAgent :: Unlisten ()
{
	if (Mpx)
		Mpx->Remove (*this);
}


/*?nodoc?*/
void
UchAgent :: HandleRead ()
{
	int fd = Accept ();
	if (fd < 0) {
		SysError (ErrWarn, "UchAgent::HandleRead: Accept");
		return;
	}
	UchRemoteAgent* ra = CreateRemote (fd);
	
	ra->SetMode (IOReadSelect);
	RemoteAgents.Append (ra);
	if (Mpx)
		Mpx->Add (ra);
}

/*?
This virtual function is called whenever a new agent connects to this one.
?*/
UchRemoteAgent*
UchAgent :: CreateRemote (int fd)
{
	return new UchRemoteAgent (this, fd);
}

/*?
This virtual function is called whenever a remote agent is removed (\fun{RemoveRemoteAgent}).
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
UchAgent :: HandleRemove (UchRemoteAgent*)
{
}

//	default error functions
//
/*?nextdoc?*/
bool
UchAgent :: 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{RemoveRemoteAgent} 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
UchAgent :: Error (errtype how, const char* who, const char* what)
{
	::Error (how, who, what);
}

/*?
Setup the server if necessary, then scan and process its channel set
by calling \fun{Run} for it.
To have a server active, you need at least to initialize it (and thus know its address),
and then run it.
If you have added your own channels to the channel set of the server,
their \fun{HandleRead} and \fun{HandleWrite} functions will be called normally by \fun{Run}.
?*/
MPX_RES
UchAgent :: Run ()
{
	if (! Mpx)
		if (! Setup ())
			Error (ErrFatal, "UchAgent::Run", "could not setup");
	if (Mpx)
		return Mpx->Run ();
	else
		return isMpxError;
}

/*?
Send a message to all the agents currently connected to this one.
If \var{flush} is true, the output buffer of each remote agent is flushed.
?*/
void
UchAgent :: Broadcast (UchMessage& msg, bool flush)
{
#ifndef CPLUS_BUG19
	CcuListIterOf <UchRemoteAgent> li (RemoteAgents);
	while (++li)
		(*li)->Send (msg, flush);
#else
	CcuListIter li (RemoteAgents);
	while (++li)
		((UchRemoteAgent*)(*li))->Send (msg, flush);
#endif
}

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

/*?nextdoc?*/
void
UchAgent :: Broadcast (UchMsgBuffer& buf, bool flush)
{
#ifndef CPLUS_BUG19
	CcuListIterOf <UchRemoteAgent> li (RemoteAgents);
	while (++li)
		(*li)->Send (buf, flush);
#else
	CcuListIter li (RemoteAgents);
	while (++li)
		((UchRemoteAgent*)(*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
UchAgent :: Broadcast (UchMsgBuffer& buf, UchRemoteAgent* excl, bool flush)
{
#ifndef CPLUS_BUG19
	CcuListIterOf <UchRemoteAgent> li (RemoteAgents);
	while (++li)
		if (*li != excl)
			(*li)->Send (buf, flush);
#else
	CcuListIter li (RemoteAgents);
	while (++li) {
		UchRemoteAgent* a = (UchRemoteAgent*)(*li);
		if (a != excl)
			a->Send (buf, flush);
	}
#endif
}

#ifdef DOC
//fake entries for inline functions

/*?
Return the channel set used by the server.
This is useful if you want to add your own channels to the channel set.
?*/
UchBaseMultiplexer*
UchAgent :: GetMultiplexer ()
{
}
#endif
 
UchRemoteAgent*
UchAgent :: Contact (UchAddress* a)
{
	if (!a)
		return 0;
	UchRemoteAgent* ra = new UchRemoteAgent (this, a);
	if (ra)
		Mpx->Add (ra);
	return ra;
}
 
/*?nodoc?*/
UchRemoteAgent :: UchRemoteAgent (UchAgent* a)
: UchMsgStream (),
  MyLocalAgent (a)
{
}

/*?nodoc?*/
UchRemoteAgent :: UchRemoteAgent (const UchRemoteAgent& cl)
: UchMsgStream (cl),
  MyLocalAgent (cl.MyLocalAgent)
{
}

/*?
Construct a new agent connected to this one on channel \var{ch}.
?*/
UchRemoteAgent :: UchRemoteAgent (UchAgent* a, int fd)
: UchMsgStream (),
  MyLocalAgent (a)
{
	UchChannel::Open (fd);
}

UchRemoteAgent :: UchRemoteAgent (UchAgent* a, UchAddress* addr)
: UchMsgStream (0, addr),
  MyLocalAgent (a)
{
	if (!Setup ())
		SysError (ErrWarn, "Connect");
	SetSyncMode (true);
}


/*?nodoc?*/
UchRemoteAgent :: ~UchRemoteAgent ()
{
	if (MyLocalAgent) {
		UchAgent* s = MyLocalAgent;
		s->Error (ErrWarn, "~UchRemoteAgent", "remote agent still connected; deleting anyway ...\n");
		s->RemoveRemoteAgent (this);
		if (s->Mpx)
			s->Mpx->Remove (*this);	// calls the destructor if no more refs
	}
}

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

/*?
This function must be used to delete a remote agent 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
UchRemoteAgent :: Delete ()
{
	if (MyLocalAgent) {
		UchAgent* s = MyLocalAgent;
		s->RemoveRemoteAgent (this);
		if (s->Mpx)
			s->Mpx->Remove (*this);	// calls the destructor if no more refs
	}
}



#ifdef DOC
//fake entries for inline functions

/*?
Return the server corresponding to a given client.
?*/
UchAgent*
UchRemoteAgent :: GetAgent ()
{
}

#endif /* DOC */