summaryrefslogtreecommitdiff
path: root/comm/BusAccess.cc
blob: c92a52cb14d93adc91c15b4703bb1399a612126d (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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1997
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	CENA bus access, by Stephane Chatty
 *
 *	$Id$
 *	$CurLog$
 */

#include "BusAccess.h"
#include "Multiplexer.h"
#include "BufStream.h"
#include "ccu/String.h"
#include "dnn/Trigger.h"
#include "dnn/Reaction.h"
#include <stdio.h>
#include <ostream.h>
#include <regex.h>
#include <stdarg.h>

int UchBusAccess::Version = 2;

typedef enum {
	BusBye,		/* quitte l'application ( non utilise ) */
	BusRegexp,		/* expression reguliere d'un client */
	BusMsg,		/* message reel */
	BusError,		/* error message */
	BusDelRegexp,	/* Remove expression reguliere */ 
	BusReady
} BusMsgType;




class UchBusSubscription {
protected:
	int	Id;
	CcuString	Regexp;
	regex_t	Compiled;
public:
		UchBusSubscription (int, const char*);
inline	int	GetId () const	{ return Id; }
inline	const char*	GetRegexp () const	{ return Regexp; }
inline	const regex_t*	GetCompiled () const	{ return &Compiled; }
};

class UchBusTrigger : public DnnTrigger {
protected:
	int	Id;
	CcuString	Regexp;
public:
		UchBusTrigger (const char*);
inline	int	GetId () const	{ return Id; }
inline	void	SetId (int id)		{ Id = id; }
inline	const char*	GetRegexp () const	{ return Regexp; }
};

class UchBusServer : public UchStream {
public:
	UchBusAccess*	MyBus;

		UchBusServer (UchBusAccess*);
	void	HandleRead ();
};

class UchBusAgent : public UchBufStream {
protected:
	CcuListOf<UchBusSubscription>	RemoteSubscriptions;
	UchBusAccess*	MyBus;

	void	WriteString (const char*);
	void	ProcessLine (const char*);

public:
		UchBusAgent (int, UchBusAccess*);
		UchBusAgent (const char*, sword, UchBusAccess*);
	void	HandleRead ();
	void	SendLocalSubscription (const UchBusTrigger*);
	void	SendLocalSubscriptions ();
	void	Emit (const char*);
};

/* this is used for local subscriptions */
UchBusTrigger :: UchBusTrigger (const char* r)
: Id (-1),
  Regexp (r)
{
}

/* this is used for remote subscriptions */
UchBusSubscription :: UchBusSubscription (int id, const char* r)
: Id (id),
  Regexp (r)
{
	int reg = regcomp (&Compiled, r, REG_ICASE|REG_EXTENDED);
	if (reg != 0)
		cerr << "bad regexp " << r << "\n";
}

UchBusServer :: UchBusServer (UchBusAccess* bus)
: UchStream (new UchInetAddress (ANYADDR)),
  MyBus (bus)
{
	if (Listen () < 0) {
		cerr << "cannot listen on bus server\n";
		return;
	}
	SetMode (IORead);
	Add (UchMpx);
}

/* this happens when another agent opens a connection with us */
void
UchBusServer :: HandleRead ()
{
	/* Accept connection */
	int fd = Accept ();
	if (fd < 0) {
		cerr << "cannot accept connection on server\n";
		return;
	}

	/* create agent handle */
	cout << "Someone was there. Hello!\n";
	UchBusAgent* a = new UchBusAgent (fd, MyBus);
	
	/* send subscriptions*/
	a->SendLocalSubscriptions ();
}

UchBusAgent :: UchBusAgent (int fd, UchBusAccess* bus)
: UchBufStream (),
  RemoteSubscriptions (),
  MyBus (bus)
{
	SetMode (IOReadWrite);
	UchChannel::Open (fd);
	Add (UchMpx);
	MyBus->Agents.Append (this);
}


UchBusAgent :: UchBusAgent (const char* host, sword port, UchBusAccess *bus)
: UchBufStream (0, new UchInetAddress (host, port)),
  RemoteSubscriptions (),
  MyBus (bus)
{
	if (!Setup ()) {
		cerr << "cannot set up stream on " << host << ":" << port << "\n";
		return;
	}
	SetMode (IOReadWrite);
	Add (UchMpx);
	MyBus->Agents.Append (this);
}


UchBusAgent :: ~UchBusAgent ()
{
	MyBus->Agents.Remove (this);
	Remove ();
	Close ();
}


/* this had to be redefined because UchChannel adds null character
and we don't want it */
void
UchBusAgent :: WriteString (const char* s)
{
	/* now that this is a BufStream, perhaps we should use Buffer? */
	Write ((byte*) s, strlen (s));
}

void
UchBusAgent :: SendLocalSubscriptions ()
{
	cout << "sending subscriptions\n";
	CcuListIterOf<UchBusTrigger> s = MyBus->LocalSubscriptions;
	while (++s)
		SendLocalSubscription (*s);

	/* say I'm ready */
	char buf[32];
	sprintf (buf, "%d %d\n", BusReady, 0);
	*this << buf;
}

void
UchBusAgent :: SendLocalSubscription (const UchBusTrigger* s)
{
	char buf[32];
	sprintf (buf, "%d %d ", BusRegexp, s->GetId ());
	WriteString (buf);
	WriteString (s->GetRegexp ());
	WriteString ("\n");
	cout << "Sending regexp: " << buf << s->GetRegexp () << '\n';
}


/* this happens when another agent talks */
void
UchBusAgent :: HandleRead ()
{
	/* read in buffer */
	if (InBuffer.BufLength () == 0)
		InBuffer.NeedSize (128);
	int n = Read (InBuffer);

	/* handle errors */
	if (n == -2) {
		InBuffer.Grow ();
		n = Read (InBuffer);
	}
	if (n <= 0) {
		delete this;
		return;
	}

	/* slice input */
	char* start = (char*) InBuffer.Buffer ();
	char* stop = start + InBuffer.BufLength ();
	for (char* p = start; p < stop; p++) {
		if (*p == '\n') {
			*p = '\0';
			ProcessLine (start);
			start = p+1;
			InBuffer.Flush ((byte*) start);
		}
	}
}


#if 0
	byte buf [256];
	int nb_read = Read (buf, 256);
	if (nb_read <= 0) {
		delete this;
		return;
	}
	buf[nb_read] = '\0';
	cout << "agent says:" << (const char*) buf;
}
#endif

