diff options
Diffstat (limited to 'src/Ivy.java')
-rwxr-xr-x | src/Ivy.java | 114 |
1 files changed, 45 insertions, 69 deletions
diff --git a/src/Ivy.java b/src/Ivy.java index 6fce37b..8756c94 100755 --- a/src/Ivy.java +++ b/src/Ivy.java @@ -8,6 +8,7 @@ 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 { @@ -19,41 +20,30 @@ public class Ivy implements Runnable, IvyApplicationListener { 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 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; - } - // constructeur - public Ivy( String name, String message, IvyApplicationListener appcb) - { + public synchronized boolean ivyRunning(){ return ivyRunning; } + + 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 ); + 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 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); @@ -64,27 +54,20 @@ public class Ivy implements Runnable, IvyApplicationListener { 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); + 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; } - callback = (IvyMessageListener)callbacks.elementAt(msgid); - StringTokenizer st = new StringTokenizer(msgarg, IvyClient.EndArg); - args = new String[st.countTokens()]; + 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 ); + while (st.hasMoreTokens()){ args[i++] = st.nextToken();} + callback.receive( client, args ); } - public int getApplicationPort() { - return myport; - } + public int getApplicationPort() { return myport; } public int sendMsg( String message ) { int count = 0; @@ -97,34 +80,28 @@ public class Ivy implements Runnable, IvyApplicationListener { return count; } + static private int serial=0; 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 ); + 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 id; + return key.intValue(); } 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 ); - } - } + 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){ @@ -207,19 +184,18 @@ public class Ivy implements Runnable, IvyApplicationListener { 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; + 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.appPort == 0 ) return false; + 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; |