diff options
author | jestin | 2012-04-26 15:26:33 +0000 |
---|---|---|
committer | jestin | 2012-04-26 15:26:33 +0000 |
commit | ac8c6c0d9bb921166697e9b13009f9ff83ba9716 (patch) | |
tree | 1661b2885aa4d4be2f3cf26deda479641d6f6630 /src/SelfIvyClient.java | |
parent | 934dfab1b2e6571f241facfcadc733d25a411024 (diff) | |
download | ivy-java-ac8c6c0d9bb921166697e9b13009f9ff83ba9716.zip ivy-java-ac8c6c0d9bb921166697e9b13009f9ff83ba9716.tar.gz ivy-java-ac8c6c0d9bb921166697e9b13009f9ff83ba9716.tar.bz2 ivy-java-ac8c6c0d9bb921166697e9b13009f9ff83ba9716.tar.xz |
Goes to a set of synchronized collections (Map, HashMap, ...) to try and avoid
multithreaded issues ...
Diffstat (limited to 'src/SelfIvyClient.java')
-rw-r--r-- | src/SelfIvyClient.java | 63 |
1 files changed, 40 insertions, 23 deletions
diff --git a/src/SelfIvyClient.java b/src/SelfIvyClient.java index a87c859..3e9ba6a 100644 --- a/src/SelfIvyClient.java +++ b/src/SelfIvyClient.java @@ -6,6 +6,8 @@ * @since 1.2.4 * * CHANGELOG: + * 1.2.16 + * - now uses the synchronized wrappers of the Java API for all collections * 1.2.14 * - uses autoboxing for Boolean * - switch from gnu regexp (deprecated) to the built in java regexp @@ -22,15 +24,19 @@ */ package fr.dgac.ivy ; -import java.util.*; +import java.util.Map; +import java.util.HashMap; +import java.util.Collections; import java.util.regex.*; public class SelfIvyClient extends IvyClient { private Ivy bus; private static int serial=0; /* an unique ID for each regexp */ - private Hashtable<Integer,IvyMessageListener> callbacks=new Hashtable<Integer,IvyMessageListener>(); - private Hashtable<Integer,BindType> threadedFlag=new Hashtable<Integer,BindType>(); + private Map<Integer,IvyMessageListener> callbacks= + Collections.synchronizedMap(new HashMap<Integer,IvyMessageListener>()); + private Map<Integer,BindType> threadedFlag= + Collections.synchronizedMap(new HashMap<Integer,BindType>()); public void sendDirectMsg(int id,String message) { bus.directMessage(this,id,message); @@ -43,42 +49,50 @@ public class SelfIvyClient extends IvyClient { this.appName=appName; } - synchronized protected int bindMsg(String sregexp, IvyMessageListener callback, BindType type ) throws IvyException { + protected int bindMsg(String sregexp, IvyMessageListener callback, BindType type ) throws IvyException { // creates a new binding (regexp,callback) try { 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,type); // use autoboxing of boolean + synchronized (callbacks) { + callbacks.put(key,callback); + } + synchronized (threadedFlag) { + threadedFlag.put(key,type); // use autoboxing of boolean + } return key.intValue(); } catch (PatternSyntaxException ree) { throw new IvyException("Invalid regexp " + sregexp); } } - synchronized protected void unBindMsg(int id) throws IvyException { + protected synchronized void unBindMsg(int id) throws IvyException { Integer key = id; - if ( ( regexps.remove(key) == null ) - || (regexpsText.remove(key) == null ) - || (callbacks.remove(key) == null ) - || (threadedFlag.remove(key) == null ) - ) - throw new IvyException("client wants to remove an unexistant regexp "+id); + synchronized (regexps) { synchronized (callbacks) { synchronized (threadedFlag) { + if ( ( regexps.remove(key) == null ) + || (regexpsText.remove(key) == null ) + || (callbacks.remove(key) == null ) + || (threadedFlag.remove(key) == null ) + ) + throw new IvyException("client wants to remove an unexistant regexp "+id); + } } } } // unbinds to the first regexp - synchronized protected boolean unBindMsg(String re) { - if (!regexpsText.contains(re)) return false; - for (Integer k : regexpsText.keySet()) { - if ( (regexpsText.get(k)).compareTo(re) == 0) { - try { - bus.unBindMsg(k.intValue()); - } catch (IvyException ie) { - return false; + protected synchronized boolean unBindMsg(String re) { + synchronized (regexpsText) { + if (regexpsText.get(re) == null) return false; + for (Integer k : regexpsText.keySet()) { + if ( (regexpsText.get(k)).compareTo(re) == 0) { + try { + bus.unBindMsg(k.intValue()); + } catch (IvyException ie) { + return false; + } + return true; } - return true; } } return false; @@ -106,7 +120,10 @@ public class SelfIvyClient extends IvyClient { } protected void callCallback(IvyClient client, Integer key, String[] tab) { - IvyMessageListener callback=callbacks.get(key); + IvyMessageListener callback; + synchronized (callbacks) { + callback=callbacks.get(key); + } if (callback==null) { traceDebug("Not regexp matching id "+key.intValue()+", it must have been unsubscribed concurrently"); return; |