summaryrefslogtreecommitdiff
path: root/comm/BusAccess.h
blob: 3d45db82b28b704f5b4fa58015498a875119c38c (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
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1997
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Bus access, by Stephane Chatty
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef BusAccess_H_
#define BusAccess_H_

#include "Datagram.h"
#include "Stream.h"
#include "BufStream.h"
#include "SignalHandler.h"
#include "ivl/List.h"
#include "ivl/IdTable.h"
#include "ivl/String.h"
#include "ivl/HashTable.h"
#include "ivl/Event.h"
#include "ivl/Trigger.h"

class IvlBusSubscription;
class IvlBusTrigger;
class IvlBaseReaction;

class IvlBusAccess : public IvlDatagram, public IvlBaseScheduledHandler {
friend	class	IvlBusAgent;

protected:
static	int	Version;
static	const char* const	DefaultBus;
static	const char* const	DefaultBusNet;
static	sword const	DefaultBusPort;
	IvlString	Name;
	sword	ListenPort;
	sword	BroadcastPort;
	IvlStream*	Server;
	IvlListOf<IvlBusAgent>	Agents;
	IvlListOf<IvlBusTrigger>	LocalSubscriptions;
	IvlIdTableOf<IvlBusTrigger>	IdToSubscription;

	void	HandleRead ();
	void	Clear ();
	void	DeferredHandle (int);
	void	HandleEvent (int, const char*);
	bool	CheckUnicity (IvlBusAgent*, sword);

public:
	IvlTrigger	NewAgents;

		IvlBusAccess (const char*, const char* = 0);
		~IvlBusAccess ();
	void	Subscribe (IvlBaseReaction&, const char*, ...);
	void	Emit (const char*, ...);
};

class IvlBusAgent : public IvlBufStream {
friend	class	IvlBusAccess;
friend	class	IvlBusServer;

protected:
	IvlListOf<IvlBusSubscription>	RemoteSubscriptions;
	sword	RemoteId;
	IvlBusAccess*	MyBus;
	IvlString	Name;

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

		IvlBusAgent (int, IvlBusAccess*);
		IvlBusAgent (const char*, sword, IvlBusAccess*);
		~IvlBusAgent ();

	void	HandleRead ();
	void	SendLocalSubscription (const IvlBusTrigger*);
	void	SayHello ();
	void	SayBye ();
	void	EmitEvent (const char*);

public:
	IvlTrigger	Bye;
	IvlHashedArrayOf<IvlTrigger>	DirectMsg;

	void	Emit (int, const char*, ...);
inline	const char*	GetName () const	{ return Name; }
};

class IvlAgentEvent : public IvlEvent {
public:
static	IvlEventType*	BusNewAgent, *BusAgentBye;

protected:
	IvlBusAgent*	Agent;

public:
		IvlAgentEvent (IvlBusAgent*, IvlEventType*);
		~IvlAgentEvent ();

inline	IvlBusAgent*	GetAgent () const	{ return Agent;}
};

class IvlDirectEvent : public IvlEvent {
public:
	IvlBusAgent*	Agent;
	IvlString	Msg;

public:
		IvlDirectEvent (IvlBusAgent*, const char*);
		~IvlDirectEvent ();
};

class IvlBusEvent : public IvlEvent {
public:
	IvlString	Regexp;
	int	NbMatches;
	IvlListOf<IvlString>	MatchList;
	IvlString	Matches;

		IvlBusEvent (const char*, const char*);
		~IvlBusEvent ();
};

#endif	/* BusAccess_H_ */