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
|
/**
* Ivy java library API tester.
*
* @author Yannick Jestin <mailto:yannick.jestin@enac.fr>
*
* (c) ENAC
*
* usage: java Unitaire
*
*/
import fr.dgac.ivy.*;
public class SendNow {
Ivy bus;
public SendNow (String args[]) throws IvyException {
bus = new Ivy("ReceiveNow",null,null);
bus.bindMsg("^hop hop",new IvyMessageListener() {
public void receive(IvyClient ic,String args[]){
System.out.println("hop hop received ! quitting");
bus.stop();
}
});
System.out.println("starting receiver");
bus.start(Ivy.getDomainArgs("IvyTest" , args));
}
public static void main(final String[] args) throws IvyException,InterruptedException {
Ivy sendbus = new Ivy("SendNow" , null, null);
new SendNow(args);
//Thread.sleep(10);
// no sleep
try {
System.out.println("starting sender");
sendbus.start(Ivy.getDomainArgs("IvyTest" , args));
System.out.println("sending");
// THIS IS WRONG ON PURPOSE, to test a race condidition on startup
// Correct code to add is
// sendbus.waitForClient("ReceiveNow",0);
int i = sendbus.sendMsg("hop hop");
System.out.println("stopping");
sendbus.stop();
System.out.println("end stopping");
if (i==0) {
// it can fail in the following case:
// SendNow has started, sent "hop hop", and starts closing *before*
// the hanshake is initiated. There's no way of knowing if somebody
// else is joining the bus
// we could add something at the JVM level, but it's no use for inter
// process anyway. 200ms before close + lock is long enough
System.out.println("the Receiver has not received our message, quitting anyway");
System.exit(-1);
}
} catch (IvyException ie) {
System.out.println("Ivy main test error");
ie.printStackTrace();
}
}
}
|