From 750f33265d208df8218f85359e3f027900c58363 Mon Sep 17 00:00:00 2001 From: jestin Date: Fri, 22 Jul 2011 16:49:57 +0000 Subject: Passage en 1.2.14 --- src/SelfIvyClient.java | 113 +++++++++++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 41 deletions(-) (limited to 'src/SelfIvyClient.java') diff --git a/src/SelfIvyClient.java b/src/SelfIvyClient.java index f4a1cd2..a87c859 100644 --- a/src/SelfIvyClient.java +++ b/src/SelfIvyClient.java @@ -6,6 +6,10 @@ * @since 1.2.4 * * CHANGELOG: + * 1.2.14 + * - uses autoboxing for Boolean + * - switch from gnu regexp (deprecated) to the built in java regexp + * - add generic types to declarations * 1.2.7: * - fixes a bug on unbindMsg(String) ( closes Matthieu's burreport ) * 1.2.6: @@ -19,15 +23,14 @@ package fr.dgac.ivy ; import java.util.*; -import org.apache.regexp.*; +import java.util.regex.*; -class SelfIvyClient extends IvyClient { +public class SelfIvyClient extends IvyClient { private Ivy bus; private static int serial=0; /* an unique ID for each regexp */ - private Hashtable callbacks=new Hashtable(); - private Hashtable threadedFlag=new Hashtable(); - private boolean massThreaded=false; + private Hashtable callbacks=new Hashtable(); + private Hashtable threadedFlag=new Hashtable(); public void sendDirectMsg(int id,String message) { bus.directMessage(this,id,message); @@ -40,23 +43,23 @@ class SelfIvyClient extends IvyClient { this.appName=appName; } - synchronized protected int bindMsg(String sregexp, IvyMessageListener callback, boolean threaded ) throws IvyException { + synchronized protected int bindMsg(String sregexp, IvyMessageListener callback, BindType type ) throws IvyException { // creates a new binding (regexp,callback) try { - RE re=new RE(sregexp); - Integer key = new Integer(serial++); + Pattern re=Pattern.compile(sregexp,Pattern.DOTALL); + Integer key = serial++; regexps.put(key,re); regexpsText.put(key,sregexp); callbacks.put(key,callback); - threadedFlag.put(key,new Boolean(threaded)); + threadedFlag.put(key,type); // use autoboxing of boolean return key.intValue(); - } catch (RESyntaxException ree) { + } catch (PatternSyntaxException ree) { throw new IvyException("Invalid regexp " + sregexp); } } synchronized protected void unBindMsg(int id) throws IvyException { - Integer key = new Integer(id); + Integer key = id; if ( ( regexps.remove(key) == null ) || (regexpsText.remove(key) == null ) || (callbacks.remove(key) == null ) @@ -68,9 +71,8 @@ class SelfIvyClient extends IvyClient { // unbinds to the first regexp synchronized protected boolean unBindMsg(String re) { if (!regexpsText.contains(re)) return false; - for (Enumeration e=regexpsText.keys();e.hasMoreElements();) { - Integer k = (Integer)e.nextElement(); - if ( ((String)regexpsText.get(k)).compareTo(re) == 0) { + for (Integer k : regexpsText.keySet()) { + if ( (regexpsText.get(k)).compareTo(re) == 0) { try { bus.unBindMsg(k.intValue()); } catch (IvyException ie) { @@ -84,51 +86,80 @@ class SelfIvyClient extends IvyClient { protected int sendSelfMsg(String message) { int count = 0; - for (Enumeration e = regexps.keys();e.hasMoreElements();) { - Integer key = (Integer)e.nextElement(); - RE regexp = (RE)regexps.get(key); - String sre = (String)regexpsText.get(key); + traceDebug("trying to send to self the message <"+message+">"); + for (Integer key : regexps.keySet() ) { + Pattern regexp = regexps.get(key); + String sre = regexpsText.get(key); synchronized(regexp) { - if (!regexp.match(message)) continue; + traceDebug("checking against: "+sre); + Matcher m = regexp.matcher(message); + if (!m.matches()) { + traceDebug("checking against: "+sre+" failed"); + continue; + } + traceDebug("checking against: "+sre+" succeeded"); count++; - callCallback(this,key,toArgs(regexp)); + callCallback(this,key,toArgs(m)); } } return count; } protected void callCallback(IvyClient client, Integer key, String[] tab) { - IvyMessageListener callback=(IvyMessageListener)callbacks.get(key); + IvyMessageListener callback=callbacks.get(key); if (callback==null) { traceDebug("Not regexp matching id "+key.intValue()+", it must have been unsubscribed concurrently"); return; - // TODO check that nasty synchro issue, test suite: Request - } - Boolean b = (Boolean)threadedFlag.get(key); - if (callback==null) { - System.out.println("threadedFlag.get returns null for"+key.intValue()+", it must have been unsubscribed concurrently"); - return; + // DONE check that nasty synchro issue, test suite: Request } - boolean threaded=b.booleanValue(); - if (!threaded) { - // runs the callback in the same thread - callback.receive(client, tab); // TODO tab can be faulty ?! - } else { - // starts a new Thread for each callback ... ( Async API ) - new Runner(callback,client,tab); + BindType type = threadedFlag.get(key); + switch (type) { + case NORMAL: + // runs the callback in the same thread + callback.receive(client, tab); // can tab can be faulty ?! TODO + break; + case ASYNC: + // starts a new Thread for each callback ... ( Async API ) + new Runner(callback,client,tab); + break; + case SWING: + // deferes the callback to the Event Dispatch Thread + new SwingRunner(callback,client,tab); + break; } } - private String[] toArgs(RE re) { - String[] args = new String[re.getParenCount()-1]; - for(int sub=1;sub0) ? new String[m.groupCount()] : new String[0]; + //System.out.println("DEBUG "+args.length+" arguments"); + for(int sub=0;subSelfIvyClient "+bus.appName+":"+appName+"<-- "+s); + System.out.println("-->SelfIvyClient "+bus.getAppName()+":"+appName+"<-- "+s); } } -- cgit v1.1