@After public void cleanup() { timer1.cancel(); timer2.cancel(); seed.shutdown(); peer.shutdown(); }
public FutureBootstrap start() { if (peer.isShutdown()) { return FUTURE_BOOTSTRAP_SHUTDOWN; } if (routingConfiguration == null) { routingConfiguration = new RoutingConfiguration(5, 10, 2); } if (requestP2PConfiguration == null) { int size = peer.getPeerBean().peerMap().size() + 1; requestP2PConfiguration = new RequestP2PConfiguration(Math.min(size, 3), 5, 3); } // if (broadcast) { return broadcast(); } if (peerAddress == null && inetAddress != null && bootstrapTo == null) { peerAddress = new PeerAddress(Number160.ZERO, inetAddress, portTCP, portUDP); return bootstrapPing(peerAddress); } else if (peerAddress != null && bootstrapTo == null) { bootstrapTo = new ArrayList<PeerAddress>(1); bootstrapTo.add(peerAddress); return bootstrap(); } else if (bootstrapTo != null) { return bootstrap(); } else { return FUTURE_BOOTSTRAP_NO_ADDRESS; } }
public static void main(String[] args) throws Exception { Peer master = null; try { Peer[] peers = ExampleUtils.createAndAttachNodes(100, 4001); master = peers[0]; MyPeer myPeer = new MyPeer(master); ExampleUtils.bootstrap(peers); myPeer .put( "This is my location key", "This is my domain", "This is my content key", "And here comes the data") .awaitUninterruptibly(); FutureDHT futureDHT = myPeer.get("This is my location key", "This is my domain", "This is my content key"); futureDHT.awaitUninterruptibly(); System.err.println(futureDHT.getFailedReason()); Map<Number160, Data> map = futureDHT.getDataMap(); for (Data data : map.values()) { MyData myData = (MyData) data.getObject(); System.out.println( "key: " + myData.getKey() + ", domain: " + myData.getDomain() + ", content: " + myData.getContent() + ", data: " + myData.getData()); } } finally { master.halt(); } }
/** * Pings a peer. * * @param peerAddress The peer address of the remote peer. * @param isUDP Set to true if UDP should be used, false for TCP. * @return The future response */ public FutureResponse ping(PeerAddress peerAddress, final boolean isUDP) { final RequestHandler<FutureResponse> request = peer.pingRPC().ping(peerAddress, connectionConfiguration); if (isUDP) { FutureChannelCreator fcc = peer.connectionBean().reservation().create(1, 0); fcc.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isSuccess()) { FutureResponse futureResponse = request.sendUDP(future.channelCreator()); Utils.addReleaseListener(future.channelCreator(), futureResponse); } else { request.futureResponse().failed(future); } } }); } else { FutureChannelCreator fcc = peer.connectionBean().reservation().create(0, 1); fcc.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isSuccess()) { FutureResponse futureResponse = request.sendTCP(future.channelCreator()); Utils.addReleaseListener(future.channelCreator(), futureResponse); } else { request.futureResponse().failed(future); } } }); } return request.futureResponse(); }
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; }
private FutureBootstrap bootstrap() { final FutureWrappedBootstrap<FutureDone<Pair<FutureRouting, FutureRouting>>> result = new FutureWrappedBootstrap<FutureDone<Pair<FutureRouting, FutureRouting>>>(); result.setBootstrapTo(bootstrapTo); int conn = Math.max(routingConfiguration.getParallel(), requestP2PConfiguration.getParallel()); FutureChannelCreator fcc = peer.getConnectionBean().reservation().create(conn, 0); fcc.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator futureChannelCreator) throws Exception { if (futureChannelCreator.isSuccess()) { RoutingBuilder routingBuilder = createBuilder(requestP2PConfiguration, routingConfiguration); FutureDone<Pair<FutureRouting, FutureRouting>> futureBootstrap = peer.getDistributedRouting() .bootstrap( bootstrapTo, routingBuilder, futureChannelCreator.getChannelCreator()); Utils.addReleaseListener(futureChannelCreator.getChannelCreator(), futureBootstrap); result.waitFor(futureBootstrap); } else { result.setFailed(futureChannelCreator); } } }); return result; }
private String[] get(Number160 name) throws ClassNotFoundException, IOException { FutureDHT futureDHT = peer.get(name); futureDHT.awaitUninterruptibly(); if (futureDHT.isSuccess()) { return (String[]) futureDHT.getData().values().iterator().next().getObject(); } else return null; }
private String get(String name) throws ClassNotFoundException, IOException { FutureDHT futureDHT = peer.get(Number160.createHash(name)); futureDHT.awaitUninterruptibly(); if (futureDHT.isSuccess()) { return futureDHT.getData().values().iterator().next().getObject().toString(); } else return "Not Found"; }
public BaseFuture start() { if (peer.isShutdown()) { return FUTURE_PING_SHUTDOWN; } if (connectionConfiguration == null) { connectionConfiguration = new DefaultConnectionConfiguration(); } if (broadcast) { return pingBroadcast(port); } else { if (peerAddress != null) { if (tcpPing) { return ping(peerAddress, false); } else { return ping(peerAddress, true); } } else if (inetAddress != null) { if (tcpPing) { return ping(new InetSocketAddress(inetAddress, port), Number160.ZERO, false); } else { return ping(new InetSocketAddress(inetAddress, port), Number160.ZERO, true); } } else if (peerConnection != null) { return pingPeerConnection(peerConnection); } else { throw new IllegalArgumentException( "cannot ping, need to know peer address or inet address"); } } }
private FutureWrappedBootstrap<FutureBootstrap> broadcast() { final FutureWrappedBootstrap<FutureBootstrap> result = new FutureWrappedBootstrap<FutureBootstrap>(); // limit after @SuppressWarnings("unchecked") final FutureLateJoin<FutureResponse> tmp = (FutureLateJoin<FutureResponse>) peer.ping().setBroadcast().setPort(portUDP).start(); tmp.addListener( new BaseFutureAdapter<FutureLateJoin<FutureResponse>>() { @Override public void operationComplete(final FutureLateJoin<FutureResponse> future) throws Exception { if (future.isSuccess()) { FutureResponse futureResponse = future.getLastSuceessFuture(); if (futureResponse == null) { result.setFailed("no futures found", future); return; } if (bootstrapTo != null && bootstrapTo.size() > 0) { logger.info( "you added peers to bootstrapTo. However with broadcast we found our own peers."); } peerAddress = futureResponse.getResponse().getSender(); bootstrapTo = new ArrayList<PeerAddress>(1); bootstrapTo.add(peerAddress); result.setBootstrapTo(bootstrapTo); result.waitFor(bootstrap()); } else { result.setFailed("could not reach anyone with the broadcast", future); } } }); return result; }
/** * Message passer for sending messages regarding tasks. OBS! Only ONE Passer may be present for a * Peer. * * @param peer This peer * @param client Client to put and get results * @param falseResults * @param pool */ public TaskPasserDeny( Peer peer, NetworkInterface client, Map<String, byte[]> falseResults, ExecutorService pool) { super(peer); this.falseResults = falseResults; this.pool = pool; this.myWorkerID = new WorkerID(peer.getPeerBean().getKeyPair().getPublic()); this.client = client; }
@SuppressWarnings("unchecked") private Vector<Number160> getPeerList(String name) throws ClassNotFoundException, IOException { FutureDHT futureDHT = peer.get(Number160.createHash(name)); futureDHT.awaitUninterruptibly(); if (futureDHT.isSuccess()) { return (Vector<Number160>) futureDHT.getData().values().iterator().next().getObject(); } else return null; }
protected void preBuild(String name) { if (domainKey == null) { domainKey = DEFAULT_DOMAIN; } if (routingConfiguration == null) { routingConfiguration = new RoutingConfiguration(5, 10, 2); } if (requestP2PConfiguration == null) { requestP2PConfiguration = new RequestP2PConfiguration(3, 5, 3); } int size = peer.getPeerBean().peerMap().size() + 1; requestP2PConfiguration = requestP2PConfiguration.adjustMinimumResult(size); if (futureChannelCreator == null) { futureChannelCreator = peer.getConnectionBean() .reservation() .create(routingConfiguration, requestP2PConfiguration, this); } }
FutureLateJoin<FutureResponse> pingBroadcast(final int port) { final Bindings bindings = peer.connectionBean().sender().channelClientConfiguration().externalBindings(); final int size = bindings.broadcastAddresses().size(); final FutureLateJoin<FutureResponse> futureLateJoin = new FutureLateJoin<FutureResponse>(size, 1); if (size > 0) { FutureChannelCreator fcc = peer.connectionBean().reservation().create(size, 0); fcc.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(FutureChannelCreator future) throws Exception { if (future.isSuccess()) { Utils.addReleaseListener(future.channelCreator(), futureLateJoin); for (int i = 0; i < size; i++) { final InetAddress broadcastAddress = bindings.broadcastAddresses().get(i); final PeerAddress peerAddress = new PeerAddress(Number160.ZERO, broadcastAddress, port, port); FutureResponse validBroadcast = peer.pingRPC() .pingBroadcastUDP( peerAddress, future.channelCreator(), connectionConfiguration); if (!futureLateJoin.add(validBroadcast)) { // the latejoin future is fininshed if the add returns false break; } } } else { futureLateJoin.failed(future); } } }); } else { futureLateJoin.failed("No broadcast address found. Cannot ping nothing"); } return futureLateJoin; }
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(); }
private FutureDHT put(String key, String domain, String content, String data) throws IOException { Number160 locationKey = Number160.createHash(key); Number160 domainKey = Number160.createHash(domain); Number160 contentKey = Number160.createHash(content); MyData myData = new MyData(); myData.setKey(key); myData.setDomain(domain); myData.setContent(content); myData.setData(data); return peer.put(locationKey) .setDomainKey(domainKey) .setData(contentKey, new Data(myData)) .start(); }
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); } }
private void add(Peer peer, byte[] key, byte[] value) throws InterruptedException { RoutingConfiguration rc = new RoutingConfiguration(1, 0, 10, 1); RequestP2PConfiguration pc = new RequestP2PConfiguration(3, 5, 0); ConfigurationStore cs = Configurations.defaultStoreConfiguration(); cs.setRequestP2PConfiguration(pc); cs.setRoutingConfiguration(rc); Data data = new Data(value); data.setTTLSeconds(3); peer.add(new Number160(key), data, cs) .addListener( new BaseFutureAdapter<FutureDHT>() { @Override public void operationComplete(final FutureDHT future) throws Exception { System.out.println(future.getRawKeys()); } }); }
@Override public void run() { for (final byte[] key : keyValueStore.keySet()) { peer.getAll(new Number160(key)) .addListener( new BaseFutureAdapter<FutureDHT>() { @Override public void operationComplete(FutureDHT future) throws Exception { if (!future.isSuccess()) { testSuccess = false; System.out.println("getAll failed " + future.getFailedReason()); } else { System.out.println("getAll result: " + future.getDataMap().size()); } latch.countDown(); } }); } }
public FutureResponse pingPeerConnection(final PeerConnection peerConnection) { final RequestHandler<FutureResponse> request = peer.pingRPC().ping(peerConnection.remotePeer(), connectionConfiguration); FutureChannelCreator futureChannelCreator = peerConnection.acquire(request.futureResponse()); futureChannelCreator.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(FutureChannelCreator future) throws Exception { if (future.isSuccess()) { request.futureResponse().request().keepAlive(true); request.sendTCP(peerConnection); } else { request.futureResponse().failed(future); } } }); return request.futureResponse(); }
/** * Opens a new peer connection to the receiver and sends the message through it. * * @param peer * @param message * @param config * @return */ public static FutureResponse connectAndSend(final Peer peer, final Message message) { final FutureResponse futureResponse = new FutureResponse(message); final FuturePeerConnection fpc = peer.createPeerConnection(message.recipient()); fpc.addListener( new BaseFutureAdapter<FuturePeerConnection>() { public void operationComplete(final FuturePeerConnection futurePeerConnection) throws Exception { if (futurePeerConnection.isSuccess()) { // successfully created a connection to the other peer final PeerConnection peerConnection = futurePeerConnection.object(); // send the message send(peerConnection, peer.peerBean(), peer.connectionBean(), futureResponse); } else { futureResponse.failed(fpc); } } }); return futureResponse; }
private FutureWrappedBootstrap<FutureBootstrap> bootstrapPing(PeerAddress address) { final FutureWrappedBootstrap<FutureBootstrap> result = new FutureWrappedBootstrap<FutureBootstrap>(); final FutureResponse tmp = (FutureResponse) peer.ping().setPeerAddress(address).setTcpPing().start(); tmp.addListener( new BaseFutureAdapter<FutureResponse>() { @Override public void operationComplete(final FutureResponse future) throws Exception { if (future.isSuccess()) { peerAddress = future.getResponse().getSender(); bootstrapTo = new ArrayList<PeerAddress>(1); bootstrapTo.add(peerAddress); result.setBootstrapTo(bootstrapTo); result.waitFor(bootstrap()); } else { result.setFailed("could not reach anyone with bootstrap"); } } }); return result; }
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(); }
private FutureDHT get(String key, String domain, String content) { Number160 locationKey = Number160.createHash(key); Number160 domainKey = Number160.createHash(domain); Number160 contentKey = Number160.createHash(content); return peer.get(locationKey).setDomainKey(domainKey).setContentKey(contentKey).start(); }
public FutureDirect start() { if (peer.isShutdown()) { return FUTURE_REQUEST_SHUTDOWN; } final boolean keepAlive; final PeerAddress remotePeer; if (recipientAddress != null && recipientConnection == null) { keepAlive = false; remotePeer = recipientAddress; } else if (recipientAddress == null && recipientConnection != null) { keepAlive = true; remotePeer = recipientConnection.remotePeer(); } else if (peerConnection != null) { keepAlive = true; remotePeer = peerConnection.remotePeer(); } else { throw new IllegalArgumentException("either remotePeer or connection has to be set"); } if (futureChannelCreator == null) { futureChannelCreator = peer.connectionBean().reservation().create(isForceUDP() ? 1 : 0, isForceUDP() ? 0 : 1); } final RequestHandler<FutureResponse> request = peer.directDataRPC().sendInternal(remotePeer, this); if (keepAlive) { if (peerConnection != null) { sendDirectRequest(request, peerConnection); } else { recipientConnection.addListener( new BaseFutureAdapter<FuturePeerConnection>() { @Override public void operationComplete(final FuturePeerConnection future) throws Exception { if (future.isSuccess()) { sendDirectRequest(request, future.peerConnection()); } else { request.futureResponse().failed("Could not acquire channel (1)", future); } } }); } } else { futureChannelCreator.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isSuccess()) { final FutureResponse futureResponse = request.sendTCP(future.channelCreator()); Utils.addReleaseListener(future.channelCreator(), futureResponse); } else { request.futureResponse().failed("could not create channel", future); } } }); } return new FutureDirect(request.futureResponse()); }
/** * @return Set to true if the message should be signed. For protecting an entry, this needs to be * set to true. */ public SendDirectBuilder sign() { this.keyPair = peer.peerBean().keyPair(); return this; }
private void store(String key, String val) throws IOException { peer.put(Number160.createHash(key), new Data(val)).awaitUninterruptibly(); }
private void store(Number160 key, String[] vals) throws IOException { peer.put(key, new Data(vals)).awaitUninterruptibly(); }