final Peer source = new Peer(Inet4Address.getLocalHost(), (short) 5555);
final Peer dest = new Peer(Inet4Address.getLocalHost(), (short) 5556);
final MessageType ping = DMT.ping;
final MessageType pong = DMT.pong;
final UdpSocketManager sourceUSM = new UdpSocketManager((short) 5555);
final UdpSocketManager destUSM = new UdpSocketManager((short) 5556);
ping.addField("id", Integer.class);
pong.addField("id", Integer.class);
// Sender
Misc.startThread(new Runnable() {
public void run() {
int id = 0;
for(int i=0;i<15;i++) {
Message p = new Message(ping);
p.set("id", id++);
p.set(DMT.EXTERNAL_ADDRESS, dest);
System.out.print("Sender: Ping sent... ");
sourceUSM.send(dest, p);
Message r = sourceUSM.waitFor(MessageFilter.create(10000, pong).setField("id",
new Integer(id - 1)));
if (r != null) {
_pongcount++;
synchronized (lock) {
lock.notifyAll();
}
System.out.println("Sender: Pong received.");
} else {
System.out.println("Timed out");
}
}
}
});
// Receiver
for (int i = 0; i < 15; i++) {
Message p = destUSM.waitFor(MessageFilter.create(10000, ping));
if (p != null) {
_pingcount++;
System.err.println("Receiver: Ping " + p.getInt("id") + " received. Sending pong");
Message po = new Message(pong);
po.set("id", p.getInt("id"));
po.set(DMT.EXTERNAL_ADDRESS, source);
destUSM.send(source, po);
}
}
assertEquals(15, _pingcount);
synchronized(lock) {
lock.wait(2000);