aboutsummaryrefslogtreecommitdiff
path: root/src/IvyClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/IvyClient.java')
-rwxr-xr-xsrc/IvyClient.java48
1 files changed, 20 insertions, 28 deletions
diff --git a/src/IvyClient.java b/src/IvyClient.java
index aadbd0b..3cef803 100755
--- a/src/IvyClient.java
+++ b/src/IvyClient.java
@@ -12,6 +12,8 @@
* created for each remote client.
*
* CHANGELOG:
+ * 1.2.17
+ * - fixes a synchronization issue in sendMsg
* 1.2.16
* - now uses the synchronized wrappers of the Java API for all collections
* 1.2.14
@@ -103,7 +105,7 @@ import java.util.Vector;
import java.util.regex.*;
import java.util.Collection;
-public class IvyClient extends Thread {
+public class IvyClient implements Runnable {
// private variables
private final static int MAXPONGCALLBACKS = 10;
@@ -118,11 +120,10 @@ public class IvyClient extends Thread {
private BufferedReader in;
private OutputStream out;
private int remotePort=0;
- private volatile Thread clientThread;// volatile to ensure the quick communication
+ private volatile boolean keepgoing = true;// volatile to ensure the atomicity
private Integer clientKey;
private boolean discCallbackPerformed = false;
private String remoteHostname="unresolved";
- private static ThreadGroup clientsThreadGroup = new ThreadGroup("Ivy clients threadgroup");
// protected variables
String appName="none";
@@ -149,26 +150,17 @@ public class IvyClient extends Thread {
synchronized(bus) {
bus.addHalf(this); // register a half connexion
sendSchizo();
- // the registering (handShake) will take place at the reception of the regexps...
+ // the handShake will take place at the reception of the regexps.
}
}
remoteHostname = socket.getInetAddress().getHostName();
- clientThread = new Thread(clientsThreadGroup, this); // clientThread handles the incoming traffic
- clientThread.setName("Ivy client thread to "+remoteHostname+":"+remotePort);
- }
-
- /* removed from the constructor, to avoid Mulithread correctnaess issuses
- * see http://findbugs.sourceforge.net/bugDescriptions.html#SC_START_IN_CTOR
- */
- protected void doStart() {
- clientThread.start();
}
// sends our ID, whether we initiated the connexion or not
// the ID is the couple "host name,application Port", the host name
// information is in the socket itself, the port is not known if we
// initiate the connexion
- private void sendSchizo() throws IOException {
+ synchronized private void sendSchizo() throws IOException {
traceDebug("sending our service port "+bus.getAppPort());
Map<Integer,String> tosend=bus.getSelfIvyClient().regexpsText;
synchronized (tosend) {
@@ -273,9 +265,9 @@ public class IvyClient extends Thread {
protected int sendMsg(String message) {
int count = 0;
- for (Integer key : regexps.keySet()) {
- Pattern regexp = regexps.get(key);
- synchronized (regexp) {
+ synchronized (regexps) {
+ for (Integer key : regexps.keySet()) {
+ Pattern regexp = regexps.get(key);
Matcher m = regexp.matcher(message);
if (m.matches()) {
count++; // match
@@ -294,10 +286,9 @@ public class IvyClient extends Thread {
/* interrupt the listener thread */
private void stopListening() {
- Thread t = clientThread;
- if (t==null) return; // we can be summoned to quit from two path at a time
- clientThread=null;
- t.interrupt();
+ if ( !keepgoing ) return; // we can be summoned to quit from two path at a time
+ keepgoing = false;
+ interrupt();
}
/*
@@ -330,6 +321,7 @@ public class IvyClient extends Thread {
public void run() {
traceDebug("Thread started");
Thread thisThread = Thread.currentThread();
+ thisThread.setName("Ivy client thread to "+remoteHostname+":"+remotePort);
String msg = null;
try {
traceDebug("connection established with "+
@@ -337,10 +329,10 @@ public class IvyClient extends Thread {
} catch (Exception ie) {
traceDebug("Interrupted while resolving remote hostname");
}
- while (clientThread==thisThread) {
+ while ( keepgoing ) {
try {
if ((msg=in.readLine()) != null ) {
- if (clientThread!=thisThread) break; // early stop during readLine()
+ if ( !keepgoing ) break; // early stop during readLine()
if (!newParseMsg(msg)) {
close(true);
break;
@@ -354,9 +346,9 @@ public class IvyClient extends Thread {
ie.printStackTrace();
} catch (InterruptedIOException ioe) {
traceDebug("I have been interrupted. I'm about to leave my thread loop");
- if (thisThread!=clientThread) break;
+ if ( !keepgoing) break;
} catch (IOException e) {
- if (clientThread!=null) {
+ if ( !keepgoing ) {
traceDebug("abnormally Disconnected from "+ socket.getInetAddress().getHostName()+":"+socket.getPort());
}
break;
@@ -370,8 +362,8 @@ public class IvyClient extends Thread {
traceDebug("Thread stopped");
}
- @Override public void interrupt(){
- super.interrupt();
+ void interrupt(){
+ Thread.currentThread().interrupt();
try {
if (socket!=null) socket.close();
} catch (IOException ioe) {
@@ -577,7 +569,7 @@ public class IvyClient extends Thread {
try {
bus.addHalf(this);
sendSchizo();
- bus.handShake(this);
+ bus.handShake(this); //
} catch (IOException ioe) {
throw new IvyException(ioe.toString());
}