diff options
author | jestin | 2002-12-27 17:17:20 +0000 |
---|---|---|
committer | jestin | 2002-12-27 17:17:20 +0000 |
commit | 5a44dbef7c997b6e09a565306c60e7146f0f8c2b (patch) | |
tree | 682d9cf3854d06f11757d2ad9eb60ef3d45faf9a /examples/Translate.java | |
parent | 21db182d2ba046114e5a48893e32335e7990f8f5 (diff) | |
download | ivy-java-5a44dbef7c997b6e09a565306c60e7146f0f8c2b.zip ivy-java-5a44dbef7c997b6e09a565306c60e7146f0f8c2b.tar.gz ivy-java-5a44dbef7c997b6e09a565306c60e7146f0f8c2b.tar.bz2 ivy-java-5a44dbef7c997b6e09a565306c60e7146f0f8c2b.tar.xz |
Passage à 1.2.2
Diffstat (limited to 'examples/Translate.java')
-rw-r--r-- | examples/Translate.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/examples/Translate.java b/examples/Translate.java new file mode 100644 index 0000000..dee8210 --- /dev/null +++ b/examples/Translate.java @@ -0,0 +1,63 @@ +import fr.dgac.ivy.* ; +import java.io.* ; +import gnu.regexp.* ; + +class Translate { + + private Ivy bus; + + Translate(String filename) { + bus = new Ivy("Translater","Hello le monde",null); + parseFile(filename); + bus.bindMsg("^Bye$",new IvyMessageListener() { + // callback for "Bye" message + public void receive(IvyClient client, String[] args) {System.exit(0);} + }); + try { + // starts the bus on the default domain or IVY_DOMAIN property + bus.start(null); + } catch (IvyException ie) { + System.err.println("can't run the Ivy bus" + ie.getMessage()); + } + } + + private void parseFile(String filename) { + try { + BufferedReader in = new BufferedReader(new FileReader(new File(filename))); + String s; + RE regexp; + regexp = new RE("\"([^\"]*)\" \"([^\"]*)\""); + while ( (s=in.readLine()) != null ) { + REMatch result = regexp.getMatch(s); + bus.bindMsg(result.toString(1),new TALK(result.toString(2))); + } + in.close(); + } catch (REException ree) { + System.out.println("regexp error"); + System.exit(-1); + } catch (FileNotFoundException fnfe) { + System.out.println("file "+filename+" not found. Good bye !"); + System.exit(-1); + } catch (IOException ioe) { + System.out.println("error reading "+filename+". Good bye !"); + System.exit(-1); + } + } + + private class TALK implements IvyMessageListener { + private String go; + TALK(String s) {go=s;} + public void receive(IvyClient client, String[] args) { + bus.sendMsg(go); + } + } + + // callback associated to the "Hello" messages" + public void receive(IvyClient client, String[] args) { + bus.sendMsg("Bonjour"+((args.length>0)?args[0]:"")); + } + + public static void main(String args[]) { + new Translate("translation.txt"); + } +} |