annotate src/fdl/FederatedLinda.java @ 40:046feb56a196 metacomm-worked **INVALID**

message logging
author kono
date Mon, 25 Aug 2008 14:38:18 +0900
parents 81abceebc869
children c0591636a71a a1d796c0e975
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.channels.ClosedSelectorException;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
18 import java.nio.channels.SelectionKey;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
19 import java.nio.channels.Selector;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
20 import java.util.Hashtable;
31
846c6c14cf04 worked?
kono
parents: 25
diff changeset
21 import java.util.Iterator;
40
046feb56a196 message logging
kono
parents: 39
diff changeset
22 import java.util.logging.Level;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
23
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
24 /**
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
25 FederatedLinda
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
26 *
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
27 * @author Shinji Kono
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
28 *
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
29 * @param mytsid Tuple Space ID
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
30
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
31 Initialize connection channel for a tuple space
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
32
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
33 one instance for each Tuple space connection
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
34
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
35 */
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
36
17
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
37 public class FederatedLinda {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
38
35
fe338d497c72 FederatedLinda was static singleton. It does not work on Thread based test.
kono
parents: 34
diff changeset
39 FederatedLinda fdl;
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
40 static int MAX_SEQUENCE = 2048;
33
64071f8e2e0d *** empty log message ***
kono
parents: 32
diff changeset
41 static boolean debug = false;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
42
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
43 public int tid;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
44 public int seq;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
45 public int qsize;
25
330fa49bc4fd *** empty log message ***
kono
parents: 24
diff changeset
46 public PSXLinda linda;
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
47
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
48 public Selector selector;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
49
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
50 public PSXQueue q_top,q_end;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
51 public PSXReply r_top,r_end;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
52 public Hashtable<Integer,PSXReply> seqHash;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
53
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
54 public static FederatedLinda init() {
35
fe338d497c72 FederatedLinda was static singleton. It does not work on Thread based test.
kono
parents: 34
diff changeset
55 FederatedLinda fdl = new FederatedLinda();
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++;
39
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
71 PSXLindaImpl newlinda = new PSXLindaImpl(this,selector,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
39
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
76 PSXLinda openFromMetaLinda(MetaLinda metaLinda, String _host, int _port)
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
77 throws IOException {
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
78 tid++;
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
79 PSXLindaImpl newlinda = new PSXLindaImpl(this,metaLinda.fds.selector,tid,_host,_port);
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
80 linda = newlinda.add(linda);
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
81 return newlinda;
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
82 }
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
83
23
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
84 public PSXReply psx_queue(PSXLinda linda, int id, ByteBuffer s, int mode, PSXCallback callback) {
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
85 PSXQueue c = new PSXQueue(linda,id,mode,s,callback);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
86
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
87 if (q_top == null) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
88 c = q_end = q_top = c;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
89 } else {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
90 q_end.next = c;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
91 q_end = c;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
92 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
93 qsize++;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
94
17
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
95 if (mode != PSX.PSX_OUT) {
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
96 PSXReply p = new PSXReply(PSX.PSX_REPLY,callback);
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
97 p.seq = seq(p);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
98 c.setSeq(p.seq);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
99 if (r_top == null){
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
100 r_end = r_top = p;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
101 } else {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
102 r_end.next = p;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
103 r_end = p;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
104 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
105 return p;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
106 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
107 return null;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
108 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
109
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
110 public int seq(PSXReply reply) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
111 Integer s;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
112 do {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
113 seq++;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
114 if (seq>MAX_SEQUENCE) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
115 seq = 0;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
116 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
117 s = new Integer(seq);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
118 } while (seqHash.containsKey(s));
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
119 if (debug) {
40
046feb56a196 message logging
kono
parents: 39
diff changeset
120 log(Level.INFO,"hash value = "+s.hashCode());
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
121 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
122 seqHash.put(s,reply);
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
123 seq++;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
124 return seq-1;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
125 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
126
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
127 public Selector selector() {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
128 return selector;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
129 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
130
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
131 public int sync() throws IOException {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
132 return sync(0);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
133 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
134
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
135 public int sync(long mtimeout)
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
136 throws IOException {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
137 int key_num = 0;
39
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
138 queueExec();
2
b49e593b2502 Add ComDebug_Client
fuchita
parents: 0
diff changeset
139
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
140 try {
31
846c6c14cf04 worked?
kono
parents: 25
diff changeset
141 if (selector.select(mtimeout)>0) {
846c6c14cf04 worked?
kono
parents: 25
diff changeset
142 for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) {
846c6c14cf04 worked?
kono
parents: 25
diff changeset
143 SelectionKey s = it.next();
846c6c14cf04 worked?
kono
parents: 25
diff changeset
144 it.remove();
33
64071f8e2e0d *** empty log message ***
kono
parents: 32
diff changeset
145 try {
34
e7c5958fd285 *** empty log message ***
kono
parents: 33
diff changeset
146 if (!s.isReadable())
e7c5958fd285 *** empty log message ***
kono
parents: 33
diff changeset
147 throw new IOException();
39
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
148 TupleHandler handle = (TupleHandler)s.attachment();
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
149 handle.handle(s);
33
64071f8e2e0d *** empty log message ***
kono
parents: 32
diff changeset
150 } catch (IOException e) {
34
e7c5958fd285 *** empty log message ***
kono
parents: 33
diff changeset
151 s.cancel();
40
046feb56a196 message logging
kono
parents: 39
diff changeset
152 log(Level.INFO,""+s.channel()+" is closed.");
33
64071f8e2e0d *** empty log message ***
kono
parents: 32
diff changeset
153 }
64071f8e2e0d *** empty log message ***
kono
parents: 32
diff changeset
154 }
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
155 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
156 } catch (IOException e) {
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
157 e.printStackTrace();
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
158 } catch (ClosedSelectorException e) {
36
d5bca4b5ee95 meta.sync() recursive call fix.
kono
parents: 35
diff changeset
159 // client should be know
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
160 }
2
b49e593b2502 Add ComDebug_Client
fuchita
parents: 0
diff changeset
161
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
162 return key_num;
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
163 }
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
164
39
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
165 private void queueExec() {
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
166 while (q_top != null){
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
167 PSXQueue c = q_top;
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
168 c.send();
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
169 q_top = c.next;
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
170 qsize--;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
171 }
11
1355eb28e41d fix seqHash removal on checkServe.
kono
parents: 2
diff changeset
172 }
39
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
173
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
174 PSXReply getReply(int rseq) {
24
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
175 Integer a;
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
176
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
177 PSXReply r = seqHash.get((a = new Integer(rseq)));
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
178 seqHash.remove(a);
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
179 return r;
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
180 }
39
81abceebc869 *** empty log message ***
kono
parents: 38
diff changeset
181
40
046feb56a196 message logging
kono
parents: 39
diff changeset
182 public void log(Level level,String msg) {
046feb56a196 message logging
kono
parents: 39
diff changeset
183 System.err.println(msg);
046feb56a196 message logging
kono
parents: 39
diff changeset
184 if (level==Level.SEVERE)
046feb56a196 message logging
kono
parents: 39
diff changeset
185 new IOException().setStackTrace(null);
046feb56a196 message logging
kono
parents: 39
diff changeset
186 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
187 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
188
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
189 /* end */