public void run() { PeerDiscovery peerDiscovery = new DnsDiscovery(NetworkParameters.prodNet()); try { while (true) { InetSocketAddress addresses[] = peerDiscovery.getPeers(); // InetSocketAddress addresses[]={new InetSocketAddress("localhost",8333)}; System.out.println("peers " + addresses.length); NetworkConnection conn; Peer peer; System.out.println("connecting to peers"); for (InetSocketAddress a : addresses) { try { System.out.println("Trying " + a); conn = new NetworkConnection( a.getAddress(), Bitten.networkParameters, blockChain.getChainHead().getHeight(), 3000); System.out.println("after network connection"); peer = new Peer(Bitten.networkParameters, conn, blockChain); System.out.println("starting chain download"); peer.start(); peer.startBlockChainDownload(); while (true) { if (!peer.running()) { System.out.println("peer stopped!"); break; } sleep(1000); } } catch (IOException e) { System.out.println(e); continue; } catch (ProtocolException e) { continue; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); continue; } } } } catch (PeerDiscoveryException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
protected InboundMessageQueuer connect(Peer peer, VersionMessage versionMessage) throws Exception { checkArgument(versionMessage.hasBlockChain()); final AtomicBoolean doneConnecting = new AtomicBoolean(false); final Thread thisThread = Thread.currentThread(); peer.addEventListener( new AbstractPeerEventListener() { @Override public void onPeerDisconnected(Peer p, int peerCount) { synchronized (doneConnecting) { if (!doneConnecting.get()) thisThread.interrupt(); } } }); if (clientType == ClientType.NIO_CLIENT_MANAGER || clientType == ClientType.BLOCKING_CLIENT_MANAGER) channels.openConnection(new InetSocketAddress("127.0.0.1", 2000), peer); else if (clientType == ClientType.NIO_CLIENT) new NioClient(new InetSocketAddress("127.0.0.1", 2000), peer, 100); else if (clientType == ClientType.BLOCKING_CLIENT) new BlockingClient( new InetSocketAddress("127.0.0.1", 2000), peer, 100, SocketFactory.getDefault(), null); else throw new RuntimeException(); // Claim we are connected to a different IP that what we really are, so tx confidence // broadcastBy sets work InboundMessageQueuer writeTarget = newPeerWriteTargetQueue.take(); writeTarget.peer = peer; // Complete handshake with the peer - send/receive version(ack)s, receive bloom filter writeTarget.sendMessage(versionMessage); writeTarget.sendMessage(new VersionAck()); try { checkState(writeTarget.nextMessageBlocking() instanceof VersionMessage); checkState(writeTarget.nextMessageBlocking() instanceof VersionAck); synchronized (doneConnecting) { doneConnecting.set(true); } Thread.interrupted(); // Clear interrupted bit in case it was set before we got into the CS } catch (InterruptedException e) { // We were disconnected before we got back version/verack } return writeTarget; }
protected void closePeer(Peer peer) throws Exception { peer.close(); }