summaryrefslogtreecommitdiff
path: root/comm/OLD/TextServer.cc
blob: 4b9ac14acec6048cc438cbdab920e8a24200111d (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
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1993
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Text-oriented servers
 *
 *	$Id$
 *	$CurLog$
 */

#include "TextServer.h"
#include "Multiplexer.h"
#include "TimeOut.h"
#include "error.h"
#include "PortServer.h"
#include <stdio.h>

/* every REG_TIME milliseconds, the server registers to the port server
   we want this time to be shorter than the REG_TIME of the port server.
   half the value of the port server's REG_TIME seems reasonable */
#define REG_TIME	30000

class UchTextServerTimer : public UchBaseTimeOut {
protected:
	UchTextServer*	Server;

public:
		UchTextServerTimer (UchTextServer*);
		~UchTextServerTimer ();
	void	Handle (Millisecond);
};

UchTextServerTimer :: UchTextServerTimer (UchTextServer* s)
: UchBaseTimeOut (*s->GetMultiplexer (), REG_TIME),
  Server (s)
{
	Handle (0);
}


UchTextServerTimer :: ~UchTextServerTimer ()
{
	PortServerRemove ("agent", Server->service, Server->BoundTo ());
}

void
UchTextServerTimer :: Handle (Millisecond)
{
	PortServerRegister ("agent", Server->service, Server->BoundTo ());
}


/*?
Create a \var{UchTextServer} registered with the portserver under the name \var{serv}.
\var{newcl} is the function that instantiates clients.
It takes a pointer to this server as argument, and must return a dynamically
allocated object of a derived class of \var{UchTextClient}.
In most cases, the function's body is \com{return new MY_CLIENT (s);},
if \typ{MY_CLIENT} is the derived class of \var{UchTextClient} defined
by the application and \var{s} is this server.
?*/
UchTextServer :: UchTextServer (const char* serv, fNEW_CLIENT newcl)
: UchStream (new UchInetAddress (ANYADDR), 0),
  Timer (0),
  ok (FALSE),
  Clients ()
{
	service = serv;
	newClient = newcl;
}

/*?
Remove the service name from the port server.
This destructor does not close down the server.
Use \fun{Quit} to terminate the server properly.
?*/
UchTextServer :: ~UchTextServer ()
{
	if (Timer)
		delete Timer;
}

/*?
Initialize the server, register it with the multiplexer,
and register its address with the port server.
?*/
bool
UchTextServer :: Init (UchBaseMultiplexer& mpx)
{
	if (Listen () < 0) {	// calls Setup
		SysError (ErrWarn, "UchTextServer::Setup");
		return FALSE;
	}
	mpx.Add (*this);
	ok = TRUE;
	char buf [128];
	sprintf (buf, "listening on port #%d", (void*) ((UchInetAddress*) BoundTo ())->Port ());
	Error (ErrLog, "UchTextServer::Init", buf);
	Timer = new UchTextServerTimer (this);
	return TRUE;
}

/*?
Terminate the server.
First each client is notified that the server is about to quit by calling
its \var{Quitting} virtual member and removing it from the multiplexer.
This should normally delete the client.
Finally, the server itself is removed from the multiplexer, which should
delete it in a similar way.
?*/
void
UchTextServer :: Quit ()
{
	if (! ok)
		return;
	ok = FALSE;
	// a bit tricky because of the smart pointers:
	// each tstream probably is referenced only through the multiplexer
	// removing it from the multiplexer hence deletes it.
	// The same thing holds for the UchTextServer itself
	// this is why we delete it last.
	// Finally, this should make the multiplexer loop exit,
	// if there are no other channels in the multiplexer.
#ifndef CPLUS_BUG19
	CcuListIterOf <UchTextClient> ci = Clients;
	while (++ci) {
		UchTextClient* c = *ci;
#else
	CcuListIter ci = Clients;
	while (++ci) {
		UchTextClient* c = (UchTextClient*) *ci;
#endif
		c->Quitting ();// signal the client we're leaving
		if (Mpx)
			Mpx->Remove (*c);
	}
	Mpx->Remove (*this);
}

/*?
Remove a client from the client list and unregister it from the multiplexer.
This should delete the client, since the multiplexer probably holds the
last smart pointer to the client.
This protected function must be called by a client that is about to disappear.
?*/
void
UchTextServer :: Remove (UchTextClient* s)
{
	if (! ok)
		return;

	int found = Clients.Remove (s);

	if (!found)
		Error (ErrWarn, "UchTextServer::Remove", "stream not in client list");

	if (Mpx)
		Mpx->Remove (*s);
}

/*?
The implementation of \fun{UchChannel::HandleRead}.
The server accepts the incoming conncetion and calls \fun{CreateClient}
to instantiate the new client.
?*/
void
UchTextServer :: HandleRead ()
{
	if (! ok)
		return;
	// can't use UchChannel::Accept or SOCKET::SockAccept
	// (should change their interface)
	int fd = accept (FilDes (), 0, 0);
	if (fd < 0) {
		SysError (ErrWarn, "UchTextServer::Accept");
		return;
	}
	CreateClient (fd);
}

/*?
This protected function creates a new client on file descriptor \var{fd}.
The client is instantiated by calling the function registered with the
server when it was created (see the constructor).
The new client is registered with the multiplexer, it is added to the client
list, and its \fun{Starting} virtual member is called so that it can
performed its initialization.
?*/
UchTextClient*
UchTextServer :: CreateClient (int fd)
{
	UchTextClient* cl = (*newClient) (this);
	if (! cl)
		return 0;
	cl->UchChannel::Open (fd);

	cl->SetMode (IORead);
	if (Mpx)
		Mpx->Add (*cl);
	
	// link client list	
	Clients.Append (cl);
	
	// tell the client that it is running
	cl->Starting ();
	
	return cl;
}

//---------------- UchTextClient

/*?
The constructor for a new client of server \var{s}.
?*/
UchTextClient :: UchTextClient (UchTextServer* s)
: UchTextStream (),
  MyServer (s)
{
	sendToOut = FALSE;
	out = 0;
}

/*?
The destructor of a client does nothing.
It is protected.
Clients are deleted automatically when nobody references them.
Normally, calling \fun{Close} should delete a client.
?*/
UchTextClient :: ~UchTextClient ()
{
}

/*?
This protected virtual function is called by the server when
the client has been instantiated and successfully registered.
By default it does nothing.
?*/
void
UchTextClient :: Starting ()
{
	// nothing
}

/*?
This protected virtual function is called by \fun{HandleRead} when
an end-of-file is read (the argument is then TRUE) or when an error occured
while reading (the argument is then FALSE).
It calls the server's \fun{Remove} function for this client,
which should result in the destruction of the client.
This virtual function can be redefined in derived classes, but the 
redefinition should call the default implementation.
?*/
void
UchTextClient :: Closing (bool)
{
	MyServer->Remove (this);
}

/*?
This protected virtual function is called by the server when it is closing down.
The default implementation does nothing.
?*/
void
UchTextClient :: Quitting ()
{
	// nothing
}

/*?
This is the implementation of the virtual function \fun{UchTextStream::DoSend}.
If the output of the client has been redirected with \var{SetOutput},
the output buffer is written to this output, or is discarded if no
output channel is defined.
If the output has not been redirected, the output buffer is written
on the client's channel.
?*/
void
UchTextClient :: DoSend ()
{
	if (sendToOut) {
		if (out)
			out->Write (OutBuffer);
		else
			OutBuffer.Flush ();
	} else
		Write (OutBuffer);
}

/*?
This is the implementation of the virtual function \fun{UchTextStream::Quit},
which is called when a \com{Quit} request is received.
It calls the server's \fun{Quit} function.
?*/
void
UchTextClient :: Quit ()
{
	if (MyServer)
		MyServer->Quit ();
}

/*?
This is the implementation of the virtual function \fun{UchTextStream::Close},
which is called when a \com{Close} request is received.
It calls the server's \fun{Remove} function for this client.
?*/
void
UchTextClient :: Close ()
{
	if (MyServer)
		MyServer->Remove (this);
}

/*?
Redirect the output to channel \var{ch}.
If \var{ch} is 0, output is simply discarded.
?*/
void
UchTextClient :: SetOutput (UchChannel* ch)
{
	sendToOut = TRUE;
	out = ch;
}

/*?
Reset the output to the client's channel.
?*/
void
UchTextClient :: ResetOutput ()
{
	out = 0;
	sendToOut = FALSE;
}