Mercurial > hg > Database > Alice
view src/main/java/alice/daemon/Connection.java @ 478:cf345b10a21a dispose
bug fix
author | sugi |
---|---|
date | Tue, 02 Dec 2014 17:16:34 +0900 |
parents | 041ec04d4d45 |
children | f82f259ea93b |
line wrap: on
line source
package alice.daemon; import java.io.IOException; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.util.concurrent.LinkedBlockingQueue; import alice.datasegment.Command; import alice.datasegment.DataSegment; import alice.datasegment.ReceiveData; import alice.datasegment.SendOption; 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 synchronized void write(Command cmd) { ByteBuffer buffer = cmd.convert(); try { while (buffer.hasRemaining()) { socket.getChannel().write(buffer); } } catch (ClosedChannelException e) { // when put dataSegment to remote if connection close this dataSemgent put. putConnectionInfo(); } catch (IOException e) { putConnectionInfo(); } } public void close(){ try { socket.shutdownOutput(); socket.shutdownInput(); socket.close(); putConnectionInfo(); } catch (ClosedChannelException e) { return; } catch (IOException e) { return; } } public void putConnectionInfo() { if (name!=null){ ConnectionInfo c = new ConnectionInfo(name, socket); ReceiveData rData = new ReceiveData(c, false, false); DataSegment.getLocal().put("_DISCONNECT", rData, null); if (sendManager) { SendOption option = new SendOption(false, false); DataSegment.get("manager").put("_DISCONNECTNODE", rData, option); } } } }