view src/main/java/christie/daemon/Connection.java @ 13:bcd4f2c19185

don't work MessagePack unconvert for remote put
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Wed, 10 Jan 2018 20:37:47 +0900
parents b49a926cbdd9
children 5baccb8f7fbd
line wrap: on
line source

package christie.daemon;

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

import christie.codegear.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) { }
        //putConnectionInfo();

    }

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

}