void
UchBusAgent :: ProcessLine (const char* line)
{
	/* extract */
	int msg_type, id, nb_chars;
	int nb_fields = sscanf (line,"%d %d%n", &msg_type, &id, &nb_chars);
	if (nb_fields != 2) {
		cerr << "Bad format: " << line;
		return;
	}

	const char* p = (const char*) (line+nb_chars);
	switch (msg_type) {
	case BusBye:
		cout << "agent says goodbye\n";
		delete this;
		break;
	case BusRegexp:
		RemoteSubscriptions.Append (new UchBusSubscription (id, p+1));
		break;
	case BusMsg:
		MyBus->HandleEvent (id, p);
		break;
	case BusDelRegexp:
		cerr << "not implemented: DelRegexp\n";
		break;
	case BusReady:
		break;
	case BusError:
		break;
	}
}

void
UchBusAgent :: Emit (const char* msg)
{
	const int MAX_MATCHING_ARGS = 20;
	regmatch_t pmatch[MAX_MATCHING_ARGS+1];

	/* scan all subscriptions from this agent */
	CcuListIterOf<UchBusSubscription> r = RemoteSubscriptions;
	while (++r) {
		UchBusSubscription* s = *r;
		/* does the message match the regexp? */
		if (regexec (s->GetCompiled (), msg, MAX_MATCHING_ARGS, pmatch, 0) != 0)
			continue;

		cerr << msg << " matches " << s->GetRegexp () << "\n";
		/* if yes, emit it as such */
		char buf[64];
		sprintf (buf, "%d %d", BusMsg, s->GetId ());
		WriteString (buf);
		regmatch_t* p = &pmatch[1];
		while (p->rm_so != -1) {
			char buf[256];
			sprintf (buf, " %.*s", p->rm_eo - p->rm_so, msg + p->rm_so);
			WriteString (buf);
			++p;
		}
		WriteString ("\n");
	}
}


