aboutsummaryrefslogtreecommitdiff
path: root/src/SelfIvyClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/SelfIvyClient.java')
-rw-r--r--src/SelfIvyClient.java113
1 files changed, 72 insertions, 41 deletions
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<Integer,IvyMessageListener> callbacks=new Hashtable<Integer,IvyMessageListener>();
+ private Hashtable<Integer,BindType> threadedFlag=new Hashtable<Integer,BindType>();
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;sub<re.getParenCount();sub++) {
- args[sub-1]=re.getParen(sub);
- if (bus.doProtectNewlines) args[sub-1]=decode(args[sub-1]);
+ private String[] toArgs(Matcher m) {
+ String[] args=
+ (m.groupCount()>0) ? new String[m.groupCount()] : new String[0];
+ //System.out.println("DEBUG "+args.length+" arguments");
+ for(int sub=0;sub<m.groupCount();sub++) {
+ args[sub]=m.group(sub+1);
+ if (bus.getProtectNewlines()) args[sub]=decode(args[sub]);
+ //System.out.println("DEBUG argument "+(sub)+"="+args[sub]);
}
return args;
}
- public String toString() { return "IvyClient (ourself)"+bus.appName+":"+appName; }
+ public String toString() {
+ return "IvyClient (ourself)"+bus.getAppName()+":"+appName;
+ }
+
+ // a class to perform the execution of each new callback within the Event
+ // Dispatch Thread
+ // this is an experimental feature introduced in 1.2.14
+ static class SwingRunner implements Runnable {
+ IvyMessageListener cb;
+ IvyClient c;
+ String[] args;
+ public SwingRunner(IvyMessageListener cb,IvyClient c,String[] a) {
+ this.cb=cb;
+ this.c=c;
+ args=a;
+ javax.swing.SwingUtilities.invokeLater(SwingRunner.this);
+ }
+ public void run() { cb.receive(c,args); }
+ }
// a class to perform the threaded execution of each new message
// this is an experimental feature introduced in 1.2.4
@@ -151,7 +182,7 @@ class SelfIvyClient extends IvyClient {
private void traceDebug(String s){
if (debug)
- System.out.println("-->SelfIvyClient "+bus.appName+":"+appName+"<-- "+s);
+ System.out.println("-->SelfIvyClient "+bus.getAppName()+":"+appName+"<-- "+s);
}
}