Mercurial > hg > Database > Alice
annotate src/alice/datasegment/RemoteDataSegmentManager.java @ 28:98ab26e09a98
Configuration Manager work and implements reverseKey
author | kazz <kazz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 17 Jan 2012 08:41:34 +0900 |
parents | f54dcbebde3a |
children | b5a21baf0b07 |
rev | line source |
---|---|
12 | 1 package alice.datasegment; |
2 | |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
3 import java.io.IOException; |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
4 import java.net.InetSocketAddress; |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
5 import java.nio.channels.SocketChannel; |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
6 |
25 | 7 import org.apache.log4j.Logger; |
12 | 8 import org.msgpack.type.Value; |
9 | |
10 import alice.codesegment.CodeSegment; | |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
11 import alice.daemon.Connection; |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
12 import alice.daemon.IncomingTcpConnection; |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
13 import alice.daemon.OutboundTcpConnection; |
12 | 14 |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
15 public class RemoteDataSegmentManager extends DataSegmentManager { |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
16 |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
17 Connection connection; |
25 | 18 Logger logger = Logger.getLogger(RemoteDataSegmentManager.class); |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
19 |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
20 // TODO: delete this constructor later |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
21 @Deprecated |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
22 public RemoteDataSegmentManager(Connection connection) { |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
23 this.connection = connection; |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
24 new IncomingTcpConnection(connection, this, "").start(); |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
25 new OutboundTcpConnection(connection).start(); |
20 | 26 new Thread(replyThread, "RemoteDataSegmentManager-" |
27 + connection.socket.getInetAddress().getHostName() | |
28 + ":" + connection.socket.getPort()).start(); | |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
29 } |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
30 |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
31 public RemoteDataSegmentManager(String connectionKey, final String reverseKey, final String hostName, final int port) { |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
32 connection = new Connection(); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
33 final RemoteDataSegmentManager manager = this; |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
34 new Thread(replyThread, "RemoteDataSegmentManager-" + connectionKey).start(); |
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
35 new Thread("Connect-" + connectionKey) { |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
36 public void run() { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
37 boolean connect = true; |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
38 do { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
39 try { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
40 SocketChannel sc = SocketChannel.open(new InetSocketAddress(hostName, port)); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
41 connection.socket = sc.socket(); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
42 connect = false; |
25 | 43 logger.info("Connect to " |
44 + connection.socket.getInetAddress().getHostName() | |
45 + ":" + connection.socket.getPort()); | |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
46 } catch (IOException e) { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
47 try { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
48 Thread.sleep(500); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
49 } catch (InterruptedException e1) { |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
50 e1.printStackTrace(); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
51 } |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
52 } |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
53 } while (connect); |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
54 new IncomingTcpConnection(connection, manager, reverseKey).start(); |
23
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
55 new OutboundTcpConnection(connection).start(); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
56 } |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
57 }.start(); |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
58 } |
54bf607118ae
change method to create RemoteDSM
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
20
diff
changeset
|
59 |
12 | 60 @Override |
61 public void put(String key, Value val) { | |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
62 connection.sendCommand(new Command(CommandType.PUT, null, key, val, 0, 0, null, null, null)); |
12 | 63 } |
64 | |
65 @Override | |
66 public void update(String key, Value val) { | |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
67 connection.sendCommand(new Command(CommandType.UPDATE, null, key, val, 0, 0, null, null, null)); |
12 | 68 } |
69 | |
70 @Override | |
18
72dd27d952b0
change InputDataSegment API
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
14
diff
changeset
|
71 public void take(DataSegmentReceiver receiver, String key, int index, CodeSegment cs) { |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
72 int seq = this.seq.getAndIncrement(); |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
73 Command cmd = new Command(CommandType.TAKE, receiver, key, null, index, seq, replyQueue, cs, null); |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
74 seqHash.put(seq, cmd); |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
75 connection.sendCommand(cmd); |
12 | 76 } |
77 | |
78 @Override | |
18
72dd27d952b0
change InputDataSegment API
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
14
diff
changeset
|
79 public void peek(DataSegmentReceiver receiver, String key, int index, CodeSegment cs) { |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
80 int seq = this.seq.getAndIncrement(); |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
81 Command cmd = new Command(CommandType.PEEK, receiver, key, null, index, seq, replyQueue, cs, null); |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
82 seqHash.put(seq, cmd); |
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
83 connection.sendCommand(cmd); |
12 | 84 } |
85 | |
86 @Override | |
87 public void remove(String key) { | |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
88 connection.sendCommand(new Command(CommandType.REMOVE, null, key, null, 0, 0, null, null, null)); |
12 | 89 } |
13 | 90 |
12 | 91 } |