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/IvyClient.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/IvyClient.java')
-rwxr-xr-x | src/IvyClient.java | 52 |
1 files changed, 31 insertions, 21 deletions
diff --git a/src/IvyClient.java b/src/IvyClient.java index 8ed25f7..17ce224 100755 --- a/src/IvyClient.java +++ b/src/IvyClient.java @@ -10,6 +10,8 @@ * created for each remote client. * * CHANGELOG: + * 1.2.16 + * - now uses the synchronized wrappers of the Java API for all collections * 1.2.14 * - use autoboxing for the creation of Integer (instead of * new Integer(int). This alows caching, avoids object allocation, and the @@ -89,8 +91,12 @@ package fr.dgac.ivy ; import java.lang.Thread; import java.net.*; import java.io.*; -import java.util.*; +import java.util.Map; +import java.util.HashMap; +import java.util.Collections; +import java.util.Vector; import java.util.regex.*; +import java.util.Collection; public class IvyClient extends Thread { @@ -119,7 +125,8 @@ public class IvyClient extends Thread { private static int pingSerial = 0; private static final Object lock = new Object(); private static int clientSerial=0; /* an unique ID for each IvyClient */ - private Hashtable <Integer,PingCallbackHolder>PingCallbacksTable = new Hashtable<Integer,PingCallbackHolder>(); + private Map <Integer,PingCallbackHolder>PingCallbacksTable = + Collections.synchronizedMap(new HashMap<Integer,PingCallbackHolder>()); private Ivy bus; private Socket socket; @@ -133,8 +140,8 @@ public class IvyClient extends Thread { // protected variables String appName="none"; - Hashtable <Integer,Pattern>regexps = new Hashtable<Integer,Pattern>(); - Hashtable <Integer,String>regexpsText = new Hashtable<Integer,String>(); + Map <Integer,Pattern>regexps = Collections.synchronizedMap(new HashMap<Integer,Pattern>()); + Map <Integer,String>regexpsText = Collections.synchronizedMap(new HashMap<Integer,String>()); static boolean debug = (System.getProperty("IVY_DEBUG")!=null) ; // int protocol; private boolean incoming; @@ -175,7 +182,7 @@ public class IvyClient extends Thread { // initiate the connexion private void sendSchizo() throws IOException { traceDebug("sending our service port "+bus.getAppPort()); - Hashtable<Integer,String> tosend=bus.getSelfIvyClient().regexpsText; + Map<Integer,String> tosend=bus.getSelfIvyClient().regexpsText; sendString(SchizoToken,bus.getAppPort(),bus.getAppName()); for (Integer ikey : tosend.keySet()) sendRegexp(ikey.intValue(),tosend.get(ikey)); sendString( EndRegexp,0,""); @@ -207,7 +214,7 @@ public class IvyClient extends Thread { * The content is not modifyable because String are not mutable, and cannot * be modified once they are create. */ - public Enumeration<String> getRegexps() { return regexpsText.elements(); } + public Collection<String> getRegexps() { return regexpsText.values(); } /** * allow an Ivy package class to access the list of regexps at a @@ -217,8 +224,7 @@ public class IvyClient extends Thread { public String[] getRegexpsArray() { String[] s = new String[regexpsText.size()]; int i=0; - for (Enumeration<String>e=getRegexps();e.hasMoreElements();) - s[i++]=e.nextElement(); + for (String sr : getRegexps()) s[i++]=sr; return s; } @@ -628,24 +634,28 @@ public class IvyClient extends Thread { } void PCHadd(int serial,PingCallback pc) { - PingCallbacksTable.put(serial,new PingCallbackHolder(pc)); - if (PingCallbacksTable.size()>MAXPONGCALLBACKS) { - // more than MAXPONGCALLBACKS callbacks, we ought to limit to prevent a - // memory leak - // TODO remove the first - Integer smallest=(Integer)new TreeSet<Integer>(PingCallbacksTable.keySet()).first(); - PingCallbackHolder pch = (PingCallbackHolder)PingCallbacksTable.remove(smallest); - System.err.println("no response from "+getApplicationName()+" to ping "+smallest+" after "+pch.age()+" ms, discarding"); + synchronized (PingCallbacksTable) { + PingCallbacksTable.put(serial,new PingCallbackHolder(pc)); + if (PingCallbacksTable.size()>MAXPONGCALLBACKS) { + // more than MAXPONGCALLBACKS callbacks, we ought to limit to prevent a + // memory leak + // TODO remove the first + Integer smallest=(Integer)new java.util.TreeSet<Integer>(PingCallbacksTable.keySet()).first(); + PingCallbackHolder pch = (PingCallbackHolder)PingCallbacksTable.remove(smallest); + System.err.println("no response from "+getApplicationName()+" to ping "+smallest+" after "+pch.age()+" ms, discarding"); + } } } void PCHget(Integer serial) { - PingCallbackHolder pc = (PingCallbackHolder)PingCallbacksTable.remove(serial); - if (pc==null) { - System.err.println("warning: pong received for a long lost callback"); - return; + synchronized (PingCallbacksTable) { + PingCallbackHolder pc = (PingCallbackHolder)PingCallbacksTable.remove(serial); + if (pc==null) { + System.err.println("warning: pong received for a long lost callback"); + return; + } + pc.run(); } - pc.run(); } private class PingCallbackHolder { |