// Path: localnet/src/main/java/dev/jokr/localnet/models/Payload.java
// public class Payload<T> implements Serializable {
// 
//     private T payload;
// 
//     public Payload(T payload) {
//         this.payload = payload;
//     }
// 
//     public T getPayload() {
//         return payload;
//     }
// 
// 
// }
// 
// Path: localnet/src/main/java/dev/jokr/localnet/models/RegisterMessage.java
// public class RegisterMessage implements Serializable {
// 
//     private Payload<?> payload;
//     private String ip;
//     private int port;
// 
//     public RegisterMessage(Payload<?> payload, String ip, int port) {
//         this.payload = payload;
//         this.ip = ip;
//         this.port = port;
//     }
// 
//     public String getIp() {
//         return ip;
//     }
// 
//     public int getPort() {
//         return port;
//     }
// 
//     public Payload<?> getPayload() {
//         return payload;
//     }
// }
// 
// Path: localnet/src/main/java/dev/jokr/localnet/models/SessionMessage.java
// public class SessionMessage implements Serializable {
// 
//     public static final int NONE = 0;
//     public static final int START = 1;
//     public static final int END = 2;
// 
//     private Payload<?> payload;
//     private int signal;
// 
// 
//     public SessionMessage(Payload<?> payload) {
//         this.payload = payload;
//         this.signal = NONE;
//     }
// 
//     public SessionMessage(Payload<?> payload, int signal) {
//         this.payload = payload;
//         this.signal = signal;
//     }
// 
//     public int getSignal() {
//         return signal;
//     }
// 
//     public Payload<?> getPayload() {
//         return payload;
//     }
// }


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import dev.jokr.localnet.models.Payload;
import dev.jokr.localnet.models.RegisterMessage;
import dev.jokr.localnet.models.SessionMessage;
package dev.jokr.localnet;



/**
 * Created by JoKr on 8/28/2016.
 */
public class LocalServer {
    /*
     * Class for local server. You need one device to act as a server.
     * With init() server is started in discovery mode.
     * To end discovery mode and start session, call setSession(Class c). You will need to pass
     * implementation abstract class LocalSession which is main class for running session logic.
     */
    public static final String UI_EVENT = "ui_event";
    public static final String CONNECTED_CLIENT = "conn_client";

    private Context context;
    private OnUiEventReceiver receiver;

    public LocalServer(Context context) {
        this.context = context;
        registerMessageBroadcastReceiver();
    }

    /*
     * This method will start server in discovery mode. Clients can join using LocalClient.
     */
    public void init() {
        Intent i = new Intent(context, ServerService.class);
        context.startService(i);
    }

    /*
     * This method is used to end discovery mode and start session. It receives instance of
     * LocalSession class as a Class parameter.
     * LocalServer will instantiate it in service and call onCreate method in it.
     */
    public void setSession(Class c) {
        sendSessionData(c, null);
    }

    /*
     * Same as setSession, but as you can't use constructor, you can use this class to pass bundle
     * of arguments to your LocalSession implementation. You will receive this bundle in onCreate
     * in your LocalSession implementation.
     */
    public void setSession(Class c, Bundle b) {
        sendSessionData(c, b);
    }


    /*
     * This will stop service. Make sure not to call anything on this class after shutdown.
     */
    public void shutdown() {
        Intent i = new Intent(context, ServerService.class);
        context.stopService(i);
//        i.putExtra(ServerService.ACTION, ServerService.STOP);
//        context.startService(i);
    }

    /*
     * This method is used to send data to your LocalSession implementation.
     */
// Path: localnet/src/main/java/dev/jokr/localnet/models/Payload.java
// public class Payload<T> implements Serializable {
// 
//     private T payload;
// 
//     public Payload(T payload) {
//         this.payload = payload;
//     }
// 
//     public T getPayload() {
//         return payload;
//     }
// 
// 
// }
// 
// Path: localnet/src/main/java/dev/jokr/localnet/models/RegisterMessage.java
// public class RegisterMessage implements Serializable {
// 
//     private Payload<?> payload;
//     private String ip;
//     private int port;
// 
//     public RegisterMessage(Payload<?> payload, String ip, int port) {
//         this.payload = payload;
//         this.ip = ip;
//         this.port = port;
//     }
// 
//     public String getIp() {
//         return ip;
//     }
// 
//     public int getPort() {
//         return port;
//     }
// 
//     public Payload<?> getPayload() {
//         return payload;
//     }
// }
// 
// Path: localnet/src/main/java/dev/jokr/localnet/models/SessionMessage.java
// public class SessionMessage implements Serializable {
// 
//     public static final int NONE = 0;
//     public static final int START = 1;
//     public static final int END = 2;
// 
//     private Payload<?> payload;
//     private int signal;
// 
// 
//     public SessionMessage(Payload<?> payload) {
//         this.payload = payload;
//         this.signal = NONE;
//     }
// 
//     public SessionMessage(Payload<?> payload, int signal) {
//         this.payload = payload;
//         this.signal = signal;
//     }
// 
//     public int getSignal() {
//         return signal;
//     }
// 
//     public Payload<?> getPayload() {
//         return payload;
//     }
// }


// Path: localnet/src/main/java/dev/jokr/localnet/LocalServer.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import dev.jokr.localnet.models.Payload;
import dev.jokr.localnet.models.RegisterMessage;
import dev.jokr.localnet.models.SessionMessage;

package dev.jokr.localnet;



/**
 * Created by JoKr on 8/28/2016.
 */
public class LocalServer {
    /*
     * Class for local server. You need one device to act as a server.
     * With init() server is started in discovery mode.
     * To end discovery mode and start session, call setSession(Class c). You will need to pass
     * implementation abstract class LocalSession which is main class for running session logic.
     */
    public static final String UI_EVENT = "ui_event";
    public static final String CONNECTED_CLIENT = "conn_client";

    private Context context;
    private OnUiEventReceiver receiver;

    public LocalServer(Context context) {
        this.context = context;
        registerMessageBroadcastReceiver();
    }

    /*
     * This method will start server in discovery mode. Clients can join using LocalClient.
     */
    public void init() {
        Intent i = new Intent(context, ServerService.class);
        context.startService(i);
    }

    /*
     * This method is used to end discovery mode and start session. It receives instance of
     * LocalSession class as a Class parameter.
     * LocalServer will instantiate it in service and call onCreate method in it.
     */
    public void setSession(Class c) {
        sendSessionData(c, null);
    }

    /*
     * Same as setSession, but as you can't use constructor, you can use this class to pass bundle
     * of arguments to your LocalSession implementation. You will receive this bundle in onCreate
     * in your LocalSession implementation.
     */
    public void setSession(Class c, Bundle b) {
        sendSessionData(c, b);
    }


    /*
     * This will stop service. Make sure not to call anything on this class after shutdown.
     */
    public void shutdown() {
        Intent i = new Intent(context, ServerService.class);
        context.stopService(i);
//        i.putExtra(ServerService.ACTION, ServerService.STOP);
//        context.startService(i);
    }

    /*
     * This method is used to send data to your LocalSession implementation.
     */
    public void sendLocalSessionEvent(Payload<?> payload) {