// Path: remote_controller/app/src/main/java/org/wilds/quadcontroller/app/communication/packet/HelloPacket.java
// public class HelloPacket extends Packet {
// 
//     protected String from;
// 
//     public HelloPacket(String from) {
//         this.setType(TYPE_HELLO);
//         setFrom(from);
//     }
// 
//     public String getFrom() {
//         return from;
//     }
// 
//     protected void setFrom(String from) {
//         this.from = from;
//     }
// 
//     @Override
//     public boolean waitReply() {
//         return true;
//     }
// 
//     @Override
//     public String toString() {
//         return TYPE_HELLO + " " + from;
//     }
// }
// 
// Path: remote_controller/app/src/main/java/org/wilds/quadcontroller/app/communication/packet/Packet.java
// public abstract class Packet {
// 
//     public static final String TYPE_HELLO = "hello";
//     public static final String TYPE_MOTION = "rcinput";
//     public static final String TYPE_HEARTBEAT = "heartbeat";
//     public static final String TYPE_TEST_MOTOR = "tm";
// 
//     protected String type;
// 
//     public String getType() {
//         return type;
//     }
// 
//     protected void setType(String type) {
//         this.type = type;
//     }
// 
//     public boolean waitReply() {
//         return false;
//     }
// 
//     @Override
//     public String toString() {
//         return type;
//     }
// 
//     public byte[] getByte() { return this.toString().getBytes(); }
// 
//     public int getLength() {
//         return this.toString().length();
//     }
// }


import android.util.Log;
import org.wilds.quadcontroller.app.communication.packet.HelloPacket;
import org.wilds.quadcontroller.app.communication.packet.Packet;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
package org.wilds.quadcontroller.app.communication;




/**
 * Created by Wilds on 14/04/2014.
 */
public class UDPProtocol implements Protocol {

    //
    protected DatagramSocket sendSocked;
    protected int sendPort = 58000;
    protected InetAddress sendTo;

    // Listening vars
    /*
    protected Thread listenThread;
    protected int listenPort = 57001;
    protected boolean listening = true;
    */

    protected OnReceiveListener onReceiveListener;

    public UDPProtocol(int sendPort /*, int listenPort*/) {
        this.sendPort = sendPort;
        //this.listenPort = listenPort;

        try {
            sendSocked = new DatagramSocket(sendPort);
        } catch (SocketException e) {
            e.printStackTrace();
        }

//        startListening();
    }

    @Override
    public void setOnReceiveListener(OnReceiveListener listener) {
        this.onReceiveListener = listener;
    }


    /*
    protected void startListening() {
        listenThread = new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    DatagramSocket sock = new DatagramSocket(listenPort);
                    while (listening) {
                        byte[] message = new byte[1500];
                        DatagramPacket p = new DatagramPacket(message, message.length);
                        sock.receive(p);
                        String text = new String(message, 0, p.getLength());

                        //TODO handle quadcopter response at hello packet

                        onReceiveListener.onReceive(text);
                    }
                    sock.close();

                } catch (SocketException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        });
        listenThread.start();
    }
*/

    @Override
// Path: remote_controller/app/src/main/java/org/wilds/quadcontroller/app/communication/packet/HelloPacket.java
// public class HelloPacket extends Packet {
// 
//     protected String from;
// 
//     public HelloPacket(String from) {
//         this.setType(TYPE_HELLO);
//         setFrom(from);
//     }
// 
//     public String getFrom() {
//         return from;
//     }
// 
//     protected void setFrom(String from) {
//         this.from = from;
//     }
// 
//     @Override
//     public boolean waitReply() {
//         return true;
//     }
// 
//     @Override
//     public String toString() {
//         return TYPE_HELLO + " " + from;
//     }
// }
// 
// Path: remote_controller/app/src/main/java/org/wilds/quadcontroller/app/communication/packet/Packet.java
// public abstract class Packet {
// 
//     public static final String TYPE_HELLO = "hello";
//     public static final String TYPE_MOTION = "rcinput";
//     public static final String TYPE_HEARTBEAT = "heartbeat";
//     public static final String TYPE_TEST_MOTOR = "tm";
// 
//     protected String type;
// 
//     public String getType() {
//         return type;
//     }
// 
//     protected void setType(String type) {
//         this.type = type;
//     }
// 
//     public boolean waitReply() {
//         return false;
//     }
// 
//     @Override
//     public String toString() {
//         return type;
//     }
// 
//     public byte[] getByte() { return this.toString().getBytes(); }
// 
//     public int getLength() {
//         return this.toString().length();
//     }
// }


// Path: remote_controller/app/src/main/java/org/wilds/quadcontroller/app/communication/UDPProtocol.java
import android.util.Log;
import org.wilds.quadcontroller.app.communication.packet.HelloPacket;
import org.wilds.quadcontroller.app.communication.packet.Packet;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

package org.wilds.quadcontroller.app.communication;




/**
 * Created by Wilds on 14/04/2014.
 */
public class UDPProtocol implements Protocol {

    //
    protected DatagramSocket sendSocked;
    protected int sendPort = 58000;
    protected InetAddress sendTo;

    // Listening vars
    /*
    protected Thread listenThread;
    protected int listenPort = 57001;
    protected boolean listening = true;
    */

    protected OnReceiveListener onReceiveListener;

    public UDPProtocol(int sendPort /*, int listenPort*/) {
        this.sendPort = sendPort;
        //this.listenPort = listenPort;

        try {
            sendSocked = new DatagramSocket(sendPort);
        } catch (SocketException e) {
            e.printStackTrace();
        }

//        startListening();
    }

    @Override
    public void setOnReceiveListener(OnReceiveListener listener) {
        this.onReceiveListener = listener;
    }


    /*
    protected void startListening() {
        listenThread = new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    DatagramSocket sock = new DatagramSocket(listenPort);
                    while (listening) {
                        byte[] message = new byte[1500];
                        DatagramPacket p = new DatagramPacket(message, message.length);
                        sock.receive(p);
                        String text = new String(message, 0, p.getLength());

                        //TODO handle quadcopter response at hello packet

                        onReceiveListener.onReceive(text);
                    }
                    sock.close();

                } catch (SocketException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        });
        listenThread.start();
    }
*/

    @Override
    public boolean sendPacket(final Packet packet) { // TODO convertire in async task