aboutsummaryrefslogtreecommitdiff
path: root/tests/Request.java
blob: 67c30964442aa3a602bc225160cc55adb0129f89 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
 * Another Ivy java library API tester: requests
 *
 * @author  Yannick Jestin <mailto:jestin@cena.fr>
 *
 * (c) CENA
 *
 * usage: java Request
 *
 * Changelog
 *   1.2.8 : first release
 *
 */
import fr.dgac.ivy.*;

class Request {

  public static void main(String[] args) throws IvyException {
    String domain=Ivy.getDomainArgs("RequestTest",args);
    new Request(domain);
  }

  int nb=0;
  private Ivy bus;
  String id;
  static int serial = 0;
  private static final int NBCOMP = 5;
  private static final int dodo = 0;

  public Request(String domain) throws IvyException {
    bus = new Ivy("RequestTest","RequestTest ready", null);
    id=bus.getWBUId();
    bus.bindMsg("^Computer\\d ready",new IvyMessageListener() {
      public void receive(IvyClient ic,String[] args) {
	try { bus.sendMsg("computesum id="+id+" a=3 b=4");} catch (IvyException _ ) { }
      }
    });
    bus.bindMsgOnce("^result id="+id+" value=([0-9]+)",new IvyMessageListener() {
      public void receive(IvyClient ic,String[] args) {
	System.out.println("result received: "+args[0]);
	try {Thread.sleep(1000);} catch (InterruptedException _) { }
	System.exit(0);
      }
    });
    bus.start(domain);
    System.out.println("launching "+NBCOMP+" Computers");
    for (int i=0;i<NBCOMP;i++) new Computer(domain);
  }

  private class Computer implements IvyMessageListener {
    Ivy bus;
    String name;
    public Computer(String domain) throws IvyException {
      name = "Computer"+serial++;
      bus = new Ivy(name,name+" ready",null);
      bus.bindMsg("^computesum id=([^ ]*) a=([0-9]*) b=([0-9]*)",this);
      bus.start(domain);
    }
    public void receive(IvyClient ic,String[] args) {
      String id=args[0];
      int a=Integer.parseInt(args[1]);
      int b=Integer.parseInt(args[2]);
      System.out.println(name+" sending result, id: "+id+", a:"+a+", b:"+b);
      try { bus.sendMsg("result id="+id+" value="+(a+b));}
      catch (IvyException ie) { ie.printStackTrace(); }
    }
  }

}