annotate src/alice/daemon/IncomingTcpConnection.java @ 270:23e53aaa8720

reconnect manager worked.
author sugi
date Wed, 21 Aug 2013 15:33:49 +0900
parents 88be2824a989
children 7188fe3f7c95
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
1 package alice.daemon;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
2
51
919389066887 change protocol header: remove an integer representing message length (work?)
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
3 import java.io.EOFException;
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
4 import java.io.IOException;
42
92aeb6e34683 implements close exception
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
5 import java.nio.channels.ClosedChannelException;
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
6
51
919389066887 change protocol header: remove an integer representing message length (work?)
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
7 import org.msgpack.unpacker.Unpacker;
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
8
126
669dba7cbb69 Use singleton
sugi
parents: 69
diff changeset
9 import alice.codesegment.SingletonMessage;
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
10 import alice.datasegment.Command;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
11 import alice.datasegment.CommandType;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
12 import alice.datasegment.DataSegment;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
13 import alice.datasegment.DataSegmentKey;
14
e3f1b21718b0 implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
14 import alice.datasegment.DataSegmentManager;
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
15 import alice.datasegment.LocalDataSegmentManager;
270
23e53aaa8720 reconnect manager worked.
sugi
parents: 251
diff changeset
16 import alice.topology.HostMessage;
23e53aaa8720 reconnect manager worked.
sugi
parents: 251
diff changeset
17 import alice.topology.manager.reconnection.SendError;
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
18
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
19 public class IncomingTcpConnection extends Thread {
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
20
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
21 public Connection connection;
14
e3f1b21718b0 implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
22 public DataSegmentManager manager;
28
98ab26e09a98 Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
23 public String reverseKey;
127
117dad267a9b add apiTest put
sugi
parents: 126
diff changeset
24 private LocalDataSegmentManager lmanager = DataSegment.getLocal();
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 64
diff changeset
25
28
98ab26e09a98 Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
26 public IncomingTcpConnection(Connection connection, DataSegmentManager manager, String reverseKey) {
14
e3f1b21718b0 implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
27 this.manager = manager;
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
28 this.connection = connection;
28
98ab26e09a98 Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
29 this.reverseKey = reverseKey;
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
30 }
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
31
58
ebdcab7b9b04 add comment
one
parents: 53
diff changeset
32 /**
ebdcab7b9b04 add comment
one
parents: 53
diff changeset
33 * pipeline thread for receiving
ebdcab7b9b04 add comment
one
parents: 53
diff changeset
34 */
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
35 public void run() {
51
919389066887 change protocol header: remove an integer representing message length (work?)
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
36 Unpacker unpacker = null;
919389066887 change protocol header: remove an integer representing message length (work?)
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
37 try {
126
669dba7cbb69 Use singleton
sugi
parents: 69
diff changeset
38 unpacker = SingletonMessage.getInstance().createUnpacker(connection.socket.getInputStream());
51
919389066887 change protocol header: remove an integer representing message length (work?)
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
39 } catch (IOException e2) {
919389066887 change protocol header: remove an integer representing message length (work?)
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
40 e2.printStackTrace();
919389066887 change protocol header: remove an integer representing message length (work?)
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
41 }
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
42 while (true) {
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
43 try {
51
919389066887 change protocol header: remove an integer representing message length (work?)
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
44 CommandMessage msg = unpacker.read(CommandMessage.class);
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
45 CommandType type = CommandType.getCommandTypeFromId(msg.type);
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
46 switch (type) {
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
47 case UPDATE:
240
d9c9076d6b47 no use SEDA in IncomingTcpConnection
sugi
parents: 224
diff changeset
48 getDataSegmentKey(msg).runCommand(new Command(type, null, null, msg.val, 0, 0, null, null, reverseKey));
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
49 break;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
50 case PUT:
240
d9c9076d6b47 no use SEDA in IncomingTcpConnection
sugi
parents: 224
diff changeset
51 getDataSegmentKey(msg).runCommand(new Command(type, null, null, msg.val, 0, 0, null, null, reverseKey));
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
52 break;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
53 case PEEK:
251
88be2824a989 no use Queue
sugi
parents: 240
diff changeset
54 getDataSegmentKey(msg).runCommand(new Command(type, null, null, null, msg.index, msg.seq, connection, null, null, msg.flag));
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
55 break;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
56 case TAKE:
251
88be2824a989 no use Queue
sugi
parents: 240
diff changeset
57 getDataSegmentKey(msg).runCommand(new Command(type, null, null, null, msg.index, msg.seq, connection, null, null, msg.flag));
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
58 break;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
59 case REMOVE:
240
d9c9076d6b47 no use SEDA in IncomingTcpConnection
sugi
parents: 224
diff changeset
60 getDataSegmentKey(msg).runCommand(new Command(type, null, null, null, 0, 0, null, null, null));
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
61 break;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
62 case REPLY:
240
d9c9076d6b47 no use SEDA in IncomingTcpConnection
sugi
parents: 224
diff changeset
63 Command cmd = manager.getAndRemoveCmd(msg.seq);
d9c9076d6b47 no use SEDA in IncomingTcpConnection
sugi
parents: 224
diff changeset
64 cmd.cs.ids.reply(cmd.receiver, new Command(type, null, null, msg.val, msg.index, msg.seq, null, null, null));
d9c9076d6b47 no use SEDA in IncomingTcpConnection
sugi
parents: 224
diff changeset
65 cmd=null;
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
66 break;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
67 default:
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
68 break;
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
69 }
42
92aeb6e34683 implements close exception
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
70 } catch (ClosedChannelException e) {
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 64
diff changeset
71 connection.sendCommand(new Command(CommandType.CLOSE, null, null, null, 0, 0, null, null, null));
42
92aeb6e34683 implements close exception
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
72 return;
51
919389066887 change protocol header: remove an integer representing message length (work?)
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
73 } catch (EOFException e) {
270
23e53aaa8720 reconnect manager worked.
sugi
parents: 251
diff changeset
74 new SendError(new HostMessage(connection.socket.getInetAddress().getHostName(), connection.socket.getPort())).execute();
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 64
diff changeset
75 connection.sendCommand(new Command(CommandType.CLOSE, null, null, null, 0, 0, null, null, null));
51
919389066887 change protocol header: remove an integer representing message length (work?)
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
76 return;
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
77 } catch (IOException e) {
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
78 e.printStackTrace();
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
79 }
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
80 }
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
81 }
64
7aaadd08288c add getLocal method to DataSegment
kazz
parents: 58
diff changeset
82 private DataSegmentKey getDataSegmentKey(CommandMessage msg) {
7aaadd08288c add getLocal method to DataSegment
kazz
parents: 58
diff changeset
83 return lmanager.getDataSegmentKey(msg.key);
7aaadd08288c add getLocal method to DataSegment
kazz
parents: 58
diff changeset
84 }
13
30f97d776a3e implements Alice daemon
one
parents:
diff changeset
85 }