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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/**
* Ivy java library API tester.
*
* @author Yannick Jestin <mailto:jestin@cena.fr>
*
* (c) CENA
*
* usage: java TestApi
*
*/
import fr.dgac.ivy.*;
class TestApi implements IvyMessageListener, IvyApplicationListener {
public static final String TestApiReadyMsg = "TestAPI ready";
public static final String TestMsg = "Test Message";
private Ivy bus;
private int test=0;
private String domain;
public TestApi(String domain) throws IvyException {
System.out.println("TestApi joining the bus");
bus = new Ivy("TestAPI",TestApiReadyMsg, null);
bus.addApplicationListener(this);
bus.bindMsg("^"+TestMsg+"$",this);
bus.start(this.domain=domain);
bus.sendToSelf(true);
bus.bindMsg("^go$",new Self(domain));
System.out.println("sending go ...");
try { bus.sendMsg("go"); } catch (IvyException ie) { }
System.out.println("go sent");
}
public void receive(IvyClient ic,String[] args) {
String s = "[X] received message";
for (int i=0;i<args.length;i++) s+=": "+args[i];
System.out.println(s);test++;
}
public void connect(IvyClient ic) {
if (ic.getApplicationName().compareTo("Sender")!=0) return;
System.out.println("[X] Sender connected");test++;
}
public void disconnect(IvyClient ic) {
if (ic.getApplicationName().compareTo("Sender")!=0) return;
System.out.println("[X] Sender disconnected");test++;
}
public void directMessage(IvyClient ic,int id,String arg) {
if (id!=1) return;
System.out.println("[X] Direct message received, ID=1");test++;
}
public void die(IvyClient ic,int reason,String msg) {
System.out.println("[X] Die received "+msg);test++;
System.out.println(test+" tests successful, good bye");
System.out.println("TestApi leaving the bus");
bus.stop();
System.out.println("TestApi has left");
}
class Self implements IvyMessageListener {
private String domain;
public Self(String domain) {this.domain=domain;}
public void receive(IvyClient c,String[] args){
System.out.println("[X] received my own go message");test++;
bus.sendToSelf(false);
new Sender(domain) ;
}
}
class Sender implements IvyMessageListener {
private Ivy sbus;
private String domain;
public Sender(String domain) {
try {
System.out.println("starting Sender on domain "+domain);
sbus = new Ivy("Sender","Sender ready", null);
sbus.bindMsg("^"+TestApiReadyMsg+"$",this);
sbus.start(this.domain=domain);
} catch (IvyException ie) {
ie.printStackTrace();
}
}
public void receive(IvyClient c,String[] args) {
try {
sbus.sendMsg(TestMsg);
c.sendDirectMsg(1,"bye bye");
} catch (IvyException ie) {
}
System.out.println("Sender leaving the bus");
sbus.stop();
System.out.println("Sender has left the bus");
new Killer(domain);
}
}
class Killer implements IvyMessageListener {
private Ivy kbus;
public Killer(String domain) {
try {
System.out.println("Killer joining the bus");
kbus = new Ivy("Killer","Killer ready", null);
kbus.bindMsg("^"+TestApiReadyMsg+"$",this);
kbus.start(domain);
} catch (IvyException ie) {
ie.printStackTrace();
}
}
public void receive(IvyClient c,String[] args) {
c.sendDie("bye bye");
System.out.println("Killer leaving the bus");
kbus.stop();
System.out.println("Killer has left the bus");
}
}
public static void main(String[] args) throws IvyException {
new TestApi(Ivy.getDomain(null));
}
}
|