annotate src/main/java/alice/daemon/IncomingTcpConnection.java @ 488:7ef0ebb40c9b dispose

add measurement data in protocol
author sugi
date Mon, 08 Dec 2014 23:11:26 +0900
parents c06070403ed4
children 118e150ac9f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
1 package alice.daemon;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
2
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
3 import java.io.EOFException;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
4 import java.io.IOException;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
5 import java.nio.channels.ClosedChannelException;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
6
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
7 import org.msgpack.unpacker.MessagePackUnpacker;
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
8 import org.msgpack.unpacker.Unpacker;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
9
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
10 import alice.codesegment.SingletonMessage;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
11 import alice.datasegment.Command;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
12 import alice.datasegment.CommandType;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
13 import alice.datasegment.DataSegment;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
14 import alice.datasegment.DataSegmentManager;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
15 import alice.datasegment.LocalDataSegmentManager;
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
16 import alice.datasegment.ReceiveData;
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
17 import alice.topology.manager.keeparive.RespondData;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
18
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
19 public class IncomingTcpConnection extends Thread {
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
20
480
c06070403ed4 named IncomingTcp and OutboundTcp Thread
sugi
parents: 478
diff changeset
21 private Connection connection;
c06070403ed4 named IncomingTcp and OutboundTcp Thread
sugi
parents: 478
diff changeset
22 protected DataSegmentManager manager;
c06070403ed4 named IncomingTcp and OutboundTcp Thread
sugi
parents: 478
diff changeset
23 protected String reverseKey;
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
24 private LocalDataSegmentManager lmanager = DataSegment.getLocal();
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
25
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
26 public IncomingTcpConnection(DataSegmentManager manager) {
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
27 this.manager = manager;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
28 }
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
29
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
30 public IncomingTcpConnection(Connection connection, DataSegmentManager manager, String reverseKey) {
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
31 this.manager = manager;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
32 this.connection = connection;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
33 this.reverseKey = reverseKey;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
34 }
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
35
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
36 public LocalDataSegmentManager getLocalDataSegmentManager(){
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
37 return lmanager;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
38 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
39
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
40 /**
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
41 * pipeline thread for receiving
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
42 */
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
43 public void run() {
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
44 Unpacker unpacker = null;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
45 try {
445
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
46 unpacker = SingletonMessage.getInstance().createUnpacker(connection.socket.getInputStream());
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
47 } catch (IOException e) {
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
48 e.printStackTrace();
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
49 }
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
50 if (unpacker == null) {
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
51 return;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
52 }
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
53 while (true) {
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
54 try {
447
d30451d1882f fix IncomingUdpConnection and passed UdpTest
sugi
parents: 446
diff changeset
55 Command cmd = null;
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
56 ReceiveData rData = null;
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
57 CommandMessage msg = unpacker.read(CommandMessage.class);
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
58 CommandType type = CommandType.getCommandTypeFromId(msg.type);
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
59 switch (type) {
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
60 case UPDATE:
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
61 case PUT:
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
62 rData = new ReceiveData(getSerializedByteArray(unpacker), msg.compressed, msg.serialized);
488
7ef0ebb40c9b add measurement data in protocol
sugi
parents: 480
diff changeset
63 if (msg.setTime) {
7ef0ebb40c9b add measurement data in protocol
sugi
parents: 480
diff changeset
64 rData.setTime = true;
7ef0ebb40c9b add measurement data in protocol
sugi
parents: 480
diff changeset
65 rData.time = msg.time;
7ef0ebb40c9b add measurement data in protocol
sugi
parents: 480
diff changeset
66 rData.depth = msg.depth;
7ef0ebb40c9b add measurement data in protocol
sugi
parents: 480
diff changeset
67 }
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
68 cmd = new Command(type, null, null, rData, 0, 0, null, null, reverseKey);
446
a91890dff56e refactor
sugi
parents: 445
diff changeset
69 lmanager.getDataSegmentKey(msg.key).runCommand(cmd);
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
70 break;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
71 case PEEK:
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
72 case TAKE:
446
a91890dff56e refactor
sugi
parents: 445
diff changeset
73 cmd = new Command(type, null, null, null, msg.index, msg.seq, null, null, connection);
449
09a80f83c605 add Serialized and compressed flag on protocol header
sugi
parents: 447
diff changeset
74 cmd.setQuickFlag(msg.quickFlag);
446
a91890dff56e refactor
sugi
parents: 445
diff changeset
75 lmanager.getDataSegmentKey(msg.key).runCommand(cmd);
467
6e304a7a60e7 remove white space
sugi
parents: 459
diff changeset
76 break;
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
77 case REMOVE:
446
a91890dff56e refactor
sugi
parents: 445
diff changeset
78 cmd = new Command(type, null, null, null, 0, 0, null, null, "");
a91890dff56e refactor
sugi
parents: 445
diff changeset
79 lmanager.getDataSegmentKey(msg.key).runCommand(cmd);
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
80 break;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
81 case REPLY:
446
a91890dff56e refactor
sugi
parents: 445
diff changeset
82 cmd = manager.getAndRemoveCmd(msg.seq);
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
83 rData = new ReceiveData(getSerializedByteArray(unpacker), msg.compressed, msg.serialized);
467
6e304a7a60e7 remove white space
sugi
parents: 459
diff changeset
84 Command rCmd = new Command(type, null, null, rData, msg.index, msg.seq, null, null, "");
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 449
diff changeset
85 cmd.cs.ids.reply(cmd.receiver, rCmd);
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
86 break;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
87 case PING:
471
be0b61986ff7 checking having DataSegmentManger before get DataSegmentManager
sugi
parents: 467
diff changeset
88 if (DataSegment.contains(reverseKey))
be0b61986ff7 checking having DataSegmentManger before get DataSegmentManager
sugi
parents: 467
diff changeset
89 DataSegment.get(reverseKey).response(msg.key);
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
90 break;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
91 case RESPONSE:
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
92 rData = new ReceiveData(new RespondData(reverseKey, System.currentTimeMillis()), false, false);
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
93 DataSegment.getLocal().put(msg.key, rData, null);
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
94 break;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
95 default:
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
96 break;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
97 }
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
98 } catch (ClosedChannelException e) {
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
99 return;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
100 } catch (EOFException e) {
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
101 return;
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
102 } catch (IOException e) {
478
cf345b10a21a bug fix
sugi
parents: 471
diff changeset
103 return;
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
104 }
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
105 }
467
6e304a7a60e7 remove white space
sugi
parents: 459
diff changeset
106
419
aefbe41fcf12 change tab to space
sugi
parents: 417
diff changeset
107 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
108
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
109 private byte[] getSerializedByteArray(Unpacker unpacker) {
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
110 int len;
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
111 byte[] b = null;
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
112 try {
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
113 len = unpacker.readInt();
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
114 b = ((MessagePackUnpacker) unpacker).getSerializedByteArray(len);
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
115 } catch (IOException e) {
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
116 e.printStackTrace();
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
117 }
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
118 return b;
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
119 }
480
c06070403ed4 named IncomingTcp and OutboundTcp Thread
sugi
parents: 478
diff changeset
120
c06070403ed4 named IncomingTcp and OutboundTcp Thread
sugi
parents: 478
diff changeset
121 public void setReverseKey(String name) {
c06070403ed4 named IncomingTcp and OutboundTcp Thread
sugi
parents: 478
diff changeset
122 this.reverseKey = name;
c06070403ed4 named IncomingTcp and OutboundTcp Thread
sugi
parents: 478
diff changeset
123 setName(name+"-IncomingTcp");
c06070403ed4 named IncomingTcp and OutboundTcp Thread
sugi
parents: 478
diff changeset
124 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
125 }