*/
@Test
public void testRelaySendDirect() throws Exception {
final Random rnd = new Random(42);
final int nrOfNodes = 100;
Peer master = null;
Peer unreachablePeer1 = null;
Peer unreachablePeer2 = null;
try {
// setup test peers
Peer[] peers = UtilsNAT.createNodes(nrOfNodes, rnd, 4001);
master = peers[0];
UtilsNAT.perfectRouting(peers);
for (Peer peer : peers) {
new PeerBuilderNAT(peer).bufferConfiguration(androidConfig).start();
}
// Test setting up relay peers
unreachablePeer1 = new PeerBuilder(Number160.createHash(rnd.nextInt())).ports(13337).start();
PeerNAT uNat1 = new PeerBuilderNAT(unreachablePeer1).relayType(relayType).gcmServerCredentials(gcmServerCredentials).start();
FutureRelayNAT fbn = uNat1.startRelay(master.peerAddress());
fbn.awaitUninterruptibly();
Assert.assertTrue(fbn.isSuccess());
mockGCM(peers, uNat1);
System.out.print("Send direct message to unreachable peer " + unreachablePeer1.peerAddress());
final String request = "Hello ";
final String response = "World!";
final AtomicBoolean test1 = new AtomicBoolean(false);
final AtomicBoolean test2 = new AtomicBoolean(false);
//final Peer unr = unreachablePeer;
unreachablePeer1.objectDataReply(new ObjectDataReply() {
public Object reply(PeerAddress sender, Object obj) throws Exception {
test1.set(obj.equals(request));
Assert.assertEquals(request.toString(), request);
test2.set(sender.inetAddress().toString().contains("0.0.0.0"));
System.err.println("Got sender:"+sender);
//this is too late here, so we cannot test this here
//Collection<PeerSocketAddress> list = new ArrayList<PeerSocketAddress>();
//list.add(new PeerSocketAddress(InetAddress.getByName("101.101.101.101"), 101, 101));
//unr.peerBean().serverPeerAddress(unr.peerBean().serverPeerAddress().changePeerSocketAddresses(list));
return response;
}
});
unreachablePeer2 = new PeerBuilder(Number160.createHash(rnd.nextInt())).ports(13338).start();
PeerNAT uNat2 = new PeerBuilderNAT(unreachablePeer2).relayType(relayType).gcmServerCredentials(gcmServerCredentials).start();
fbn = uNat2.startRelay(peers[42].peerAddress());
fbn.awaitUninterruptibly();
Assert.assertTrue(fbn.isSuccess());
mockGCM(peers, uNat2);
//prevent rcon
Collection<PeerSocketAddress> list = unreachablePeer2.peerBean().serverPeerAddress().peerSocketAddresses();
if(list.size() >= relayType.maxRelayCount()) {
Iterator<PeerSocketAddress> iterator = list.iterator();
iterator.next();
iterator.remove();
}
list.add(new PeerSocketAddress(InetAddress.getByName("10.10.10.10"), 10, 10));
unreachablePeer2.peerBean().serverPeerAddress(unreachablePeer2.peerBean().serverPeerAddress().changePeerSocketAddresses(list));
System.err.println("unreachablePeer1: " + unreachablePeer1.peerAddress());
System.err.println("unreachablePeer2: "+unreachablePeer2.peerAddress());
FutureDirect fd = unreachablePeer2.sendDirect(unreachablePeer1.peerAddress()).object(request).start().awaitUninterruptibly();
System.err.println("got msg from: "+fd.futureResponse().responseMessage().sender());
Assert.assertEquals(response, fd.object());
//make sure we did not receive it from the unreachable peer with port 13337
//System.err.println(fd.getWrappedFuture());
//TODO: this case is true for relay
//Assert.assertEquals(fd.wrappedFuture().responseMessage().senderSocket().getPort(), 4001);
//TODO: this case is true for rcon
Assert.assertEquals(unreachablePeer1.peerID(), fd.wrappedFuture().responseMessage().sender().peerId());
Assert.assertTrue(test1.get());
Assert.assertFalse(test2.get());
Assert.assertEquals(relayType.maxRelayCount(), fd.futureResponse().responseMessage().sender().peerSocketAddresses().size());
} finally {
if (unreachablePeer1 != null) {
unreachablePeer1.shutdown().await();
}
if (unreachablePeer2 != null) {
unreachablePeer2.shutdown().await();
}
if (master != null) {
master.shutdown().await();
}
}