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

package fr.dgac.ivy ;

import java.net.*;
import java.io.*;
import java.util.Vector;
import java.util.Hashtable;
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 ServerSocket app;
  private IvyWatcher watch;
  private Thread server;

  private Hashtable regexp_out = new Hashtable();
  private Hashtable callbacks = new Hashtable();
  private Vector clients = new Vector();
  private Vector ivyApplicationListenerList = new Vector();
  private String messages_classes[] = null;
  private int myport;				/* Application port number */
	
  public synchronized boolean  ivyRunning(){ return ivyRunning; }

  public Ivy( String name, String message, IvyApplicationListener appcb) {
    appName = name;
    ready_message =  message;
    if ( appcb != null ) ivyApplicationListenerList.addElement( appcb );
  }

  void classes( String msg_classes[] ) { messages_classes = msg_classes; }

  public Hashtable 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, Integer key, String msgarg) {
    IvyMessageListener callback=(IvyMessageListener)callbacks.get(key);
    if (callback==null){
      traceDebug("(callCallback) Not regexp matching id "+key.intValue());
      return;
    }
    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 );
  }

  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;
  }

  static private int serial=0;
  public int bindMsg(String regexp, IvyMessageListener callback ) {
    // associe une nouvelle regexp à un nouveau callback
    Integer key = new Integer(serial++);
    regexp_out.put(key,regexp);
    callbacks.put(key,callback );
    // indique aux autres clients la nouvelle regexp
    for (int i=0;i<clients.size();i++){
      ((IvyClient)clients.elementAt(i)).sendRegexp(key.intValue(),regexp);
    }
    return key.intValue();
  }

  public void unBindMsg(int id) {
    Integer key = new Integer(id);
    if ( ( regexp_out.remove(key) == null )
         || (callbacks.remove(key) == null ) ) {
      System.err.println("attention j'enlève une regexp inexistante");
    }
    for (int i=0;i<clients.size();i++ ) {
	    ((IvyClient)clients.elementAt(i)).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 */
    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;
  }

  public 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;
  }

  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