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
|
/**
* a software bus package
*
* @author François-Régis Colin
* @author Yannick Jestin
* @author <a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
*
*/
package fr.dgac.ivy ;
import java.net.*;
import java.io.*;
import java.util.Vector;
import java.util.Hashtable;
import java.util.StringTokenizer;
/**
* A class connecting to the Ivy software bus.
* For example:
*<pre>
*Ivy bus = new Ivy("Dummy agent","ready",null);
*bus.bindMsg("(.*)",myMessageListener);
*bus.start(null);
*</pre>
*/
public class Ivy implements Runnable, IvyApplicationListener {
/**
* the name of the application on the bus
*/
public String appName;
/**
* the protocol version number
*/
public static final int PROCOCOLVERSION = 3 ;
/**
* the port for the UDP rendez vous, if none is supplied
*/
public static final int DEFAULT_PORT = 2010 ;
/**
* the domain for the UDP rendez vous
*/
public static final String DEFAULT_DOMAIN = "127.255.255.255:"+DEFAULT_PORT;
private static boolean debug = (System.getProperty("IVY_DEBUG")!=null) ;
private static int serial=0; /* an unique ID for each regexps */
private ServerSocket app;
private IvyWatcher watch;
private Thread server;
private Hashtable callbacks = new Hashtable();
private Vector clients = new Vector();
private Vector ivyApplicationListenerList = new Vector();
private String messages_classes[] = null;
int applicationPort; /* Application port number */
boolean ivyRunning = false;
Hashtable regexp_out = new Hashtable();
String ready_message = null;
/**
* Readies the structures for the software bus connexion.
*
* All the dirty work is done un the start() method
* @see #start
* @param name The name of your Ivy agent on the software bus
* @param message The hellow message you will send once ready
* @param appcb A callback handling the notification of connexions and
* disconnections, may be null
*/
public Ivy( String name, String message, IvyApplicationListener appcb) {
appName = name;
ready_message = message;
if ( appcb != null ) ivyApplicationListenerList.addElement( appcb );
}
/**
* connects the Ivy bus to a domain or list of domains
* @param domainbus a domain of the form 10.0.0:1234, it is similar to the
* netmask without the trailing .255. This will determine the meeting point
* of the different applications. Right now, this is done with an UDP
* broadcast. Beware of routing problems ! You can also use a comma
* separated list of domains.
*
*/
public void start(String domainbus) throws IvyException {
try {
app = new ServerSocket(0);
applicationPort = app.getLocalPort();
} catch (IOException e) {
throw new IvyException("can't open TCP service socket " + e );
}
traceDebug("TCP service open on port "+applicationPort);
watch = new IvyWatcher(this);
ivyRunning = true;
server = new Thread(this);
server.start();
watch.start(getDomain(domainbus));
}
/**
* disconnects from the Ivy bus.
*/
public void stop() {
try {
ivyRunning = false;
watch.stop();
app.close();
for ( int i = 0 ; i < clients.size(); i++ ) {
IvyClient client = (IvyClient)clients.elementAt(i);
client.close("normal Ivy Stopping...");
// advertise that this is a normal
//close for debugging purposes
}
} catch (IOException e) {
traceDebug("IOexception Stop ");
}
clients.removeAllElements();
}
/**
* Performs a pattern matching according to everyone's regexps, and sends
* the results to the relevant ivy agents.
* <p><em>There is one thread for each client connected, we could also
* create another thread each time we send a message.</em>
* @param message A String which will be compared to the regular
* expressions of the different clients
* @return the number of messages actually sent
*/
public int sendMsg( String message ) {
int count = 0;
// TODO: il faudrait mettre une Thread emission par client */
for ( int i = 0 ; i < clients.size(); i++ ) {
IvyClient client = (IvyClient)clients.elementAt(i);
count += client.sendMsg( message );
}
return count;
}
/**
* Subscribes to a regular expression.
*
* The callback will be executed with
* the saved parameters of the regexp as arguments when a message will sent
* by another agent. A program <em>doesn't</em> receive its own messages.
* <p>Example:
* <br>the Ivy agent A performs <pre>b.bindMsg("^Hello (*)",cb);</pre>
* <br>the Ivy agent B performs <pre>b2.sendMsg("Hello world");</pre>
* <br>a thread in A will uun the callback cb with its second argument set
* to a array of String, with one single element, "world"
* @param regexp a perl regular expression, groups are done with parenthesis
* @param callback any objects implementing the IvyMessageListener
* interface, on the AWT/Swing framework
* @return the id of the regular expression
*/
public int bindMsg(String regexp, IvyMessageListener callback ) {
// creates a new binding (regexp,callback)
Integer key = new Integer(serial++);
regexp_out.put(key,regexp);
callbacks.put(key,callback );
// notifies the other clients this new regexp
for (int i=0;i<clients.size();i++){
((IvyClient)clients.elementAt(i)).sendRegexp(key.intValue(),regexp);
}
return key.intValue();
}
/**
* unsubscribes a regular expression
*
* @param id the id of the regular expression, returned when it was bound
*/
public void unBindMsg(int id) throws IvyException {
Integer key = new Integer(id);
if ( ( regexp_out.remove(key) == null )
|| (callbacks.remove(key) == null ) ) {
throw new IvyException("client wants to remove an unexistant regexp "+id);
}
for (int i=0;i<clients.size();i++ ) {
((IvyClient)clients.elementAt(i)).delRegexp(id );
}
}
/**
* adds an application listener to a bus
* @param callback is an object implementing the IvyApplicationListener
* interface
* @return the id of the application listener, useful if you wish to remove
* it later
*/
public int addApplicationListener(IvyApplicationListener callback){
ivyApplicationListenerList.addElement(callback);
int id = ivyApplicationListenerList.indexOf( callback );
return id;
}
/**
* removes an application listener
* @param id the id of the application listener to remove
*/
public void removeApplicationListener(int id){
ivyApplicationListenerList.removeElementAt(id);
}
/* invokes the application listeners upon arrival of a new Ivy client
* it *might* be considered poor style to invoke them as the same level
* as the others applicationListeners. This is part of the interface
*/
public void connect(IvyClient client){
for ( int i = 0 ; i < ivyApplicationListenerList.size(); i++ ) {
((IvyApplicationListener)ivyApplicationListenerList.elementAt(i)).connect(client);
}
}
/* invokes the application listeners upon departure of an Ivy client
* ibid.
*/
public void disconnect(IvyClient client){
for ( int i = 0 ; i < ivyApplicationListenerList.size(); i++ ) {
((IvyApplicationListener)ivyApplicationListenerList.elementAt(i)).disconnect(client);
}
}
/* invokes the application listeners upon death of an Ivy client
* ibid
*/
public void die(IvyClient client, int id){
for ( int i = 0 ; i < ivyApplicationListenerList.size(); i++ ) {
((IvyApplicationListener)ivyApplicationListenerList.elementAt(i)).die(client, id);
}
}
/* invokes the direct message callbacks
* ibid
*/
public void directMessage( IvyClient client, int id,String msgarg ){
for ( int i = 0 ; i < ivyApplicationListenerList.size(); i++ ) {
((IvyApplicationListener)ivyApplicationListenerList.elementAt(i)).directMessage(client,id, msgarg);
}
}
/////////////////////////////////////////////////////////////////:
//
// Protected methods
//
/////////////////////////////////////////////////////////////////:
void addClient(Socket socket,boolean peerCalling) throws IOException {
IvyClient client = new IvyClient(this, socket,peerCalling);
clients.addElement(client);
}
void removeClient( IvyClient client ) {
clients.removeElement( client );
}
void callCallback(IvyClient client, Integer key, String msgarg) throws IvyException {
IvyMessageListener callback=(IvyMessageListener)callbacks.get(key);
if (callback==null){
throw new IvyException("(callCallback) Not regexp matching id "+key.intValue());
}
StringTokenizer st = new StringTokenizer(msgarg,IvyClient.EndArg);
String args[] = new String[st.countTokens()];
int i=0;
while (st.hasMoreTokens()){ args[i++] = st.nextToken();}
callback.receive( client, args );
}
static String getDomain(String domainbus) throws IvyException {
if ( domainbus == null ) domainbus = System.getProperty("IVYBUS");
if ( domainbus == null ) domainbus = DEFAULT_DOMAIN;
return domainbus;
}
/*
* checks the "validity" of a regular expression.
* TODO i'm not sure this is still used by anything.
*/
boolean CheckRegexp( String exp ) {
boolean regexp_ok = true;
if ( exp.startsWith( "^" )&&messages_classes!=null) {
regexp_ok=false;
for (int i=0 ; i < messages_classes.length;i++) {
if (messages_classes[i].equals(exp.substring(1))) return true;
}
}
return regexp_ok;
}
/*
* TODO prevents two clients from connecting to each other at the same time
* there is still a lingering bug here, that we could avoid with the
* SchizoToken.
*/
boolean checkConnected( IvyClient clnt ) {
if ( clnt.getAppPort() == 0 ) return false;
for ( int i = 0 ; i < clients.size(); i++ ) {
IvyClient client = (IvyClient)clients.elementAt(i);
if ( clnt != client && client.sameClient( clnt ) ) return true;
}
return false;
}
/*
* the service socket thread reader.
*/
public void run() {
while(ivyRunning){
try {
Socket socket = app.accept();
addClient(socket,true); // the peer called me
} catch( IOException e ) {
/* TODO is it a normal termination on socket close ? */
traceDebug("Error IvyServer exception: " + e.getMessage());
System.out.println("DEBUG TCP socket reader caught an exception " + e.getMessage());
}
}
traceDebug("IvyServer end of trans");
}
/////////////////////////////////////////////////////////////////:
//
// Private methods
//
/////////////////////////////////////////////////////////////////:
private void traceDebug(String s){
if (debug) System.out.println("-->ivy<-- "+s);
}
// TODO find out if this is useful or not ...
private void classes( String msg_classes[] ) {
messages_classes = msg_classes;
}
}
|