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 P2PClient() throws Exception { this.localIP = getLocalPeerIP(); this.serviceID = "medical"; this.peerID = Number160.createHash(localIP); this.peer = new Peer(peerID); this.peer.listen(4000, 4000); PeerAddress superAddr = new PeerAddress(Number160.createHash(1), "109.231.77.242", 8888, 8888); FutureDiscover future = this.peer.discover(superAddr); future.awaitUninterruptibly(); FutureBootstrap fb = this.peer.bootstrap(superAddr); fb.awaitUninterruptibly(); peer.discover(fb.getBootstrapTo().iterator().next()).awaitUninterruptibly(); }
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(); }