diff options
author | jestin | 2006-08-01 09:24:10 +0000 |
---|---|---|
committer | jestin | 2006-08-01 09:24:10 +0000 |
commit | d0afba319f95f0059d201c6b7cbfbc3322f2c0f6 (patch) | |
tree | 8da823c48b5843bf7022db1f3c53b63e85b8c4f5 | |
parent | d23e26197c375071459bbd6c5aa3d09c27020c31 (diff) | |
download | ivy-java-d0afba319f95f0059d201c6b7cbfbc3322f2c0f6.zip ivy-java-d0afba319f95f0059d201c6b7cbfbc3322f2c0f6.tar.gz ivy-java-d0afba319f95f0059d201c6b7cbfbc3322f2c0f6.tar.bz2 ivy-java-d0afba319f95f0059d201c6b7cbfbc3322f2c0f6.tar.xz |
Adding a correct example of translator in XML
documentation update
cleanup of IvyClient for Ping Pong
-rw-r--r-- | BUGS | 14 | ||||
-rw-r--r-- | doc/ivy-java.sgml | 51 | ||||
-rw-r--r-- | examples/EndApp.java | 36 | ||||
-rw-r--r-- | examples/Makefile | 50 | ||||
-rw-r--r-- | examples/TestIvySwing.java | 8 | ||||
-rw-r--r-- | examples/TranslateXML.java | 33 | ||||
-rw-r--r-- | java.mk | 44 | ||||
-rw-r--r-- | src/After.java | 8 | ||||
-rwxr-xr-x | src/IvyClient.java | 32 | ||||
-rw-r--r-- | src/Makefile | 8 |
10 files changed, 203 insertions, 81 deletions
@@ -1,4 +1,18 @@ ------------------------------------------------------------------------------------- +.bound ne fonctionne pas ... + +PseudoControleurCPDLC subscribes to ^CPDLC FLIGHT ([^ ]+) ([0-9]+) ([0-9_]+) +([0-9]{8})([0-9]{6}) dM18 (IAS|MAC) (\d+) +PseudoControleurCPDLC subscribes to ^CPDLC FLIGHT ([^ ]+) ([0-9]+) ([0-9_]+) +([0-9]{8})([0-9]{6}) dM(9|10|22|70) (.*) +PseudoControleurCPDLC connected +PseudoControleurCPDLC sent 'envoyez les requetes' +.bound PseudoControleurCPDLC +PseudoControleurCPDLC has subscribed to: ^CPDLC FLIGHT ([^ ]+) ([0-9]+) +([0-9_]+) ([0-9]{8})([0-9]{6}) dM(9|10|22|70) (.*) +total: 1, unbounded:0 + +------------------------------------------------------------------------------------- 7 clients on the bus -> CDS:AIX -> Rejeu diff --git a/doc/ivy-java.sgml b/doc/ivy-java.sgml index 0e4d233..a7791c5 100644 --- a/doc/ivy-java.sgml +++ b/doc/ivy-java.sgml @@ -837,7 +837,58 @@ unsubscription will take place. bus.sendMsg("MyRequest ID="+id+" QUER=2+2"); </programlisting> </para> +</sect2> +<sect2><title>how to quit the application ?</title> +<para> +If your application decides to quit or leave the bus, the safest way is to +invoque <function>Ivy.stop()</function>. This will send a clean goodby message +to the other agents, close the sockets, end the threads, etc. However, you can +en the JVM performing a <function>System.exit(int)</function> and let the +other agents realize that you've gone. +</para> +<para> +If some other agents wants you to quit the bus, it will send you a die +message. The protocol will first run you +<function>IvyApplicationListener.die()</function>, if any, then +perform some socket/thread clean up, and disconnect you from the bus. As a +good citizen on the Ivy bus, you should take the appropriate measures top stop +your application within the <function>IvyApplicationListener.die()</function> +method, for instance run <function>System.exit()</function> or make all you +thread stop, disposing your toplevel Swing Frames. Here is an example of how to do it: +<programlisting> +import fr.dgac.ivy.* ; +import javax.swing.*; + +public class EndApp extends IvyApplicationAdapter { + + public static void main(String[] args) throws IvyException { + Ivy bus=new Ivy("EndApp","EndApp ready",null); + EndApp e = new EndApp(bus); // a frame is opened, and the Swing Thread is started + bus.addApplicationListener(e); + bus.start(Ivy.getDomain(null)); // Ivy threads are up and running + // the control flow won't stop until the end of all above threads + } + + private Ivy bus; + JFrame f; + + public EndApp(Ivy b) { + this.bus=b; + f=new JFrame("test"); + f.getContentPane().add(new JLabel("some label"),java.awt.BorderLayout.CENTER); + f.pack(); + f.setVisible(true); + } + + public void die(IvyClient client, int id,String msgarg) { + System.out.println("received die msg from " + client.getApplicationName()); + f.dispose(); // closes the only window, thus quitting the swing thread + } // end of die callback, the Ivy threads are stopped + +} +</programlisting> +</para> </sect2> </sect1> diff --git a/examples/EndApp.java b/examples/EndApp.java new file mode 100644 index 0000000..df18201 --- /dev/null +++ b/examples/EndApp.java @@ -0,0 +1,36 @@ +/** + * example of close code + * (c) CENA + * Changelog: + * 1.2.12 + */ +import fr.dgac.ivy.* ; +import javax.swing.*; + +public class EndApp extends IvyApplicationAdapter { + + public static void main(String[] args) throws IvyException { + Ivy bus=new Ivy("EndApp","EndApp ready",null); + EndApp e = new EndApp(bus); // a frame is opened, and the Swing Thread is started + bus.addApplicationListener(e); + bus.start(Ivy.getDomain(null)); // Ivy threads are up and running + // the control flow won't stop until the end of all above threads + } + + private Ivy bus; + JFrame f; + + public EndApp(Ivy b) { + this.bus=b; + f=new JFrame("test"); + f.getContentPane().add(new JLabel("some label"),java.awt.BorderLayout.CENTER); + f.pack(); + f.setVisible(true); + } + + public void die(IvyClient client, int id,String msgarg) { + System.out.println("received die msg from " + client.getApplicationName()); + f.dispose(); // closes the only window, thus quitting the swing thread + } // end of die callback, the Ivy threads are stopped + +} diff --git a/examples/Makefile b/examples/Makefile index 4918928..923b69e 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -1,53 +1,13 @@ +include ../java.mk + .SUFFIXES: .java .class - SRCS = TestIvy.java TestIvySwing.java Counter.java ivyTranslater.java Translate.java TranslateXML.java + SRCS = TestIvy.java TestIvySwing.java Counter.java ivyTranslater.java Translate.java TranslateXML.java EndApp.java OBJS = $(SRCS:.java=.class) -NANOXMLPATH=/export/home/jestin/java/Jars/nanoxml-2.2.1.jar -GNUPATH=/usr/share/java/regexp.jar:/usr/share/java/gnu.getopt.jar -BUILDPATH=../lib/ivy-java.jar -ADD=$(GNUPATH):$(BUILDPATH):$(NANOXMLPATH) - -####################################### -# jikes setup on my box -####################################### -# RTPATH = /usr/lib/j2re1.3/lib/rt.jar -# RTPATH = /usr/local/j2sdk1.4.1/jre/lib/rt.jar -#JIKESPATH = ${RTPATH}:/usr/share/java/gnu.getopt.jar:/usr/share/java/repository -#JAVACOPTS = -d . -deprecation -# JAVAC = jikes -classpath $(JIKESPATH):$(ADD) - -####################################### -# generic setup -####################################### - JAVAC = javac -JAVACOPTS = -d . -deprecation -CLASSPATH = -classpath .:$(ADD) - -####################################### -# blackdown jdk118 setup -####################################### -# JAVAC = /usr/local/jdk118_v3/bin/javac -#CLASSPATH = -classpath .:/usr/local/jdk118_v3/lib/classes.zip:/home/jestin/java/Jars/swingall.jar:$(ADD) -#JAVACOPTS = -d . - -###################################### -# local Blackdown linux 1.2.2 -###################################### -#CLASSPATH = -classpath .:$(ADD) -#JAVACOPTS = -deprecation -d . -# JAVAC = /usr/local/jdk1.2.2/bin/javac - -###################################### -# local Blackdown linux 1.3.1 -###################################### -#CLASSPATH = -classpath .:$(ADD) -#JAVACOPTS = -deprecation -#JAVAOPTS = -#JAVA = /usr/local/j2sdk1.3.1/bin/java -#JAVAC = /usr/local/j2sdk1.3.1/bin/javac +BUILDPATH=../ivy-1.2.12.jar .java.class: - $(JAVAC) $(JAVACOPTS) $(CLASSPATH) $< + $(JAVAC) $(JAVACOPTS) -classpath $(BUILDPATH):$(GNUPATH) $< all: $(OBJS) diff --git a/examples/TestIvySwing.java b/examples/TestIvySwing.java index cb3eca5..31a54a9 100644 --- a/examples/TestIvySwing.java +++ b/examples/TestIvySwing.java @@ -122,9 +122,9 @@ class TestIvySwing extends JPanel implements IvyApplicationListener { ports.addActionListener(new ComboCB()); p.add(ports); add(p,BorderLayout.SOUTH); - tfRegex.setNextFocusableComponent(tfSend); - tfSend.setNextFocusableComponent(tfRegex); - tfSend.setRequestFocusEnabled(true); + //tfRegex.setNextFocusableComponent(tfSend); + //tfSend.setNextFocusableComponent(tfRegex); + //tfSend.setRequestFocusEnabled(true); localname = "TestIvySwing "+Ivy.libVersion+" ("+index+")"; index++; bus = new Ivy(localname,localname+" ready",this); @@ -232,7 +232,7 @@ class TestIvySwing extends JPanel implements IvyApplicationListener { if (--nbTIS == 0) System.exit(0); // I leave when the last TestIvySwing exits System.out.println("closed"); } - public void windowActivated(WindowEvent e) {tfSend.grabFocus();} + public void windowActivated(WindowEvent e) {tfSend.requestFocus();} } private class SPAWN implements ActionListener { diff --git a/examples/TranslateXML.java b/examples/TranslateXML.java index b7ec8fb..9dc3283 100644 --- a/examples/TranslateXML.java +++ b/examples/TranslateXML.java @@ -8,6 +8,8 @@ * this program is LGPL ... etc etc * * New: + * 1.2.12 + * leaving NanoXML in favor to the full featured xpath etc... * 1.2.6 * get compatible with new Ivy version * 1.2.3 @@ -16,10 +18,14 @@ * */ import fr.dgac.ivy.* ; +import gnu.getopt.Getopt; import java.io.* ; -import net.n3.nanoxml.*; import java.util.*; -import gnu.getopt.Getopt; +import javax.xml.parsers.*; +import javax.xml.xpath.*; +import org.xml.sax.*; +import org.w3c.dom.*; + class TranslateXML { @@ -42,19 +48,16 @@ class TranslateXML { private void parseFile(String filename) { try { - IXMLParser parser = XMLParserFactory.createDefaultXMLParser(); - IXMLReader reader = StdXMLReader.fileReader(filename); - parser.setReader(reader); - IXMLElement xml = (IXMLElement) parser.parse(); - // checks if everything is OK - // XMLWriter writer = new XMLWriter(System.out); - // writer.write(xml); - Vector translations = xml.getChildrenNamed("translate"); - for (int i=0;i<translations.size();i++) { - IXMLElement trans = (IXMLElement)translations.elementAt(i); - String from = trans.getAttribute("from",null); - String to = trans.getAttribute("to",null); - if ((from!=null)&&(to!=null)) { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document trans = builder.parse(new File(filename)); + XPath xpath = XPathFactory.newInstance().newXPath(); + NodeList list = (NodeList)xpath.evaluate("/translations/translate",trans,XPathConstants.NODESET); + for (int i=0;i<list.getLength();i++) { + NamedNodeMap nnm = list.item(i).getAttributes(); + String from = nnm.getNamedItem("from").getNodeValue(); + String to = nnm.getNamedItem("to").getNodeValue(); + if ((from!=null)&&(to!=null)&&(!from.equals(to))) { System.out.println("translating every \""+from+"\" in \""+to+"\""); bus.bindMsg(from,new TALK(to)); } @@ -8,6 +8,15 @@ # 1.2.8 first # + GNUPATH = /usr/share/java/gnu-getopt.jar:/usr/share/java/regexp.jar # debian SID + #GNUPATH = ../bundle # on my MAC +#GNUPATH = /usr/lib/jdk1.1/lib/classes.zip:/usr/share/java/gnu.getopt.jar:/usr/share/java/regexp.jar # debian woody +#GNUPATH = ${HOME}/java/Jars/gnu.getopt.jar:${HOME}/java/Jars/regexp.jar # Others +#RTPATH = /usr/local/jdk1.5.0/jre/lib/rt.jar # for jikes on my box +#RTPATH = /usr/local/jdk118_v3/lib/classes.zip:/home/jestin/java/Jars/swingall.jar # for 1.1.8 on my box + +CLASSPATH = -classpath .:$(GNUPATH) + #JAVA = kaffe JAVA = java #JAVA = gij @@ -17,3 +26,38 @@ JAVAC = gcj -C --encoding=8859_1 -Wall -g #JAVACOPTS = #JAVACOPTS = -target 1.2 JAR = jar + + + +####################################### +# jikes setup on my box +####################################### +# RTPATH = /usr/lib/j2re1.3/lib/rt.jar +# RTPATH = /usr/local/j2sdk1.4.1/jre/lib/rt.jar +#JIKESPATH = ${RTPATH}:/usr/share/java/gnu.getopt.jar:/usr/share/java/repository +#JAVACOPTS = -d . -deprecation +# JAVAC = jikes -classpath $(JIKESPATH):$(ADD) +####################################### +# blackdown jdk118 setup +####################################### +# JAVAC = /usr/local/jdk118_v3/bin/javac +#CLASSPATH = -classpath .:/usr/local/jdk118_v3/lib/classes.zip:/home/jestin/java/Jars/swingall.jar:$(ADD) +#JAVACOPTS = -d . + +###################################### +# local Blackdown linux 1.2.2 +###################################### +#CLASSPATH = -classpath .:$(ADD) +#JAVACOPTS = -deprecation -d . +# JAVAC = /usr/local/jdk1.2.2/bin/javac + +###################################### +# local Blackdown linux 1.3.1 +###################################### +#CLASSPATH = -classpath .:$(ADD) +#JAVACOPTS = -deprecation +#JAVAOPTS = +#JAVA = /usr/local/j2sdk1.3.1/bin/java +#JAVAC = /usr/local/j2sdk1.3.1/bin/javac + + diff --git a/src/After.java b/src/After.java index fa43cf9..b36d5de 100644 --- a/src/After.java +++ b/src/After.java @@ -26,10 +26,10 @@ public class After extends IvyApplicationAdapter implements IvyMessageListener { String name="AFTER"; int timeout = DEFAULTTIMEOUT; while ((c = opt.getopt()) != -1) switch (c) { - case 'b': domain=opt.getOptarg(); break; - case 't': timeout=Integer.parseInt(opt.getOptarg()); break; - case 'h': - default: System.out.println(helpmsg); System.exit(0); + case 'b': domain=opt.getOptarg(); break; + case 't': timeout=Integer.parseInt(opt.getOptarg()); break; + case 'h': + default: System.out.println(helpmsg); System.exit(0); } if (opt.getOptind()!=args.length-1) { System.out.println(helpmsg); System.exit(0); } String regexp=args[opt.getOptind()]; diff --git a/src/IvyClient.java b/src/IvyClient.java index b0c1200..af42342 100755 --- a/src/IvyClient.java +++ b/src/IvyClient.java @@ -103,10 +103,11 @@ public class IvyClient implements Runnable { final static char newLineChar = '\n'; // private variables + private final static int MAXPONGCALLBACKS = 10; private static int pingSerial = 0; private static Integer csMutex=new Integer(0); private static int clientSerial=0; /* an unique ID for each IvyClient */ - private Stack PCHStack = new Stack(); + private Hashtable PingCallbacksTable = new Hashtable(); private String messages_classes[] = null; private Ivy bus; @@ -246,7 +247,7 @@ public class IvyClient implements Runnable { * received */ public void ping(PingCallback pc) throws IvyException { - PCHStack.push(new PingCallbackHolder(pc)); + PCHadd(pingSerial,pc); sendString(Ping,pingSerial++,""); } @@ -488,7 +489,7 @@ public class IvyClient implements Runnable { } break; case Pong: - ((PingCallbackHolder)PCHStack.pop()).run(); + PCHget(msgId); break; case Ping: sendString(Pong,msgId.intValue(),""); @@ -591,16 +592,37 @@ public class IvyClient implements Runnable { traceDebug(s); } + void PCHadd(int serial,PingCallback pc) { + PingCallbacksTable.put(new Integer(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(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; + } + pc.run(); + } + private class PingCallbackHolder { PingCallback pc; long epoch; + int age() { return (int)(System.currentTimeMillis()-epoch); } PingCallbackHolder(PingCallback pc) { this.pc=pc; epoch=System.currentTimeMillis(); - PCHStack.push(this); } void run() { - pc.pongReceived(IvyClient.this,(int)(System.currentTimeMillis()-epoch)); + pc.pongReceived(IvyClient.this,age()); } } diff --git a/src/Makefile b/src/Makefile index 1d89856..2d33d08 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,14 +8,6 @@ include ../java.mk OBJS = $(SRCS:.java=.class) DOCS = ../doc/html/api - GNUPATH = /usr/share/java/gnu-getopt.jar:/usr/share/java/regexp.jar # debian SID - #GNUPATH = ../bundle # on my MAC -#GNUPATH = /usr/lib/jdk1.1/lib/classes.zip:/usr/share/java/gnu.getopt.jar:/usr/share/java/regexp.jar # debian woody -#GNUPATH = ${HOME}/java/Jars/gnu.getopt.jar:${HOME}/java/Jars/regexp.jar # Others -#RTPATH = /usr/local/jdk1.5.0/jre/lib/rt.jar # for jikes on my box -#RTPATH = /usr/local/jdk118_v3/lib/classes.zip:/home/jestin/java/Jars/swingall.jar # for 1.1.8 on my box - -CLASSPATH = -classpath .:$(GNUPATH) #ivy-java: # $(JAVAC) $(JAVACOPTS) $(CLASSPATH) $(SRCS) |