/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.bitcoin.examples;

import com.google.bitcoin.core.*;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.store.MemoryBlockStore;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

import org.jboss.netty.channel.ChannelFuture;

/**
 * Downloads the block given a block hash from the localhost node and prints it out.
 */
public class FetchBlock {
    public static void main(String[] args) throws Exception {
        System.out.println("Connecting to node");
        final NetworkParameters params = NetworkParameters.testNet();

        BlockStore blockStore = new MemoryBlockStore(params);
        BlockChain chain = new BlockChain(params, blockStore);
        PeerGroup peerGroup = new PeerGroup(params, chain);
        peerGroup.start();

        final CountDownLatch latch = new CountDownLatch(1);
        peerGroup.addEventListener(new AbstractPeerEventListener() {
            @Override
            public void onPeerConnected(Peer peer, int peerCount) {
                latch.countDown();
            }
        });
        
        ChannelFuture channelFuture =
            peerGroup.connectTo(new InetSocketAddress(InetAddress.getLocalHost(), params.port));
        latch.await();

        Peer peer = PeerGroup.peerFromChannelFuture(channelFuture);

        Sha256Hash blockHash = new Sha256Hash(args[0]);
        Future<Block> future = peer.getBlock(blockHash);
        System.out.println("Waiting for node to send us the requested block: " + blockHash);
        Block block = future.get();
        System.out.println(block);
        peerGroup.stop();
    }
}

@@ -758,37 +758,59 @@
 re;%0A
-%0A
 import 
-java.net.InetAddress;
+com.google.bitcoin.utils.BriefLogFormatter;%0A
 %0Aimp
@@ -822,30 +822,24 @@
 ava.net.Inet
-Socket
 Address;%0Aimp
@@ -867,99 +867,8 @@
 ent.
-CountDownLatch;%0Aimport java.util.concurrent.Future;%0A%0Aimport org.jboss.netty.channel.Channel
 Futu
@@ -1053,16 +1053,50 @@
 ption %7B%0A
+        BriefLogFormatter.init();%0A
         
@@ -1420,70 +1420,16 @@
 tart
-();%0A%0A        final CountDownLatch latch = new CountDownLatch(1
+AndWait(
 );%0A 
@@ -1439,388 +1439,169 @@
     
-peerGroup.addEventListener(new AbstractPeerEventListener() %7B%0A            @Override%0A            public void onPeerConnected(Peer peer, int peerCount) %7B%0A                latch.countDown();%0A            %7D%0A        %7D);%0A        %0A        ChannelFuture channelFuture =%0A            peerGroup.connectTo(new InetSocketAddress(InetAddress.getLocalHost(), params.port));%0A        latch.awai
+PeerAddress addr = new PeerAddress(InetAddress.getLocalHost(), params.port, 31000);%0A        peerGroup.addAddress(addr);%0A        peerGroup.waitForPeers(1).ge
 t();%0A
-%0A
     
@@ -1616,17 +1616,17 @@
  peer = 
-P
+p
 eerGroup
@@ -1630,43 +1630,33 @@
 oup.
-peerFromChannelFuture(channelFuture
+getConnectedPeers().get(0
 );%0A%0A

Simplify FetchBlock by updating it to the latest APIs.