view src/main/java/christie/daemon/Connection.java @ 56:1850b3eb93d8

fix SaveCookie
author akahori
date Thu, 23 Aug 2018 11:01:30 +0900
parents bf8ac57409af
children d2f1c7e3cf01
line wrap: on
line source

package christie.daemon;

import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.concurrent.LinkedBlockingQueue;

import christie.datagear.command.Command;

public class Connection {

    public Socket socket;
    public String name;
    public LinkedBlockingQueue<Command> sendQueue = new LinkedBlockingQueue<Command>();
    public boolean sendManager = true;

    public Connection(Socket socket) {
        this.socket = socket;
    }

    public Connection() {}

    public void sendCommand(Command cmd) {
        try {
            sendQueue.put(cmd);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String getInfoString() {
        return socket.getInetAddress().getHostName()
                + ":" + socket.getPort();
    }


    public void close(){
        try {
            socket.shutdownOutput();
            socket.shutdownInput();
            socket.close();
        } catch (Exception e) { }
    }

    public synchronized void write(Command cmd) {
        ByteBuffer buffer = cmd.convert();
        try {
            while (buffer.hasRemaining()) {
                socket.getChannel().write(buffer);
            }
        } catch (Exception e) {
        }
    }

}