aboutsummaryrefslogtreecommitdiff
path: root/examples/Translate.java
diff options
context:
space:
mode:
Diffstat (limited to 'examples/Translate.java')
-rw-r--r--examples/Translate.java63
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");
+ }
+}