aboutsummaryrefslogtreecommitdiff
path: root/src/Ivy.java
blob: 6fce37b04e22d2bfecab3dc84e4b70a172f8803d (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
//
// API de connexion au bus logiciel ivy
//
//

package fr.dgac.ivy ;

import java.net.*;
import java.io.*;
import java.util.Vector;
import java.util.StringTokenizer;

public class Ivy implements Runnable, IvyApplicationListener {

  static private boolean debug = (System.getProperty("IVY_DEBUG")!=null) ;

  public static final int DEFAULT_PORT = 2010 ;
  public static final String DEFAULT_DOMAIN = "127.255.255.255:"+DEFAULT_PORT;
  public String appName;
  private boolean ivyRunning = false;
  private String ready_message = null;
  private Vector regexp_out;
  private ServerSocket app;
  private IvyWatcher watch;
  private Thread server;
  private Vector clients;
  private Vector callbacks;
  private Vector ivyApplicationListenerList;
  private String messages_classes[] = null;
  private int myport;				/* Application port number */
	
  public synchronized boolean  ivyRunning(){
	return ivyRunning;
	}
  // constructeur
  public Ivy( String name, String message, IvyApplicationListener appcb)
  {
    appName = name;
    ivyApplicationListenerList = new Vector();
    ready_message =  message;
    callbacks = new Vector();
    clients = new Vector();
    regexp_out = new Vector();
	if ( appcb != null )
		ivyApplicationListenerList.addElement( appcb );
  }

  void classes( String msg_classes[] ) {
	  messages_classes = msg_classes;
  }
  public Vector getRegexpOut() {
	  return regexp_out; 
  }
  public String getReadyMessage() {
	  return ready_message; 
  }
  
  void addClient( Socket socket ) throws IOException {
    IvyClient client = new IvyClient( this, socket);
    clients.addElement( client );
  }

  void removeClient( IvyClient client ) {
	  clients.removeElement( client ); 
  }

  void callCallback( IvyClient client, int msgid, String msgarg ) {
    IvyMessageListener callback;
    String args[];
    if ( (msgid <0) || (msgid > callbacks.size()) ) {
      traceDebug("(callCallback) Not regexp matching id "+msgid);
      return;
    }
    callback = (IvyMessageListener)callbacks.elementAt(msgid);
    StringTokenizer st = new StringTokenizer(msgarg, IvyClient.EndArg);
    args = new String[st.countTokens()];
    int i=0;
    while  (st.hasMoreTokens()) 
		{ 
		args[i++] = st.nextToken();
		}
	callback.receive( client, args );
  }

  public int getApplicationPort() {
	  return myport;
  }

  public int sendMsg( String message ) {
	int count = 0;
    // envoie le message à tous les clients du bus
	// 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;
  }

  public int bindMsg(String regexp, IvyMessageListener callback ) {
    // associe une nouvelle regexp à un nouveau callback
    regexp_out.addElement( regexp );
    callbacks.addElement( callback );
    int id = regexp_out.indexOf( regexp );
    /* indique aux autres clients la nouvelle regexp */
    for ( int i = 0 ; i < clients.size(); i++ ) {
      IvyClient client = (IvyClient)clients.elementAt(i);
      client.sendRegexp(id, regexp );
    }
	return id;
  }

  public void unBindMsg(int id) {
	  // TODO: Est ce que la suppression d'1 element
	  // ne provoque pas le tassement
	  // et donc la modif des autres indexes?
	 if ( id >=0 && id < regexp_out.size() )
	 {
		regexp_out.removeElementAt( id );
		callbacks.removeElementAt( id );
		    
		for ( int i = 0 ; i < clients.size(); i++ )
		{
		IvyClient client = (IvyClient)clients.elementAt(i);
		client.delRegexp(id );
		}
	 }
  }
	/* gestion des applications listener et callback associe */
  public int addApplicationListener(IvyApplicationListener callback){
  // associe une nouvelle regexp à un nouveau callback
    ivyApplicationListenerList.addElement( callback );
    int id = ivyApplicationListenerList.indexOf( callback );
    return id;
  }
  public void removeApplicationListener( int id ) {
	  ivyApplicationListenerList.removeElementAt(id);
  }
  public void connect(IvyClient client){
	  for ( int i = 0 ; i < ivyApplicationListenerList.size(); i++ )
		{
		IvyApplicationListener listener = (IvyApplicationListener)ivyApplicationListenerList.elementAt(i);
		listener.connect(client);
		}
  }
  public void disconnect(IvyClient client){
  	  for ( int i = 0 ; i < ivyApplicationListenerList.size(); i++ )
		{
		IvyApplicationListener listener = (IvyApplicationListener)ivyApplicationListenerList.elementAt(i);
		listener.disconnect(client);
		}
  }
  public void die(IvyClient client, int id){
  	  for ( int i = 0 ; i < ivyApplicationListenerList.size(); i++ )
		{
		IvyApplicationListener listener = (IvyApplicationListener)ivyApplicationListenerList.elementAt(i);
		listener.die(client, id);
		}
  }
  public void directMessage( IvyClient client, int id,String msgarg ){
  	  for ( int i = 0 ; i < ivyApplicationListenerList.size(); i++ )
		{
		IvyApplicationListener listener = (IvyApplicationListener)ivyApplicationListenerList.elementAt(i);
		listener.directMessage(client,id, msgarg);
		}
  }

  public static String getDomain(String domainbus) throws IvyException {
	if ( domainbus == null )
		 domainbus = System.getProperty("IVYBUS");
	if ( domainbus == null )
		 domainbus = DEFAULT_DOMAIN;
	return domainbus;
  }
  public void start(String domainbus) throws IvyException {
	try {
      app = new ServerSocket(0);
      myport = app.getLocalPort();
    } catch (IOException e) {
      throw new IvyException("ne peut pas ouvrir la socket server" + e );
    }
    /* Start watcher thread */
    watch = new IvyWatcher(this);
    traceDebug("BUS TCP: "+myport);
	ivyRunning = true;
    server = new Thread( this);
	/* Start ivy server thread */
    server.start();
	/* Send ivy helo msg */
	watch.sendStart(getDomain(domainbus));
  }

  public void stop() {
	try {
	ivyRunning = false;
	watch.close(); /* Stop watcher thread */
	app.close(); /* Stop ivy server thread */
	for ( int i = 0 ; i < clients.size(); i++ ) {
		IvyClient client = (IvyClient)clients.elementAt(i);
		client.close("Ivy Stopping...");/* Stop client thread */
	}
	} catch (  IOException e ) {
      traceDebug("ioexception Stop ");
    }
	clients.removeAllElements();
  }
	
  boolean CheckRegexp( String exp ) {
    /* accepte tout par default */
    int i;
    boolean regexp_ok = true;
    if ( exp.startsWith( "^" ) && messages_classes != null ) {
      regexp_ok = false;
      for ( i = 0 ; i < messages_classes.length; i++ ) {
	if ( messages_classes[i].equals( exp.substring(1)) ) return true;
      }
    }
    return regexp_ok;
  }

  public boolean checkConnected( IvyClient clnt ) {
    if ( clnt.appPort == 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;
  }

  public void run() {	
    Socket socket;
    while( ivyRunning() ) {
		try {
		socket = app.accept();
		addClient( socket );
		} catch( IOException e ) {
		/* Normal terminaison si socket close */
		/* TODO: et les autres  */
		traceDebug("Error IvyServer exception:  "  +  e.getMessage());
		}
	}
  }

  private void traceDebug(String s){
    if (debug) System.out.println("-->ivy<-- "+s);
  }

  


} // class Ivy