// Path: src/net/corpwar/lib/corpnet/master/RetrievePeerList.java
// public class RetrievePeerList implements Serializable {
// }
// 
// Path: src/net/corpwar/lib/corpnet/util/PeerConnected.java
// public class PeerConnected implements Serializable{
// 
//     public PeerConnected(String ipToPeer, int peerPort, long pingTime) {
//         this.peerPort = peerPort;
//         this.ipToPeer = ipToPeer;
//         this.pingTime = pingTime;
//     }
// 
//     public int peerPort;
// 
//     public String ipToPeer;
// 
//     public long pingTime;
// }
// 
// Path: src/net/corpwar/lib/corpnet/util/PeerList.java
// public class PeerList implements Serializable {
// 
//     public List<PeerConnected> peerConnected = new ArrayList<>();
// 
// 
// 
// }
// 
// 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.RetrievePeerList;
import net.corpwar.lib.corpnet.util.PeerConnected;
import net.corpwar.lib.corpnet.util.PeerList;
import net.corpwar.lib.corpnet.util.SerializationUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.UUID;
/**************************************************************************
 * CorpNet
 * Copyright (C) 2016 Daniel Ekedahl
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **************************************************************************/
package net.corpwar.lib.corpnet.chat;




public class PeerClient {

    private PeerToPeer peer;
    private Random random = new Random();

    public PeerClient() {
        peer = new PeerToPeer();
        peer.startPeer();
        peer.connectToPeer(20000, "127.0.0.1");
        try {
            Connection connection = new Connection(InetAddress.getByName("127.0.0.1"), 20000);
            connection = peer.getPeers().get(connection.getConnectionId());
            connection.addToSendQue(SerializationUtils.getInstance().serialize(new RetrievePeerList()), NetworkSendType.PEER_DATA);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        peer.registerPeerListerner(new PeerReceiverListener() {

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

            @Override
            public void receivedMessage(Message message) {
                System.out.println("receivedMessage " + message.getConnectionID() + " : " + new String(message.getData()));
                if (message.getNetworkSendType() == NetworkSendType.PEER_DATA) {
                    Object object = SerializationUtils.getInstance().deserialize(message.getData());
// Path: src/net/corpwar/lib/corpnet/master/RetrievePeerList.java
// public class RetrievePeerList implements Serializable {
// }
// 
// Path: src/net/corpwar/lib/corpnet/util/PeerConnected.java
// public class PeerConnected implements Serializable{
// 
//     public PeerConnected(String ipToPeer, int peerPort, long pingTime) {
//         this.peerPort = peerPort;
//         this.ipToPeer = ipToPeer;
//         this.pingTime = pingTime;
//     }
// 
//     public int peerPort;
// 
//     public String ipToPeer;
// 
//     public long pingTime;
// }
// 
// Path: src/net/corpwar/lib/corpnet/util/PeerList.java
// public class PeerList implements Serializable {
// 
//     public List<PeerConnected> peerConnected = new ArrayList<>();
// 
// 
// 
// }
// 
// 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/chat/PeerClient.java
import net.corpwar.lib.corpnet.*;
import net.corpwar.lib.corpnet.master.RetrievePeerList;
import net.corpwar.lib.corpnet.util.PeerConnected;
import net.corpwar.lib.corpnet.util.PeerList;
import net.corpwar.lib.corpnet.util.SerializationUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.UUID;

/**************************************************************************
 * CorpNet
 * Copyright (C) 2016 Daniel Ekedahl
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **************************************************************************/
package net.corpwar.lib.corpnet.chat;




public class PeerClient {

    private PeerToPeer peer;
    private Random random = new Random();

    public PeerClient() {
        peer = new PeerToPeer();
        peer.startPeer();
        peer.connectToPeer(20000, "127.0.0.1");
        try {
            Connection connection = new Connection(InetAddress.getByName("127.0.0.1"), 20000);
            connection = peer.getPeers().get(connection.getConnectionId());
            connection.addToSendQue(SerializationUtils.getInstance().serialize(new RetrievePeerList()), NetworkSendType.PEER_DATA);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        peer.registerPeerListerner(new PeerReceiverListener() {

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

            @Override
            public void receivedMessage(Message message) {
                System.out.println("receivedMessage " + message.getConnectionID() + " : " + new String(message.getData()));
                if (message.getNetworkSendType() == NetworkSendType.PEER_DATA) {
                    Object object = SerializationUtils.getInstance().deserialize(message.getData());
                    if (object instanceof PeerList) {