summaryrefslogtreecommitdiff
path: root/comm/BusAccess.h
blob: 7a4abed68912f5ef186258c73fa5ef02f5d4bf6a (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
/*
 *	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 "ccu/List.h"
#include "ccu/IdTable.h"
#include "ccu/String.h"
#include "ccu/HashTable.h"
#include "dnn/Event.h"
#include "dnn/Trigger.h"


class UchBusSubscription;
class UchBusTrigger;
class DnnBaseReaction;

class UchBusAccess : public UchDatagram, public UchBaseSignalHandler {
friend	class	UchBusAgent;

protected:
static	int	Version;
	CcuString	Name;
	sword	ListenPort;
	sword	BroadcastPort;
	UchStream*	Server;
	CcuListOf<UchBusAgent>	Agents;
	CcuListOf<UchBusTrigger>	LocalSubscriptions;
	CcuIdTableOf<UchBusTrigger>	IdToSubscription;

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

public:
	DnnTrigger	NewAgents;

		UchBusAccess (const char*, sword);
		~UchBusAccess ();
	void	Subscribe (DnnBaseReaction&, const char*, ...);
	void	Emit (const char*, ...);
};


class UchBusAgent : public UchBufStream {
friend	class	UchBusAccess;
friend	class	UchBusServer;

protected:
	CcuListOf<UchBusSubscription>	RemoteSubscriptions;
	sword	RemoteId;
	UchBusAccess*	MyBus;
	CcuString	Name;

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

		UchBusAgent (int, UchBusAccess*);
		UchBusAgent (const char*, sword, UchBusAccess*);
		~UchBusAgent ();

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

public:
	DnnTrigger	Bye;
	CcuHashedArrayOf<DnnTrigger>	DirectMsg;

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

class UchAgentEvent : public DnnEvent {
public:
static	DnnEventType*	BusNewAgent, *BusAgentBye;

protected:
	UchBusAgent*	Agent;

public:
		UchAgentEvent (UchBusAgent*, DnnEventType*);
		~UchAgentEvent ();


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


class UchDirectEvent : public DnnEvent {
public:
	UchBusAgent*	Agent;
	CcuString	Msg;

public:
		UchDirectEvent (UchBusAgent*, const char*);
		~UchDirectEvent ();
};



class UchBusEvent : public DnnEvent {
public:
	CcuString	Regexp;
	int	NbMatches;
	CcuListOf<CcuString>	MatchList;
	CcuString	Matches;

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


#endif	/* BusAccess_H_ */