UchBusAccess :: UchBusAccess (sword port)
: UchDatagram (),
  UchBaseSignalHandler (*UchMpx, SigInt),
  ListenPort (0),
  BroadcastPort (port),
  Server (0),
  Agents (),
  IdToSubscription (256),
  LocalSubscriptions ()
{
	/** set up datagram **/
	UchInetAddress* a = new UchInetAddress (ANYADDR, port);
	BindTo (a);
	if (!Open ()) {
		cerr << "Cannot open socket\n";
		return;
	}
	if (!AllowBroadcast ()) {
		cerr << "socket won't broadcast\n";
		return;
	}
	if (!ReuseAddress ()) {
		cerr << "socket won't let address be reused\n";
		return;
	}

	if (Bind () < 0) {
		cerr << "cannot bind socket to port " << port << "\n";
		return;
	}
	SetMode (IOReadWrite);

	/** set up stream **/
	Server = new UchBusServer (this);
	a = dynamic_cast<UchInetAddress*>(Server->BoundTo ());
	if (!a) {
		cerr << "bad address type?!\n";
		return;
	}
	ListenPort = a->Port ();
	
	/** emit handshake on datagram **/
	char handshake[32];
	sprintf (handshake, "%d %hu\n", Version, ListenPort);
	UchInetAddress ba (BROADCAST, BroadcastPort);
	Send ((byte*)handshake, strlen (handshake), ba);

	/* add to main loop */
	Add (UchMpx);
}


UchBusAccess :: ~UchBusAccess ()
{
}

/* this happens when someone sends a broadcast on the bus: a newcomer */
void
UchBusAccess :: HandleRead ()
{
	/* read data */
	byte buf [256];
	int nb_bytes = Receive (buf, 256);
	if (nb_bytes < 0) {
		cerr << "read error on broadcast port\n";
		return;
	}

	/* decode data: should be a handshake */
	int bus_version;
	sword remote_listen_port;
	int nb_infos = sscanf ((char*) buf, "%d %hu", &bus_version, &remote_listen_port);
	if (nb_infos < 2) {
		cerr << "bad data on broadcast port\n";
		return;
	}

	/* where did this come from? */
	UchInetAddress* a = dynamic_cast<UchInetAddress*> (From ());
	 if (!a) {
		cerr << "bad address!?\n";
		return;
	 }
	const char* remote_host = a->GetHostName ();
	sword remote_port = a->Port ();

	/* sanity checks: is it the same protocol version? is it myself calling? */
	if (remote_listen_port == ListenPort)
		return;
	if (bus_version != Version) {
		cerr << "bad protocol version\n";
		return;
	}

	printf ("Arrivee de %s:%hu port %hu\n", remote_host, remote_port, remote_listen_port );

	/* Create agent handle */
	UchBusAgent* ag = new UchBusAgent (remote_host, remote_listen_port, this);

	/* send subscriptions */
	ag->SendLocalSubscriptions ();
}


void
UchBusAccess :: HandleEvent (int id, const char* msg)
{
	UchBusTrigger* s = IdToSubscription.Get (id);
	if (!s) {
		cerr << "bad regexp id " << id << "\n";
		return;
	}
	cout << "REGEXP " << s->GetId () << " MATCHED: " << msg << "\n";
}

/* this is the handler for SIGINT */
void
UchBusAccess :: DeferredHandle (int)
{
	cerr << "quitting\n";
	UchStop ();
}

void
UchBusAccess :: Subscribe (DnnBaseReaction& r, const char* regexp)
{
	/* store new subscription */
	UchBusTrigger* s = new UchBusTrigger (regexp);
	LocalSubscriptions.Append (s);
	int id = IdToSubscription.Store (s);
	s->SetId (id);

	/* emit to current remote agents */
	CcuListIterOf<UchBusAgent> a = Agents;
	while (++a)
		(*a)->SendLocalSubscription (s);

	/* link to reaction */
	r.SubscribeTo (*s);
}


void
UchBusAccess :: Emit (const char* fmt, ...)
{
	char message[4096];
	va_list ap;
	
	va_start (ap, fmt);
	vsprintf (message, fmt, ap);
	va_end (ap );

	CcuListIterOf <UchBusAgent> a = Agents;
	while (++a)
		(*a)->Emit (message);
}