changeset 249:2a8bcf09bd06

Refactor
author sugi
date Tue, 11 Jun 2013 16:53:22 +0900
parents 913ee9dfec4f
children 38154e695025
files src/alice/daemon/Connection.java src/alice/daemon/OutboundTcpConnection.java
diffstat 2 files changed, 18 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/alice/daemon/Connection.java	Wed May 29 15:39:55 2013 +0900
+++ b/src/alice/daemon/Connection.java	Tue Jun 11 16:53:22 2013 +0900
@@ -1,8 +1,11 @@
 package alice.daemon;
 
+import java.io.IOException;
 import java.net.Socket;
+import java.nio.ByteBuffer;
 import java.util.concurrent.LinkedBlockingQueue;
 
+import alice.codesegment.SingletonMessage;
 import alice.datasegment.Command;
 
 public class Connection {
@@ -29,4 +32,17 @@
 			   + ":" + socket.getPort();
 	}
 
+	public synchronized void write(Command cmd) {	
+		CommandMessage cmdMsg = cmd.convert();
+		ByteBuffer buffer;
+		try {
+			buffer = ByteBuffer.wrap(SingletonMessage.getInstance().write(cmdMsg));
+			while (buffer.hasRemaining()) {
+				socket.getChannel().write(buffer);
+			}
+		} catch (IOException e) {
+			
+		}
+	}
+
 }
--- a/src/alice/daemon/OutboundTcpConnection.java	Wed May 29 15:39:55 2013 +0900
+++ b/src/alice/daemon/OutboundTcpConnection.java	Tue Jun 11 16:53:22 2013 +0900
@@ -1,9 +1,6 @@
 package alice.daemon;
 
 import java.io.IOException;
-import java.nio.ByteBuffer;
-
-import alice.codesegment.SingletonMessage;
 import alice.datasegment.Command;
 
 public class OutboundTcpConnection extends Thread {
@@ -14,16 +11,7 @@
 		this.connection = connection;
 	}
 	
-	public CommandMessage convert(Command cmd) {
-		if (cmd.val==null&&cmd.obj!=null){
-			try {
-				cmd.val = SingletonMessage.getInstance().unconvert(cmd.obj);
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-		}
-		return new CommandMessage(cmd.type.id, cmd.index, cmd.seq, cmd.key, cmd.val);
-	}
+	
 	
 	/**
 	 * pipeline thread for transmission
@@ -42,11 +30,7 @@
 				default:
 					break;
 				}
-				CommandMessage cmdMsg = convert(cmd);
-				ByteBuffer buffer = ByteBuffer.wrap(SingletonMessage.getInstance().write(cmdMsg));
-				while (buffer.hasRemaining()) {
-					connection.socket.getChannel().write(buffer);
-				}
+				connection.write(cmd);
 			} catch (InterruptedException e) {
 				e.printStackTrace();
 			} catch (IOException e) {