How to manually disconnect a peer from peergroup?

1

1

Here is my code.

    params = TestNet3Params.get();
    blockStore = new MemoryBlockStore(params);

    chain = new BlockChain(params, blockStore);
    chain.addWallet(newwallet);

    final PeerGroup peerGroup = new PeerGroup(params, chain);
    peerGroup.setUserAgent("PeerMonitor", "1.0");
    peerGroup.setMaxConnections(1);
    peerGroup.addAddress(new PeerAddress(InetAddresses.forString("192.168.1.253"), 18333));

    peerGroup.addEventListener(new AbstractPeerEventListener() {
        @Override
        public void onPeerDisconnected(Peer peer, int peerCount) {
            System.out.println("hello "+peer.getAddress().getAddr().getHostAddress());
            peerGroup.addPeerDiscovery(new DnsDiscovery(params)); 
            System.out.println("DNS Added...!!");

        }
    });

    peerGroup.startAsync();

    peerGroup.downloadBlockChain();
    System.out.println("Full Downloaded...!!");

    List<Peer> peers=peerGroup.getConnectedPeers();


    System.out.println(peers);
    for (Peer peer : peers) {
        if(peer.getAddress().getAddr().getHostName().equals("192.168.1.253"))
        {
            System.out.println("checking peers...!!");
            break;
        }
    }
    System.out.println("going to stop...!!");

I want to stop the local peer and switch to the DNS when the downloading gets completed.

Ankit Sharma

Posted 2015-02-02T09:27:35.710

Reputation: 11

Answers

1

Simply:

peer.close();

Peer.java extends PeerSocketHandler.java, which is the class that defines the close() method, so maybe that's why you weren't seeing it. It would also probably be easier to find if it were called disconnect().

morsecoder

Posted 2015-02-02T09:27:35.710

Reputation: 12 624

I have called peer.close() and it triggers onDisconnected() event but and in this event I am adding DNS to the peerGroup but despite of adding DNS it is still looking for peer. I want to completely switch from peer to DNSAnkit Sharma 2015-02-03T08:01:27.320

@AnkitSharma, What do you mean by adding DNS? Do you care which peers you are connected to? And why can you not just add it from the start, why do you have to do it after a peer is disconnected?morsecoder 2015-02-03T15:47:21.583

I want to download the bulk data from the bitcoin-qt for the sake of speed as when I download blockchain from DNS from the start it takes too much time. So when all the data has been downloaded from the local peer than I want to switch to the DNSAnkit Sharma 2015-02-04T06:24:26.420