Exemplo n.º 1
0
 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();
   }
 }
Exemplo n.º 2
0
  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();

  }