4. Your first Ivy application

We are going to write a "Hello world translater" for an Ivy bus. The application will subscribe to all messages starting with "Hello", and re-emit them after translating "Hello" into "Bonjour". In addition, the application will quit when it receives any message containing exactly "Bye".

4.1. The code

Here is the code of "ivyTranslater.java":

import fr.dgac.ivy.* ;

class ivyTranslater implements IvyMessageListener {

  private Ivy bus;

  ivyTranslater() {
    // initialization
    bus = new Ivy("IvyTranslater","Hello le monde",null);
    bus.bindMsg("^Hello(.*)",this);
    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());
    }
  }

  // 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 ivyTranslater(); }
}

4.2. Compiling it

You should be able to compile the application with the following command (if the ivy-java jar is in your development classpath):

$ javac ivyTranslater.java
$

4.3. Testing

We are going to test our application with fr.dgac.ivy.Probe. In a terminal window, launch ivyTranslater.

$ java ivyTranslater
Then in another terminal window, launch java fr.dgac.ivy.Probe '(.*)'. You are then ready to start. Type "Hello Paul", and you should get "Bonjour Paul". Type "Bye", and your application should quit:
$ java fr.dgac.ivy.Probe '(.*)'
you want to subscribe to (.*)
broadcasting on 127.255.255.255:2010
IvyTranslater connected
IvyTranslater subscribes to ^Bye$
IvyTranslater subscribes to ^Hello(.*)
IvyTranslater sent 'Hello le monde'
Hello Paul
-> Sent to 1 peers
IvyTranslater sent 'Bonjour Paul'
Bye
-> Sent to 1 peers
IvyTranslater disconnected
<Ctrl-D>
$