diff src/alice/daemon/OutboundTcpConnection.java @ 13:30f97d776a3e

implements Alice daemon
author one
date Fri, 13 Jan 2012 19:04:59 +0900
parents
children e3f1b21718b0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/daemon/OutboundTcpConnection.java	Fri Jan 13 19:04:59 2012 +0900
@@ -0,0 +1,41 @@
+package alice.daemon;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.msgpack.MessagePack;
+
+import alice.datasegment.Command;
+
+public class OutboundTcpConnection extends Thread {
+	
+	public Connection connection;
+	
+	public OutboundTcpConnection(Connection connection) {
+		this.connection = connection;
+	}
+	
+	public CommandMessage convert(Command cmd) {
+		return new CommandMessage(cmd.type.id, cmd.index, cmd.seq, null, cmd.val);
+	}
+	
+	public void run() {
+		MessagePack msgpack = new MessagePack();
+		while (true) {
+			try {
+				CommandMessage cmdMsg = convert(connection.sendQueue.take());
+				byte[] buf = msgpack.write(cmdMsg);
+				ByteBuffer buffer = ByteBuffer.allocateDirect(4 + buf.length);
+				buffer.putInt(buf.length);
+				buffer.put(buf);
+				connection.socket.getChannel().write(buffer);
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+			
+		}
+	}
+	
+}