comparison src/alice/daemon/OutboundTcpConnection.java @ 249:2a8bcf09bd06

Refactor
author sugi
date Tue, 11 Jun 2013 16:53:22 +0900
parents d50cddf64396
children
comparison
equal deleted inserted replaced
248:913ee9dfec4f 249:2a8bcf09bd06
1 package alice.daemon; 1 package alice.daemon;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.nio.ByteBuffer;
5
6 import alice.codesegment.SingletonMessage;
7 import alice.datasegment.Command; 4 import alice.datasegment.Command;
8 5
9 public class OutboundTcpConnection extends Thread { 6 public class OutboundTcpConnection extends Thread {
10 7
11 public Connection connection; 8 public Connection connection;
12 9
13 public OutboundTcpConnection(Connection connection) { 10 public OutboundTcpConnection(Connection connection) {
14 this.connection = connection; 11 this.connection = connection;
15 } 12 }
16 13
17 public CommandMessage convert(Command cmd) { 14
18 if (cmd.val==null&&cmd.obj!=null){
19 try {
20 cmd.val = SingletonMessage.getInstance().unconvert(cmd.obj);
21 } catch (IOException e) {
22 e.printStackTrace();
23 }
24 }
25 return new CommandMessage(cmd.type.id, cmd.index, cmd.seq, cmd.key, cmd.val);
26 }
27 15
28 /** 16 /**
29 * pipeline thread for transmission 17 * pipeline thread for transmission
30 */ 18 */
31 public void run() { 19 public void run() {
40 System.exit(0); 28 System.exit(0);
41 return; 29 return;
42 default: 30 default:
43 break; 31 break;
44 } 32 }
45 CommandMessage cmdMsg = convert(cmd); 33 connection.write(cmd);
46 ByteBuffer buffer = ByteBuffer.wrap(SingletonMessage.getInstance().write(cmdMsg));
47 while (buffer.hasRemaining()) {
48 connection.socket.getChannel().write(buffer);
49 }
50 } catch (InterruptedException e) { 34 } catch (InterruptedException e) {
51 e.printStackTrace(); 35 e.printStackTrace();
52 } catch (IOException e) { 36 } catch (IOException e) {
53 e.printStackTrace(); 37 e.printStackTrace();
54 } 38 }