Mercurial > hg > FederatedLinda
annotate src/fdl/TupleSpace.java @ 20:a0fd653d1121
Debug Client and Meta Engine for logging.
author | kono |
---|---|
date | Tue, 19 Aug 2008 06:26:20 +0900 |
parents | 0243987383b7 |
children | b4fd7fb9135a |
rev | line source |
---|---|
16 | 1 package fdl; |
2 | |
3 import java.io.IOException; | |
4 import java.nio.ByteBuffer; | |
5 import java.nio.channels.SelectionKey; | |
6 import java.nio.channels.SocketChannel; | |
7 | |
17 | 8 public class TupleSpace { |
16 | 9 static final boolean debug = true; |
10 static final int CAPSIZE = 4194304; | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
11 public static int user = 0; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
12 public byte userchar[] = new byte[2]; |
16 | 13 public Tuple[] tuple_space; |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
14 public IOHandlerHook hook = new NullIOHandlerHook(); |
16 | 15 |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
16 public TupleSpace() { |
16 | 17 // 読みこんだデータを格納するためのリストの初期化 |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
18 tuple_space = new Tuple[TupleHandler.MAX_TUPLE]; |
16 | 19 } |
20 | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
21 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
22 public void newUser() { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
23 Tuple tmpTuple; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
24 //初期生成 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
25 if((tmpTuple = tuple_space[TupleHandler.MAX_TUPLE-1]) == null) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
26 tmpTuple = tuple_space[TupleHandler.MAX_TUPLE-1] = new Tuple(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
27 tmpTuple.next = null; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
28 } else { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
29 while(tmpTuple.next != null) tmpTuple = tmpTuple.next; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
30 tmpTuple.next = new Tuple(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
31 tmpTuple = tmpTuple.next; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
32 tmpTuple.next = null; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
33 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
34 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
35 user++; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
36 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
37 ByteBuffer data = ByteBuffer.allocate(2); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
38 data.clear(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
39 userchar[0] = (byte) (user/10 + '0'); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
40 userchar[1] = (byte) (user%10 + '0'); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
41 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
42 data.put(userchar[0]); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
43 data.put(userchar[1]); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
44 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
45 data.rewind(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
46 tmpTuple.setData(data); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
47 //Tuple |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
48 int id = TupleHandler.MAX_TUPLE-1; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
49 tmpTuple.setTuple('o', id, 0, data.limit(), data); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
50 System.out.println("Server: assign id "+user); |
16 | 51 } |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
52 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
53 protected void Out(SelectionKey key,ByteBuffer command, ByteBuffer data) { |
17 | 54 Tuple tuple; |
16 | 55 int id; |
56 int datasize; | |
17 | 57 char idc = (char)command.getShort(PSX.LINDA_ID_OFFSET); |
16 | 58 command.rewind(); |
59 id = (int)idc; | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
60 |
17 | 61 datasize = command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET); |
16 | 62 command.rewind(); |
63 | |
64 System.out.println("*** out command : id = "+id +" ***"); | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
65 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
66 command.rewind(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
67 hook.outHook(key,id,seq,'o',data); |
16 | 68 |
69 while((tuple_space[id] != null) && | |
17 | 70 ((tuple_space[id].mode == PSX.PSX_WAIT_RD)||(tuple_space[id].mode == PSX.PSX_RD))) { |
71 PSX.setAnserCommand(command, tuple_space[id].getSeq()); | |
16 | 72 send(tuple_space[id].ch, command, data); |
73 | |
17 | 74 removeTuple(id); |
75 tuple = null; | |
16 | 76 } |
17 | 77 if(tuple_space[id] != null && tuple_space[id].mode == PSX.PSX_IN) { |
78 PSX.setAnserCommand(command, tuple_space[id].getSeq()); | |
16 | 79 |
80 if(debug){ | |
17 | 81 int sendsize = datasize+PSX.LINDA_HEADER_SIZE; |
16 | 82 System.out.println("send size "+sendsize+" : mode = "+(char)'a'); |
83 } | |
84 send(tuple_space[id].ch, command, data); | |
17 | 85 removeTuple(id); |
86 tuple = null; | |
87 } else if ((tuple_space[id] == null)|| (tuple_space[id].getMode() == PSX.PSX_OUT)) { | |
88 if((tuple = tuple_space[id]) == null) { | |
89 tuple = tuple_space[id] = new Tuple(); | |
90 tuple.next = null; | |
16 | 91 } |
92 else { | |
17 | 93 while(tuple.next != null) tuple = tuple.next; |
94 tuple.next = new Tuple(); | |
95 tuple = tuple.next; | |
96 tuple.next = null; | |
16 | 97 } |
98 | |
17 | 99 tuple.setMode('o'); |
100 tuple.setSeq(seq); | |
101 tuple.setData(data); | |
102 tuple.setDataLength(datasize); | |
16 | 103 if(debug){ |
17 | 104 System.out.println("data inserted len = "+tuple.getdataLength()+" : id = "+id); |
16 | 105 } |
106 } | |
107 else { | |
108 System.out.println("Incorrect mode :"+(char)tuple_space[id].getMode()); | |
109 command.clear(); | |
110 data.clear(); | |
111 System.exit(1); | |
112 } | |
113 } | |
114 | |
17 | 115 private void removeTuple(int id) { |
116 Tuple tuple; | |
117 //後処理 | |
118 tuple = tuple_space[id]; | |
119 tuple_space[id] = tuple.next; | |
120 } | |
121 | |
16 | 122 protected void Wait_Rd(SelectionKey key, ByteBuffer command, int mode) { |
17 | 123 Tuple tuple; |
16 | 124 int id; |
125 | |
17 | 126 char idc = (char)command.getShort(PSX.LINDA_ID_OFFSET); |
16 | 127 command.rewind(); |
128 id = (int)idc; | |
129 | |
130 if (debug) | |
131 System.out.println("*** "+(char)mode+" command : id = "+ id +" ***\n"); | |
132 | |
17 | 133 tuple = new Tuple(); |
134 tuple.setMode(mode); | |
135 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); | |
16 | 136 command.rewind(); |
17 | 137 tuple.setSeq(seq); |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
138 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
139 hook.waitReadHook(key,id,seq,(char)mode); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
140 |
17 | 141 tuple.ch = (SocketChannel) key.channel(); |
142 tuple.setDataLength(0); | |
16 | 143 ByteBuffer buff = ByteBuffer.allocate(0); |
17 | 144 tuple.setData(buff); |
145 tuple.next = tuple_space[id]; | |
146 tuple_space[id] = tuple; | |
16 | 147 if(debug){ |
148 System.out.println("data inserted insert seq = "+seq +", id = "+id); | |
149 } | |
150 } | |
151 | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
152 protected void In_Rd(SelectionKey key, ByteBuffer command, int mode) |
16 | 153 throws IOException { |
17 | 154 Tuple tuple = read_in_1(key, command, mode); |
16 | 155 |
17 | 156 if (tuple!=null) { |
16 | 157 //send |
17 | 158 ByteBuffer sendcommand = tuple.getCommand(); |
159 ByteBuffer senddata = tuple.getData(); | |
16 | 160 send(key,sendcommand, senddata); |
161 } | |
162 } | |
163 | |
164 private Tuple read_in_1(SelectionKey key, ByteBuffer command, int mode) { | |
17 | 165 Tuple tuple; |
16 | 166 int id; |
17 | 167 //id = command.getInt(PSX.LINDA_ID_OFFSET); |
168 //int mode = command.getInt(PSX.LINDA_MODE_OFFSET); | |
16 | 169 Tuple temp = null; |
170 | |
17 | 171 char idc = (char)command.getShort(PSX.LINDA_ID_OFFSET); |
16 | 172 command.rewind(); |
173 id = (int)idc; | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
174 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
175 command.rewind(); |
16 | 176 |
177 System.out.println("*** "+(char)mode+" command : id = "+ id +" ***\n"); | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
178 hook.inHook(key,id,seq,(char)mode); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
179 |
17 | 180 tuple = tuple_space[id]; |
16 | 181 |
182 //wを無視 | |
17 | 183 while(tuple != null && tuple.next != null && (tuple.mode == 'w')){ |
184 temp = tuple; | |
185 tuple = tuple.next; | |
16 | 186 } |
187 | |
17 | 188 if (tuple != null && (tuple.mode == 'o')){ |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
189 tupleIsAvailable(command, mode, tuple, id, temp); |
16 | 190 } else { |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
191 tuple = setupWait(key, command, mode, tuple, id); |
16 | 192 } |
17 | 193 return tuple; |
16 | 194 } |
195 | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
196 public ByteBuffer IN(int id,int mode, ByteBuffer command) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
197 Tuple tuple,temp=null; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
198 tuple = tuple_space[id]; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
199 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
200 //wを無視 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
201 while(tuple != null && tuple.next != null && (tuple.mode == 'w')){ |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
202 temp = tuple; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
203 tuple = tuple.next; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
204 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
205 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
206 if (tuple != null && (tuple.mode == 'o')){ |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
207 ByteBuffer data = tuple.data; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
208 tupleIsAvailable(command, mode, tuple, id, temp); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
209 return data; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
210 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
211 return null; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
212 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
213 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
214 private void tupleIsAvailable(ByteBuffer command, int mode, Tuple tuple, |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
215 int id, Tuple temp) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
216 //tmpTuple = new Tuple((SocketChannel)key.channel()); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
217 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
218 command.rewind(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
219 tuple.setCommand('a', seq); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
220 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
221 if(debug){ |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
222 int sendsize = tuple.getdataLength()+PSX.LINDA_HEADER_SIZE; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
223 System.out.println("send size "+sendsize+" : mode = "+(char)tuple.getMode()); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
224 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
225 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
226 |
16 | 227 |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
228 //INの場合はremoveする |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
229 if(mode == PSX.PSX_IN) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
230 if(tuple.data != null){ |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
231 //ByteBuffer buff = ByteBuffer.allocate(0); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
232 //tmpTuple.setData(buff); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
233 tuple.data = null; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
234 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
235 if(temp != null){ |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
236 temp.next = tuple.next; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
237 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
238 else { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
239 tuple_space[id] = tuple.next; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
240 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
241 } |
16 | 242 } |
243 | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
244 private Tuple setupWait(SelectionKey key, ByteBuffer command, int mode, |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
245 Tuple tuple, int id) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
246 if(tuple == null) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
247 //ServerSocketChannel sc = (ServerSocketChannel)key.channel(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
248 tuple = tuple_space[id] = new Tuple((SocketChannel)key.channel()); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
249 tuple.next = null; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
250 }else { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
251 while(tuple.next !=null) tuple =tuple.next; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
252 tuple.next= new Tuple((SocketChannel)key.channel()); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
253 tuple = tuple.next; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
254 tuple.next = null; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
255 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
256 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
257 tuple.setMode(mode); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
258 int seq2 = command.getInt(PSX.LINDA_SEQ_OFFSET); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
259 command.rewind(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
260 tuple.setSeq(seq2); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
261 tuple.ch = (SocketChannel) key.channel(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
262 tuple.setDataLength(0); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
263 ByteBuffer buff = ByteBuffer.allocate(0); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
264 buff.rewind(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
265 tuple.setData(buff); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
266 tuple = null; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
267 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
268 if(debug){ |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
269 System.out.println("data inserted insert seq = "+seq2 +", id = "+id); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
270 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
271 return tuple; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
272 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
273 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
274 protected void Check(SelectionKey key, ByteBuffer command) throws IOException { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
275 ByteBuffer data = check1(key,command); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
276 send(key, command, data); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
277 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
278 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
279 public ByteBuffer check1(SelectionKey key,ByteBuffer command) { |
16 | 280 ByteBuffer data; |
281 Tuple tmpTuple; | |
282 int id; | |
17 | 283 char idc = (char)command.getShort(PSX.LINDA_ID_OFFSET); |
16 | 284 command.rewind(); |
285 id = (int)idc; | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
286 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
287 command.rewind(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
288 hook.checkHook(key,id,seq,'c'); |
16 | 289 |
290 tmpTuple = tuple_space[id]; | |
291 while(tmpTuple != null && tmpTuple.next != null && (tmpTuple.mode == 'w')){ | |
292 tmpTuple = tmpTuple.next; | |
293 } | |
294 if (tmpTuple != null && (tmpTuple.mode == 'o')) { | |
17 | 295 command.putInt(PSX.LINDA_DATA_LENGTH_OFFSET, tmpTuple.datalen); |
16 | 296 command.rewind(); |
297 data = tmpTuple.getData(); | |
298 }else { | |
299 //means no out tuple | |
17 | 300 command.putInt(PSX.LINDA_DATA_LENGTH_OFFSET, 0); |
16 | 301 command.rewind(); |
302 data = ByteBuffer.allocate(0); | |
303 } | |
304 return data; | |
305 } | |
306 | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
307 public void send(SocketChannel ch, ByteBuffer command, ByteBuffer data) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
308 if (debug) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
309 if (command == null) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
310 System.out.println("Manager_run: command is null"); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
311 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
312 if (data == null) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
313 System.out.println("Manager_run: data is null"); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
314 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
315 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
316 int send_size = PSX.LINDA_HEADER_SIZE; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
317 int count = 0; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
318 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
319 try { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
320 //command Send |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
321 command.rewind(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
322 while(send_size > 0){ |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
323 count = ch.write(command); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
324 if(count < 0) throw new IOException(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
325 send_size -= count; |
16 | 326 } |
327 | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
328 if (data==null) return; |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
329 //data Send |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
330 data.rewind(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
331 while(data.remaining() > 0){ |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
332 count = ch.write(data); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
333 if(count < 0) throw new IOException(); |
16 | 334 } |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
335 } catch (IOException e) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
336 System.out.println("Write Falied on:"+ch); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
337 return; |
16 | 338 } |
339 } | |
340 | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
341 public void send(SelectionKey key, ByteBuffer command, ByteBuffer data) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
342 SocketChannel ch = (SocketChannel)key.channel(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
343 send(ch,command,data); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
344 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
345 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
346 |
16 | 347 } |