aboutsummaryrefslogtreecommitdiff
path: root/examples/ivyTranslater.java
blob: 497ac87c002a7603d884671a741dbb6527b1bd24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * Yet another Ivy java program example
 *
 * This is the example from the documentations, it connects to the bus, and
 * translate the messages beginning with "Hello" by the same message, where
 * "Hello" is replaced by "Bonjour".
 *
 * @author Yannick Jestin <jestin@cena.fr>
 *
 * (c) CENA
 *
 * This program is distributed as is, under the LGPL licence, which should be
 * present in the package.
 *
 */
import fr.dgac.ivy.* ;

class ivyTranslater implements IvyMessageListener {

    private Ivy bus;

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