The Ivy java library guide
Prev Next

Your first Ivy java 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".

The code

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

Compiling

On a Unix computer, you should be able to compile the application with the following command :
$ javac ivyTranslater.java
$

Testing

We are going to test our application with Probe. In a terminal window, launch ivyTranslater:
$ java ivyTranslater
 

Then in another terminal window, launch Probe. You are then ready to start, Type "Hello Paul", and you should get "Bonjour Paul". Then 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>
$


Prev Home Next
The Ivy java library   Basic functions