Mercurial > hg > Database > Alice
annotate src/alice/daemon/OutboundTcpConnection.java @ 30:b5a21baf0b07
implements RingTopology
author | kazz <kazz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 17 Jan 2012 16:13:03 +0900 |
parents | 433e601a8e28 |
children | f9334781344a |
rev | line source |
---|---|
13 | 1 package alice.daemon; |
2 | |
3 import java.io.IOException; | |
4 import java.nio.ByteBuffer; | |
5 | |
6 import org.msgpack.MessagePack; | |
7 | |
8 import alice.datasegment.Command; | |
30 | 9 import alice.datasegment.CommandType; |
13 | 10 |
11 public class OutboundTcpConnection extends Thread { | |
12 | |
13 public Connection connection; | |
14 | |
15 public OutboundTcpConnection(Connection connection) { | |
16 this.connection = connection; | |
17 } | |
18 | |
19 public CommandMessage convert(Command cmd) { | |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
20 return new CommandMessage(cmd.type.id, cmd.index, cmd.seq, cmd.key, cmd.val); |
13 | 21 } |
22 | |
23 public void run() { | |
24 MessagePack msgpack = new MessagePack(); | |
25 while (true) { | |
26 try { | |
30 | 27 Command cmd = connection.sendQueue.take(); |
28 if (cmd.type == CommandType.FINISH) { | |
29 System.exit(0); | |
30 return; | |
31 } | |
32 CommandMessage cmdMsg = convert(cmd); | |
13 | 33 byte[] buf = msgpack.write(cmdMsg); |
34 ByteBuffer buffer = ByteBuffer.allocateDirect(4 + buf.length); | |
35 buffer.putInt(buf.length); | |
36 buffer.put(buf); | |
16 | 37 buffer.flip(); |
13 | 38 connection.socket.getChannel().write(buffer); |
39 } catch (InterruptedException e) { | |
40 e.printStackTrace(); | |
41 } catch (IOException e) { | |
42 e.printStackTrace(); | |
43 } | |
44 } | |
45 } | |
46 | |
47 } |