aboutsummaryrefslogtreecommitdiff
path: root/src/Ivy.java
diff options
context:
space:
mode:
authorjestin2005-11-22 12:16:36 +0000
committerjestin2005-11-22 12:16:36 +0000
commit8e92224db5274f8a028f28f830b52e78ee88fda2 (patch)
tree2d4281a99d94329bce5b23af65377243cccbd98e /src/Ivy.java
parent5d466ac8508cf202bd025bc9d813b07fc47ac44b (diff)
downloadivy-java-8e92224db5274f8a028f28f830b52e78ee88fda2.zip
ivy-java-8e92224db5274f8a028f28f830b52e78ee88fda2.tar.gz
ivy-java-8e92224db5274f8a028f28f830b52e78ee88fda2.tar.bz2
ivy-java-8e92224db5274f8a028f28f830b52e78ee88fda2.tar.xz
See changes inside.
major change in multibus error handling. I reintroduced bugs ..
Diffstat (limited to 'src/Ivy.java')
-rwxr-xr-xsrc/Ivy.java119
1 files changed, 90 insertions, 29 deletions
diff --git a/src/Ivy.java b/src/Ivy.java
index 2fc6307..0b5168a 100755
--- a/src/Ivy.java
+++ b/src/Ivy.java
@@ -14,6 +14,9 @@
*
* CHANGELOG:
* 1.2.8:
+ * - domainaddr goes protected in Domain ( gij compatibility )
+ * - checks if (Client)e.nextElement() each time we want to ...
+ * Multithreaded Enumerations ..., should fix [YJnul05]
* - added getDomainArgs(String,String[]) as a facility to parse the
* command line in search of a -b domain
* - added getWBUId(), un function returning a string ID to perform
@@ -120,7 +123,7 @@ public class Ivy implements Runnable {
protected boolean doProtectNewlines = false ;
private boolean doSendToSelf = false ;
protected SelfIvyClient selfIvyClient ;
- public final static int TIMEOUTLENGTH = 3000;
+ public final static int TIMEOUTLENGTH = 1000;
private static int serial=0;
private int myserial=serial++;
static long current = System.currentTimeMillis();
@@ -136,7 +139,7 @@ public class Ivy implements Runnable {
* @param appcb A callback handling the notification of connexions and
* disconnections, may be null
*/
- public Ivy( String name, String message, IvyApplicationListener appcb) {
+ public Ivy(String name, String message, IvyApplicationListener appcb) {
appName = name;
ready_message = message;
debug = (System.getProperty("IVY_DEBUG")!=null);
@@ -174,18 +177,31 @@ public class Ivy implements Runnable {
IvyClient ic;
if (name==null) throw new IvyException("null name given to waitForClient");
// first check if client with the same name is on the bus
- for (Enumeration e=clients.elements();e.hasMoreElements();) {
- ic = (IvyClient)e.nextElement();
- if (name.compareTo(ic.getApplicationName())==0) return ic;
- }
+ if ((ic=alreadyThere(clients,name))!=null) return ic;
// if not enter the waiting loop
- WaiterClient w = new WaiterClient(name,timeout);
+ WaiterClient w = new WaiterClient(name,timeout,clients);
int i = addApplicationListener(w);
ic=w.waitForClient();
removeApplicationListener(i);
return ic;
}
+ /*
+ * since 1.2.8
+ */
+ static protected IvyClient alreadyThere(Hashtable c,String name) {
+ IvyClient ic;
+ for (Enumeration e=c.elements();e.hasMoreElements();) {
+ try {
+ ic = (IvyClient)e.nextElement();
+ } catch (ArrayIndexOutOfBoundsException _ ) {
+ return null; // with gij, it ... can happen
+ }
+ if ((ic!=null)&&(name.compareTo(ic.getApplicationName())==0)) return ic;
+ }
+ return null;
+ }
+
/**
* connects the Ivy bus to a domain or list of domains.
*
@@ -199,8 +215,10 @@ public class Ivy implements Runnable {
* broadcast. Beware of routing problems ! You can also use a comma
* separated list of domains.
*
+ * 1.2.8: goes synchronized. I don't know if it's really useful
+ *
*/
- public void start(String domainbus) throws IvyException {
+ public synchronized void start(String domainbus) throws IvyException {
if (!stopped) throw new IvyException("cannot start a bus that's already started");
stopped=false;
if (domainbus==null) domainbus=getDomain(null);
@@ -250,7 +268,7 @@ public class Ivy implements Runnable {
/**
* disconnects from the Ivy bus
*/
- public synchronized void stop() {
+ public void stop() {
if (stopped) return;
stopped=true;
traceDebug("beginning stopping");
@@ -259,6 +277,8 @@ public class Ivy implements Runnable {
Thread t=serverThread;
serverThread=null;
if (t!=null) t.interrupt(); // The serverThread might be stopped even before having been created
+ // System.out.println("IZZZ joining "+t);
+ try { t.join(); } catch ( InterruptedException _ ) { }
// TODO BUG avec gcj+kaffe, le close() reste pendu et ne rend pas la main
app.close();
// stopping the IvyWatchers
@@ -266,9 +286,12 @@ public class Ivy implements Runnable {
watchers.clear();
// stopping the remaining IvyClients
for (Enumeration e=clients.elements();e.hasMoreElements();) {
- IvyClient c = (IvyClient)e.nextElement();
- if (c!=null) c.close(true);
- removeClient(c);
+ try {
+ IvyClient c = (IvyClient)e.nextElement();
+ if (c!=null) { c.close(true);removeClient(c); }
+ } catch (ArrayIndexOutOfBoundsException _ ) {
+ continue;
+ }
}
} catch (IOException e) {
traceDebug("IOexception Stop ");
@@ -322,8 +345,12 @@ public class Ivy implements Runnable {
else if ( (message.indexOf(IvyClient.newLineChar)!=-1)||(message.indexOf(IvyClient.endArgChar)!=-1))
throw new IvyException("newline character not allowed in Ivy messages");
for ( Enumeration e=clients.elements();e.hasMoreElements();) {
- IvyClient client = (IvyClient)e.nextElement();
- count += client.sendMsg(message);
+ try {
+ IvyClient client = (IvyClient)e.nextElement();
+ if (client!=null) count += client.sendMsg(message);
+ } catch (ArrayIndexOutOfBoundsException _ ) {
+ continue; // gij problem
+ }
}
if (doSendToSelf) count+=selfIvyClient.sendSelfMsg(message);
return count;
@@ -393,8 +420,12 @@ public class Ivy implements Runnable {
int key=selfIvyClient.bindMsg(sregexp,callback,async);
// notifies the other clients this new regexp
for (Enumeration e=clients.elements();e.hasMoreElements();) {
- IvyClient c = (IvyClient)e.nextElement();
- c.sendRegexp(key,sregexp);
+ try {
+ IvyClient c = (IvyClient)e.nextElement();
+ if (c!=null) c.sendRegexp(key,sregexp);
+ } catch (ArrayIndexOutOfBoundsException _ ) {
+ continue; // gij problem
+ }
}
return key;
}
@@ -425,8 +456,15 @@ public class Ivy implements Runnable {
*/
public void unBindMsg(int id) throws IvyException {
selfIvyClient.unBindMsg(id);
- for (Enumeration e=clients.elements();e.hasMoreElements();)
- ((IvyClient)e.nextElement()).delRegexp(id );
+ for (Enumeration e=clients.elements();e.hasMoreElements();) {
+ try {
+ IvyClient ic=(IvyClient)e.nextElement();
+ if (ic!=null) ic.delRegexp(id );
+ } catch (ArrayIndexOutOfBoundsException _ ) {
+ continue;
+ }
+
+ }
}
/**
@@ -553,12 +591,17 @@ public class Ivy implements Runnable {
}
/**
- * gives the names of IvyClient(s)
+ * gives the IvyClient() at a given instant
*/
public Vector getIvyClients() {
Vector v=new Vector();
for (Enumeration e=clients.elements();e.hasMoreElements();) {
- v.addElement(e.nextElement());
+ try {
+ IvyClient ic=(IvyClient)e.nextElement();
+ if (ic!=null) v.addElement(ic);
+ } catch (ArrayIndexOutOfBoundsException _) {
+ continue;
+ }
}
return v;
}
@@ -570,9 +613,15 @@ public class Ivy implements Runnable {
*/
public Vector getIvyClientsByName(String name) {
Vector v=new Vector();
+ String icname;
for (Enumeration e=clients.elements();e.hasMoreElements();) {
- IvyClient ic = (IvyClient)e.nextElement();
- if ( ((ic.getApplicationName()).compareTo(name))==0 ) v.addElement(ic);
+ try {
+ IvyClient ic = (IvyClient)e.nextElement();
+ if ( (ic==null)||((icname=ic.getApplicationName())==null) ) break;
+ if (icname.compareTo(name)==0) v.addElement(ic);
+ } catch (ArrayIndexOutOfBoundsException _ ) {
+ continue;
+ }
}
return v;
}
@@ -647,14 +696,17 @@ public class Ivy implements Runnable {
synchronized (clients) { clients.put(c.getClientKey(),c); }
traceDebug("added "+c+" in clients: "+getClientNames(clients));
}
+
void removeClient(IvyClient c) {
synchronized (clients) {clients.remove(c.getClientKey());}
traceDebug("removed "+c+" from clients: "+getClientNames(clients));
}
+
void addHalf(IvyClient c) {
synchronized(half){half.put(c.getClientKey(),c);}
traceDebug("added "+c+" in half: "+getClientNames(half));
}
+
void removeHalf(IvyClient c) {
synchronized(half){half.remove(c.getClientKey());}
traceDebug("removed "+c+" from half: "+getClientNames(half));
@@ -672,10 +724,16 @@ public class Ivy implements Runnable {
private IvyClient searchPeer(IvyClient ic) {
IvyClient peer;
- for (Enumeration e=half.elements();e.hasMoreElements();)
- if ((peer=(IvyClient)e.nextElement()).equals(ic)) return peer;
- for (Enumeration e=clients.elements();e.hasMoreElements();)
- if ((peer=(IvyClient)e.nextElement()).equals(ic)) return peer;
+ for (Enumeration e=half.elements();e.hasMoreElements();) {
+ peer=(IvyClient)e.nextElement();
+ if ((peer!=null)&&(peer.equals(ic))) return peer;
+ }
+ synchronized (clients) {
+ for (Enumeration e=clients.elements();e.hasMoreElements();){
+ peer=(IvyClient)e.nextElement();
+ if ((peer!=null)&&(peer.equals(ic))) return peer;
+ }
+ }
return null;
}
@@ -689,8 +747,9 @@ public class Ivy implements Runnable {
try {
Socket socket = app.accept();
if ((thisThread!=serverThread)||stopped) break; // early disconnexion
- new IvyClient(this,socket,0); // the peer called me
+ new IvyClient(this,socket,0,true); // the peer called me
} catch (InterruptedIOException ie) {
+ // traceDebug("server socket was interrupted. good");
if (thisThread!=serverThread) break;
} catch( IOException e ) {
if (serverThread==thisThread) {
@@ -698,7 +757,9 @@ public class Ivy implements Runnable {
System.out.println("Ivy server socket reader caught an exception " + e.getMessage());
System.out.println("this is probably a bug in your JVM ! (e.g. blackdown jdk1.1.8 linux)");
System.exit(0);
- } else { traceDebug("my server socket has been closed"); }
+ } else {
+ traceDebug("my server socket has been closed");
+ }
}
}
traceDebug("service thread stopped"); // THREADDEBUG
@@ -738,7 +799,7 @@ public class Ivy implements Runnable {
class Domain {
- private String domainaddr;
+ String domainaddr;
int port;
public Domain(String domainaddr,int port) {this.domainaddr=domainaddr;this.port=port;}
public String toString() {return domainaddr+":"+port;}