// Path: src/net/corpwar/lib/corpnet/master/TestMessage.java
// public class TestMessage implements Serializable {
// 
//     public String testMessage = "This is a test message to test NAT";
// }
// 
// Path: src/net/corpwar/lib/corpnet/util/SerializationUtils.java
// public class SerializationUtils {
// 
//     private static SerializationUtils serializationUtils = null;
//     private static final Logger LOG = Logger.getLogger(SerializationUtils.class.getName());
//     private static final ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 
// 
//     private SerializationUtils() {
//     }
// 
//     public static SerializationUtils getInstance() {
//         if (serializationUtils == null) {
//             serializationUtils = new SerializationUtils();
//         }
//         return serializationUtils;
//     }
// 
//     public <T> byte[] serialize(T obj) {
//         ObjectOutputStream oos = null;
//         try {
//             oos = new ObjectOutputStream(baos);
//             oos.writeObject(obj);
//             oos.reset();
//         } catch (IOException e) {
//             LOG.log(Level.SEVERE, "Error IOException", e);
//         } finally {
//             if (oos != null) {
//                 try {
//                     oos.close();
//                 } catch (IOException e) {
//                     LOG.log(Level.SEVERE, "Error IOException", e);
//                 }
//             }
//         }
//         byte[] byteObj = baos.toByteArray();
//         baos.reset();
//         return byteObj;
//     }
// 
//     public <T> T deserialize(byte[] bytes) {
//         T obj = null;
//         ByteArrayInputStream bais = null;
//         ObjectInputStream ois = null;
// 
//         try {
//             bais = new ByteArrayInputStream(bytes);
//             ois = new ObjectInputStream(bais);
//             obj = (T) ois.readObject();
//         } catch (IOException e) {
//             LOG.log(Level.SEVERE, "Error IOException", e);
//         } catch (ClassNotFoundException e) {
//             LOG.log(Level.SEVERE, "Error Class not found", e);
//             return null;
//         } finally {
//             if (ois != null) {
//                 try {
//                     ois.close();
//                 } catch (IOException e) {
//                     LOG.log(Level.SEVERE, "Error IOException", e);
//                 }
//             }
//         }
//         return obj;
//     }
// }


import net.corpwar.lib.corpnet.*;
import net.corpwar.lib.corpnet.master.TestMessage;
import net.corpwar.lib.corpnet.util.SerializationUtils;
import java.util.UUID;
package net.corpwar.lib.corpnet.masterserver;



/**
 * corpnet
 * Created by Ghost on 2016-12-15.
 */
public class PeerNatServerTest implements PeerReceiverListener {

    private PeerToPeer peerToMaster;

    public PeerNatServerTest() {
        peerToMaster = new PeerToPeer();
        peerToMaster.registerPeerListerner(this);
        peerToMaster.startPeer();
        System.out.println("Nat test client started");
        peerToMaster.connectToMasterServer("127.0.0.1", 44444);
        System.out.println("Peer amount: " + peerToMaster.getPeers().size());
        peerToMaster.testNatViaMasterServer();
    }


    @Override
    public void connected(Connection connection) {
        System.out.println("Connected: " + connection.getConnectionId() + " " + connection.getAddress().getHostAddress() + ":" + connection.getPort());
    }

    @Override
    public void receivedMessage(Message message) {
        System.out.println(new String(message.getData()) + "\nPeer amount: " + peerToMaster.getPeers().size());
// Path: src/net/corpwar/lib/corpnet/master/TestMessage.java
// public class TestMessage implements Serializable {
// 
//     public String testMessage = "This is a test message to test NAT";
// }
// 
// Path: src/net/corpwar/lib/corpnet/util/SerializationUtils.java
// public class SerializationUtils {
// 
//     private static SerializationUtils serializationUtils = null;
//     private static final Logger LOG = Logger.getLogger(SerializationUtils.class.getName());
//     private static final ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 
// 
//     private SerializationUtils() {
//     }
// 
//     public static SerializationUtils getInstance() {
//         if (serializationUtils == null) {
//             serializationUtils = new SerializationUtils();
//         }
//         return serializationUtils;
//     }
// 
//     public <T> byte[] serialize(T obj) {
//         ObjectOutputStream oos = null;
//         try {
//             oos = new ObjectOutputStream(baos);
//             oos.writeObject(obj);
//             oos.reset();
//         } catch (IOException e) {
//             LOG.log(Level.SEVERE, "Error IOException", e);
//         } finally {
//             if (oos != null) {
//                 try {
//                     oos.close();
//                 } catch (IOException e) {
//                     LOG.log(Level.SEVERE, "Error IOException", e);
//                 }
//             }
//         }
//         byte[] byteObj = baos.toByteArray();
//         baos.reset();
//         return byteObj;
//     }
// 
//     public <T> T deserialize(byte[] bytes) {
//         T obj = null;
//         ByteArrayInputStream bais = null;
//         ObjectInputStream ois = null;
// 
//         try {
//             bais = new ByteArrayInputStream(bytes);
//             ois = new ObjectInputStream(bais);
//             obj = (T) ois.readObject();
//         } catch (IOException e) {
//             LOG.log(Level.SEVERE, "Error IOException", e);
//         } catch (ClassNotFoundException e) {
//             LOG.log(Level.SEVERE, "Error Class not found", e);
//             return null;
//         } finally {
//             if (ois != null) {
//                 try {
//                     ois.close();
//                 } catch (IOException e) {
//                     LOG.log(Level.SEVERE, "Error IOException", e);
//                 }
//             }
//         }
//         return obj;
//     }
// }


// Path: test/net/corpwar/lib/corpnet/masterserver/PeerNatServerTest.java
import net.corpwar.lib.corpnet.*;
import net.corpwar.lib.corpnet.master.TestMessage;
import net.corpwar.lib.corpnet.util.SerializationUtils;
import java.util.UUID;

package net.corpwar.lib.corpnet.masterserver;



/**
 * corpnet
 * Created by Ghost on 2016-12-15.
 */
public class PeerNatServerTest implements PeerReceiverListener {

    private PeerToPeer peerToMaster;

    public PeerNatServerTest() {
        peerToMaster = new PeerToPeer();
        peerToMaster.registerPeerListerner(this);
        peerToMaster.startPeer();
        System.out.println("Nat test client started");
        peerToMaster.connectToMasterServer("127.0.0.1", 44444);
        System.out.println("Peer amount: " + peerToMaster.getPeers().size());
        peerToMaster.testNatViaMasterServer();
    }


    @Override
    public void connected(Connection connection) {
        System.out.println("Connected: " + connection.getConnectionId() + " " + connection.getAddress().getHostAddress() + ":" + connection.getPort());
    }

    @Override
    public void receivedMessage(Message message) {
        System.out.println(new String(message.getData()) + "\nPeer amount: " + peerToMaster.getPeers().size());
        peerToMaster.getPeers().get(message.getConnectionID()).addToSendQue(SerializationUtils.getInstance().serialize(new TestMessage()), NetworkSendType.PEER_DATA);