// 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;
                    sendSocked.send(new DatagramPacket(packet.getByte(), packet.getLength(), sendTo, sendPort));

                    if (packet.waitReply()) {
                        byte[] message = new byte[1024];
                        DatagramPacket p = new DatagramPacket(message, message.length);
                        sendSocked.receive(p);
                        String text = new String(message, 0, p.getLength());

                        //TODO handle quadcopter response
                        if (onReceiveListener != null)
                            onReceiveListener.onReceive(packet.getType(), text);
                    }
                    //return true;
                } catch (IOException e) {
                    e.printStackTrace();
                    //return false;
                }
            }
        }).start();

        return true;
    }


    @Override
    public boolean searchForQuadcopter() {  // TODO convertire in async task
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
// 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;

                    sendSocked.send(new DatagramPacket(packet.getByte(), packet.getLength(), sendTo, sendPort));

                    if (packet.waitReply()) {
                        byte[] message = new byte[1024];
                        DatagramPacket p = new DatagramPacket(message, message.length);
                        sendSocked.receive(p);
                        String text = new String(message, 0, p.getLength());

                        //TODO handle quadcopter response
                        if (onReceiveListener != null)
                            onReceiveListener.onReceive(packet.getType(), text);
                    }
                    //return true;
                } catch (IOException e) {
                    e.printStackTrace();
                    //return false;
                }
            }
        }).start();

        return true;
    }


    @Override
    public boolean searchForQuadcopter() {  // TODO convertire in async task
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Packet packet = new HelloPacket(InetAddress.getLocalHost().getHostAddress());