package net.tomp2p.test;

import net.tomp2p.connection.Bindings;
import net.tomp2p.futures.BaseFuture;
import net.tomp2p.futures.FutureBootstrap;
import net.tomp2p.futures.FutureDirect;
import net.tomp2p.futures.FutureDiscover;
import net.tomp2p.p2p.Peer;
import net.tomp2p.p2p.PeerBuilder;
import net.tomp2p.peers.Number160;
import net.tomp2p.peers.PeerAddress;
import net.tomp2p.rpc.ObjectDataReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Random;

/**
 * Created by root on 28.02.17.
 */
public class Main {

	private static final Random r = new Random();
	private static Peer peer = null;
	private static final Logger LOG = LoggerFactory.getLogger(Main.class);

	public static void main(String[] args) {

		if (args.length != 0 && args[0].equals("master")) {
			try {
				Bindings b = new Bindings();

				if (args.length > 1) {
					b.addInterface(args[1]);
				} else {
					b.addInterface("eth3");
				}
				peer = new PeerBuilder(new Number160(r)).bindings(b).ports(4000).behindFirewall(true).start();

				peer.objectDataReply(new ObjectDataReply() {
					public Object reply(PeerAddress peerAddress, Object o) throws Exception {
						LOG.error("Message from peer: " + peerAddress.toString() + "\n" + "Message: " + ((String) o));
						return "World";
					}
				});
			} catch (IOException e) {
				LOG.error(e.getStackTrace().toString());
			}
		} else {
			try {
				Bindings b = new Bindings();
				b.addInterface("veth-u1");
				peer = new PeerBuilder(new Number160(r)).bindings(b).ports(4001).start();
				peer.objectDataReply(new ObjectDataReply() {
					public Object reply(PeerAddress peerAddress, Object o) throws Exception {
						LOG.error("Message from peer: " + peerAddress.toString() + "\n" + "Message: " + ((String) o));
						return "";
					}
				});

//				InetAddress address = Inet4Address.getByName("194.230.137.73");
				InetAddress address = Inet4Address.getByName("10.0.0.1");

				FutureDiscover futureDiscover = peer.discover().inetAddress(address).portTCP(4000).discoverTimeoutSec(10).start();
				futureDiscover.awaitUninterruptibly();

				if (futureDiscover.isSuccess()) {
					System.out.println("found that my outside address is " + futureDiscover.peerAddress());
				} else {
					System.out.println("failed " + futureDiscover.peerAddress());
				}
				FutureBootstrap fb = peer.bootstrap().peerAddress(futureDiscover.peerAddress()).start();
				fb.awaitUninterruptibly();

				if (fb.isSuccess()) {
					LOG.error("SUCCESS");
				} else {
					LOG.error("FAIL");
					LOG.error("shutdown initiated");
					BaseFuture fs = peer.shutdown();
					fs.awaitUninterruptibly();
					System.exit(1);
				}

				FutureDirect direct = peer.sendDirect(futureDiscover.peerAddress()).object("HELLO").start();
				direct.awaitUninterruptibly();
				
				if (direct.isSuccess()) {
					LOG.error("SUCCESS INDEED!");
					System.out.println((String) direct.object());
					System.out.println(direct.object().toString());
					System.out.println(direct);
				} else {
					System.out.println("MESSAGE COULD NOT BE TRANSFERRED");
				}
				
				LOG.error("shutdown initiated");
				BaseFuture fs = peer.shutdown();
				fs.awaitUninterruptibly();
				System.exit(0);

			} catch (Exception e) {
				LOG.error(e.getStackTrace().toString());
			}
		}
	}

}


@@ -1548,15 +1548,15 @@
 ce(%22
-veth-u1
+enp0s25
 %22);%0A
@@ -1629,24 +1629,26 @@
 1).start();%0A
+//
 %09%09%09%09peer.obj
@@ -1676,32 +1676,34 @@
 ctDataReply() %7B%0A
+//
 %09%09%09%09%09public Obje
@@ -1757,32 +1757,34 @@
 ows Exception %7B%0A
+//
 %09%09%09%09%09%09LOG.error(
@@ -1860,32 +1860,34 @@
  ((String) o));%0A
+//
 %09%09%09%09%09%09return %22%22;
@@ -1887,23 +1887,27 @@
 urn %22%22;%0A
+//
 %09%09%09%09%09%7D%0A
+//
 %09%09%09%09%7D);%0A
@@ -2030,19 +2030,19 @@
 ame(%2210.
-0.0
+1.1
 .1%22);%0A%0A%09
@@ -2121,41 +2121,16 @@
 port
-TCP
+s
 (4000).
-discoverTimeoutSec(10).
 star
@@ -2385,24 +2385,177 @@
 Address());%0A
+%09%09%09%09%09LOG.error(%22FAIL%22);%0A%09%09%09%09%09LOG.error(%22shutdown initiated%22);%0A%09%09%09%09%09BaseFuture fs = peer.shutdown();%0A%09%09%09%09%09fs.awaitUninterruptibly();%0A%09%09%09%09%09System.exit(1);%0A
 %09%09%09%09%7D%0A%09%09%09%09Fu
@@ -2952,35 +2952,32 @@
 ureDiscover.
-peerAddress
+reporter
 ()).object(%22

fix and running HELLO WORLD