annotate src/fdl/FederatedLinda.java @ 24:35375016b2f0 simple-test-passed

cleanup.
author kono
date Wed, 20 Aug 2008 10:18:05 +0900 (2008-08-20)
parents b4fd7fb9135a
children 330fa49bc4fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
1
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
2 /*
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
3 * @(#)FederatedLinda.java 1.1 06/04/01
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
4 *
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
5 * Copyright 2006 Shinji KONO
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
6 *
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
7
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
8 PSX Lidna
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
9 Trasport layer of PSX Linda library
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
10
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
11 */
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
12
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
13 package fdl;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
14
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
15 import java.io.IOException;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
16 import java.nio.ByteBuffer;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
17 import java.nio.ByteOrder;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
18 import java.nio.channels.ClosedSelectorException;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
19 import java.nio.channels.SelectionKey;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
20 import java.nio.channels.Selector;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
21 import java.nio.channels.SocketChannel;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
22 import java.util.Hashtable;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
23 import java.util.Set;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
24
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
25
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
26 /**
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
27 FederatedLinda
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
28 *
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
29 * @author Shinji Kono
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
30 *
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
31 * @param mytsid Tuple Space ID
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
32
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
33 Initialize connection channel for a tuple space
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
34
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
35 one instance for each Tuple space connection
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
36
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
37 */
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
38
17
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
39 public class FederatedLinda {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
40
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
41 static FederatedLinda fdl = new FederatedLinda();
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
42 static int MAX_SEQUENCE = 2048;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
43 static boolean debug = true;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
44
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
45 public int tid;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
46 public int seq;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
47 public int qsize;
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
48 public PSXLindaInterface linda;
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
49
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
50 public Selector selector;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
51
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
52 public PSXQueue q_top,q_end;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
53 public PSXReply r_top,r_end;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
54 public Hashtable<Integer,PSXReply> seqHash;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
55
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
56 public static FederatedLinda init() {
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
57 return fdl;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
58 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
59
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
60 private FederatedLinda() {
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
61 try {
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
62 selector = Selector.open();
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
63 } catch (IOException e) {
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
64 e.printStackTrace();
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
65 }
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
66 seqHash = new Hashtable<Integer, PSXReply>();
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
67 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
68
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
69 public PSXLindaInterface open(String _host,int _port)
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
70 throws IOException {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
71 tid++;
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
72 PSXLindaInterface newlinda = new PSXLinda(this,tid,_host,_port);
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
73 linda = newlinda.add(linda);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
74 return linda;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
75 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
76
23
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
77 public PSXReply psx_queue(PSXLinda linda, int id, ByteBuffer s, int mode, PSXCallback callback) {
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
78 PSXQueue c = new PSXQueue(linda,id,mode,s,callback);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
79
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
80 if (q_top == null) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
81 c = q_end = q_top = c;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
82 } else {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
83 q_end.next = c;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
84 q_end = c;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
85 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
86 qsize++;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
87
17
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
88 if (mode != PSX.PSX_OUT) {
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
89 PSXReply p = new PSXReply(PSX.PSX_REPLY,callback);
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
90 p.seq = seq(p);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
91 c.setSeq(p.seq);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
92 if (r_top == null){
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
93 r_end = r_top = p;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
94 } else {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
95 r_end.next = p;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
96 r_end = p;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
97 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
98 return p;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
99 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
100 return null;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
101 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
102
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
103 public int seq(PSXReply reply) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
104 Integer s;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
105 do {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
106 seq++;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
107 if (seq>MAX_SEQUENCE) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
108 seq = 0;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
109 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
110 s = new Integer(seq);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
111 } while (seqHash.containsKey(s));
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
112 if (debug) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
113 System.out.print("hash value = ");
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
114 System.out.println(s.hashCode());
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
115 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
116 seqHash.put(s,reply);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
117 seq++;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
118 return seq-1;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
119 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
120
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
121 public Selector selector() {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
122 return selector;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
123 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
124
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
125 public int sync() throws IOException {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
126 return sync(0);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
127 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
128
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
129 public int sync(long mtimeout)
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
130 throws IOException {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
131 int key_num = 0;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
132 Set<SelectionKey> keys;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
133
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
134 while (q_top != null){
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
135 PSXQueue c = q_top;
23
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
136 c.send();
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
137 q_top = c.next;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
138 qsize--;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
139 }
2
b49e593b2502 Add ComDebug_Client
fuchita
parents: 0
diff changeset
140
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
141 try {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
142 key_num = selector.select(mtimeout);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
143 keys = selector.selectedKeys();
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
144 for (SelectionKey key : keys) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
145 SocketChannel sock = (SocketChannel)key.channel();
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
146 chkServe(sock);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
147 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
148 } catch (IOException e) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
149 e.printStackTrace();
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
150 } catch (ClosedSelectorException e) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
151 e.printStackTrace();
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
152 }
2
b49e593b2502 Add ComDebug_Client
fuchita
parents: 0
diff changeset
153
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
154 return key_num;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
155 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
156
2
b49e593b2502 Add ComDebug_Client
fuchita
parents: 0
diff changeset
157 private void chkServe(SocketChannel sock)
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
158 throws IOException {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
159 int length;
17
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
160 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE);
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
161 command.order(ByteOrder.BIG_ENDIAN);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
162
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
163 sock.read(command);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
164 command.rewind();
17
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
165 length = command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET);
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
166 if (length>0) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
167 ByteBuffer data = ByteBuffer.allocate(length);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
168 int read = length;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
169 if (debug) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
170 System.out.print("reading:");
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
171 System.out.println(length);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
172 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
173
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
174 data.order(ByteOrder.BIG_ENDIAN);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
175 while(read>0) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
176 read -= sock.read(data);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
177 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
178 data.rewind();
2
b49e593b2502 Add ComDebug_Client
fuchita
parents: 0
diff changeset
179
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
180 if (debug) {
17
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
181 PSX.printCommand(command, data);
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
182 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
183
17
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
184 int rseq = command.getInt(PSX.LINDA_SEQ_OFFSET);
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
185 int mode = command.get(PSX.LINDA_MODE_OFFSET);
24
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
186 PSXReply r = getReply(rseq);
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
187 r.setAnswer(mode,command,data);
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
188
24
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
189 if (r.callback != null ) {
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
190 r.callback.callback(data);
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
191 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
192 }
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
193 }
24
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
194
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
195 private PSXReply getReply(int rseq) {
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
196 Integer a;
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
197
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
198 PSXReply r = seqHash.get((a = new Integer(rseq)));
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
199 if (r==null) {
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
200 System.out.println("hashed reply not found");
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
201 }
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
202 seqHash.remove(a);
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
203 return r;
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
204 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
205 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
206
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
207 /* end */