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
|
/*
* The Unix Channel
*
* by Michel Beaudouin-Lafon
*
* Copyright 1990-1993
* Laboratoire de Recherche en Informatique (LRI)
*
* Client side: services
*
* $Id$
* $CurLog$
*/
#include "Service.h"
/*?class UchService
An object of class \typ{UchService} (derived from \typ{UchMsgStream}) exists in a client process
to represent the server it is connected to.
The class \typ{UchService} is virtual: you must create subclasses that redefine
at least \fun{NewMessage} from class \fun{UchMsgStream}, and if necessary \fun{ConvertAnswer}.
\fun{NewMessage} should decipher the incoming message, transform it into an event,
and put in into the event queue with \fun{PutEvent}.
It can also handle non event messages (for instance errors).
?*/
/*?class UchEvtMsgQueue
An event queue is a linked list of events.
Events are normally appended to the end of the queue and extracted from the beginning.
?*/
/*?nodoc?*/
UchEvtMsgQueue :: UchEvtMsgQueue ()
: CcuSmartData (),
Queue ()
{
}
/*?nodoc?*/
UchEvtMsgQueue :: ~UchEvtMsgQueue ()
{
}
#ifdef DOC
/*?
Append an event to the queue.
?*/
void
UchEvtMsgQueue :: Put (UchEventMsg* msg)
{
}
/*?
Put an event back in the queue (therefore it becomes the first event of the queue).
?*/
void
UchEvtMsgQueue :: PutBack (UchEventMsg* msg)
{
}
/*?
Return the first event from the queue and remove it.
?*/
UchEventMsg*
UchEvtMsgQueue :: Get ()
{
}
/*?
Return the first event from the queue without removing it.
?*/
UchEventMsg*
UchEvtMsgQueue :: Peek ()
{
}
#endif /* DOC */
/*?
Construct an empty service.
?*/
UchService :: UchService ()
: UchMsgStream (),
EvQueue (0)
{
}
/*?
Construct a service connected to address \var{a}.
?*/
UchService :: UchService (UchAddress* a)
: UchMsgStream (0, a),
EvQueue (0)
{
}
/*?nodoc?*/
UchService :: UchService (const UchService& s)
: UchMsgStream (s),
EvQueue (s.EvQueue)
{
}
/*?nodoc?*/
UchService :: ~UchService ()
{
EvQueue = 0; // deletes it
}
/*?nodoc?*/
UchChannel*
UchService :: Copy () const
{
return new UchService (*this);
}
/*?
Set the event queue to be used to store the incoming events.
If not set, a default event queue is created.
This function is intended to share a queue between several servers.
?*/
void
UchService :: SetEvQueue (UchEvtMsgQueue* evq)
{
EvQueue = evq;
}
/*?nextdoc?*/
UchEventMsg*
UchService :: PeekEvent (bool wait)
{
Flush ();
if (!EvQueue)
EvQueue = new UchEvtMsgQueue;
UchEventMsg* ev = EvQueue->Peek ();
if (ev || ! wait)
return ev;
while (! ev) {
HandleRead ();
ev = EvQueue->Peek ();
}
return ev;
}
/*?
These functions flush the output buffer, and check the events already in the queue.
If there is at least one, it is returned;
\fun{GetEvent} also removes it from the event queue.
If the event queue is empty and \var{wait} is TRUE, the function blocks until an event arrives,
else it returns 0 without blocking.
These functions also create the event queue if it was not set with \fun{SetEvQueue}.
?*/
UchEventMsg*
UchService :: GetEvent (bool wait)
{
Flush ();
if (!EvQueue)
EvQueue = new UchEvtMsgQueue;
UchEventMsg* ev = EvQueue->Get ();
if (ev || ! wait)
return ev;
while (! ev) {
HandleRead ();
ev = EvQueue->Get ();
}
return ev;
}
/*?nextdoc?*/
void
UchService :: PutEvent (UchEventMsg* ev)
{
if (! EvQueue)
EvQueue = new UchEvtMsgQueue;
ev->From = this;
EvQueue->Put (ev);
}
/*?
These functions are similar to the functions \fun{Put} and \fun{PutBack}
on the event queue of the server.
They create the event queue if it was not set with \fun{SetEvQueue},
and set the event's server.
?*/
void
UchService :: PutBackEvent (UchEventMsg* ev)
{
if (! EvQueue)
EvQueue = new UchEvtMsgQueue;
ev->From = this;
EvQueue->PutBack (ev);
}
/*?class UchEventMsg
This class derives from \typ{UchMessage}, so it inherits the usual virtual functions
\fun{ReadFrom} and \fun{WriteTo} that must be redefined in each derived class.
An event is created when a client receives an asynchronous message from its server.
Events are linked together in event queues.
Events must derive from this class.
?*/
#ifdef DOC
// fake entries for inline functions
/*?
Construct an event.
?*/
UchEventMsg :: UchEventMsg ()
{
}
#endif /* DOC */
/*?nodoc?*/
UchEventMsg :: ~UchEventMsg ()
{
}
#ifdef DOC
/*?
Return the server that sent this event.
The service is known only if the event was appended to the event queue with \fun{UchService::PutEvent},
else it is 0.
?*/
UchService*
UchEventMsg :: GetService ()
{
}
#endif /* DOC */
/*?class UchGenEvtMsg
This is a sample derived class of \typ{UchEventMsg}.
It defines events that contain a pointer to a \typ{UchMessage}.
This message must be allocated dynamically because it is deleted by the destructor.
The virtual functions \fun{ReadFrom} and \fun{WriteTo} are defined to act upon the message stored in the event.
The following example fetches a word from the input buffer,
creates a message depending on its value
(\typ{FOO_MSG} and \typ{BAR_MSG} have been derived from \typ{UchMessage}),
and transfers the data from the buffer to the event with \fun{Get}.
This piece of code typically appears in the body
of \fun{NewMessage}:
\begin{ccode}
UchGenEvtMsg* ev = new UchGenEvtMsg;
sword type;
if (! buf.Peek (&type))
return;
switch (type) {
case Foo :
ev->SetMsg (new FOO_MSG);
break;
case Bar :
ev->SetMsg (new BAR_MSG);
break;
...
}
if (!buf.Get (ev))
// protocol error
PutEvent (ev);
\end{ccode}
?*/
#ifdef DOC
/*?nextdoc?*/
UchGenEvtMsg :: UchGenEvtMsg ()
{ }
/*?
Construct a generic event. The second constructor sets its message.
The message is deleted when the event is destroyed.
Thus the message must have been allocated dynamically.
?*/
UchGenEvtMsg :: UchGenEvtMsg (UchMessage* m)
{ }
#endif /* DOC */
/*?nodoc?*/
UchGenEvtMsg :: ~UchGenEvtMsg ()
{
if (Msg)
delete Msg;
}
#ifdef DOC
/*?nextdoc?*/
void
UchGenEvtMsg :: SetMsg (UchMessage* m)
{ }
/*?
Set and get the message associated to the event.
When setting the value, the previous message of the event (if any) is deleted.
?*/
UchMessage*
UchGenEvtMsg :: GetMsg ()
{ }
#endif /* DOC */
/*?nodoc?*/
void
UchGenEvtMsg :: ReadFrom (UchMsgBuffer& buf, lword l)
{
if (Msg)
Msg->ReadFrom (buf, l);
}
/*?nodoc?*/
void
UchGenEvtMsg :: WriteTo (UchMsgBuffer& buf)
{
if (Msg)
Msg->WriteTo (buf);
}
|