// // 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;iivy<-- "+s); } } // class Ivy