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
|
/**
* 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 {
bus = new Ivy("TestAPI",TestApiReadyMsg, null);
bus.addApplicationListener(this);
bus.bindMsg("^"+TestMsg+"$",this);
bus.start(this.domain=domain);
new Sender(domain) ;
}
public void receive(IvyClient ic,String[] args) {
System.out.println("[X] received message");
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) {
System.out.println("[X] Die received");
test++;
System.out.println(test+ "/5 tests successful, good bye");
bus.stop();
}
class Sender implements IvyMessageListener {
private Ivy sbus;
private String domain;
public Sender(String domain) {
try {
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) {
sbus.sendMsg(TestMsg);
c.sendDirectMsg(1,"bye bye");
sbus.stop();
new Killer(domain);
}
}
class Killer implements IvyMessageListener {
private Ivy kbus;
public Killer(String domain) {
try {
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");
kbus.stop();
}
}
public static void main(String[] args) throws IvyException {
new TestApi(null);
}
}
|