annotate src/fdl/FederatedLinda.java @ 32:7e0f6f00763e new-comdebug

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