package com.jld.vod.socket;

import android.content.Context;


import com.jld.vod.config.Config;

import org.json.JSONException;
import org.json.JSONObject;


public class SendSocketManager {

    private static volatile SendSocketManager instance = null;
    private SendUDPSocket udpSocket;
    private Context mContext;

    private SendSocketManager(Context context) {
        mContext = context.getApplicationContext();
    }

    public static SendSocketManager getInstance(Context context) {
        // if already inited, no need to get lock everytime
        if (instance == null) {
            synchronized (SendSocketManager.class) {
                if (instance == null) {
                    instance = new SendSocketManager(context);
                }
            }
        }

        return instance;
    }

    public void startUdpConnection() {
        if (udpSocket == null) {
            udpSocket = new SendUDPSocket(mContext);
        }
        udpSocket.startUDPSocket();

    }



    public void stopSocket() {
        udpSocket.stopUDPSocket();
        if (udpSocket != null) {
            udpSocket = null;
        }
    }

    public void sendMsg(String type, String msg)
    {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(Config.TYPE, type);
            jsonObject.put(Config.MSG, msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        udpSocket.sendMessage(jsonObject.toString());
    }

}
