public Peer createAndAttachRemotePeer() { final Peer peer; try { peer = new PeerMaker(new Number160(rnd)).setPorts(5003).buildAndListen(); } catch (Exception e) { e.printStackTrace(); Assert.fail(); return null; } final FutureBootstrap fb = peer.bootstrapBroadcast(seed.getPeerAddress().portTCP()); fb.awaitUninterruptibly(); peer.discover(fb.getBootstrapTo().iterator().next()); fb.addListener( new BaseFutureListener<BaseFuture>() { @Override public void operationComplete(BaseFuture future) throws Exception { Collection<PeerAddress> addresses = fb.getBootstrapTo(); if (addresses != null && !addresses.isEmpty()) { peer.discover(addresses.iterator().next()).awaitUninterruptibly(); } else { Assert.assertTrue("Unable to boostrap to peers in the network", false); } } @Override public void exceptionCaught(Throwable t) throws Exception { t.fillInStackTrace(); } }); return peer; }
public static void startClient(String ipAddress) throws Exception { Random rnd = new Random(42L); Bindings b = new Bindings(Protocol.IPv4, Inet4Address.getByName("127.0.0.1"), 4001, 4001); // b.addInterface("eth0"); Peer client = new PeerMaker(new Number160(rnd)).setPorts(4001).setBindings(b).makeAndListen(); System.out.println( "Client started and Listening to: " + DiscoverNetworks.discoverInterfaces(b)); System.out.println("address visible to outside is " + client.getPeerAddress()); InetAddress address = Inet4Address.getByName(ipAddress); int masterPort = 4000; PeerAddress pa = new PeerAddress(Number160.ZERO, address, masterPort, masterPort); System.out.println("PeerAddress: " + pa); // Creates a connetion before we discover // client.createPeerConnection(pa, 10000); // Future Discover FutureDiscover futureDiscover = client.discover().setInetAddress(address).setPorts(masterPort).start(); futureDiscover.awaitUninterruptibly(); // Future Bootstrap - slave FutureBootstrap futureBootstrap = client.bootstrap().setInetAddress(address).setPorts(masterPort).start(); futureBootstrap.awaitUninterruptibly(); Collection<PeerAddress> addressList = client.getPeerBean().getPeerMap().getAll(); System.out.println(addressList.size()); if (futureDiscover.isSuccess()) { System.out.println("found that my outside address is " + futureDiscover.getPeerAddress()); } else { System.out.println("failed " + futureDiscover.getFailedReason()); } client.halt(); // Future Bootstrap - master // futureBootstrap = master.bootstrap(masterPA); // futureBootstrap.awaitUninterruptibly(); }
public static void startServer() throws Exception { Random rnd = new Random(43L); Bindings b = new Bindings(Protocol.IPv4, Inet4Address.getByName("127.0.0.1"), 4000, 4000); // b.addInterface("eth0"); Peer master = new PeerMaker(new Number160(rnd)).setPorts(4000).setBindings(b).makeAndListen(); System.out.println("Server started Listening to: " + DiscoverNetworks.discoverInterfaces(b)); System.out.println("address visible to outside is " + master.getPeerAddress()); while (true) { for (PeerAddress pa : master.getPeerBean().getPeerMap().getAll()) { System.out.println("PeerAddress: " + pa); FutureChannelCreator fcc = master.getConnectionBean().getConnectionReservation().reserve(1); fcc.awaitUninterruptibly(); ChannelCreator cc = fcc.getChannelCreator(); // FutureResponse fr1 = master.getHandshakeRPC().pingTCP(pa, cc); // fr1.awaitUninterruptibly(); // if (fr1.isSuccess()) // System.out.println("peer online T:" + pa); // else // System.out.println("offline " + pa); // FutureResponse fr2 = master.getHandshakeRPC().pingUDP(pa, cc); // fr2.awaitUninterruptibly(); master.getConnectionBean().getConnectionReservation().release(cc); // if (fr2.isSuccess()) // System.out.println("peer online U:" + pa); // else // System.out.println("offline " + pa); } Thread.sleep(1500); } }