Mercurial > hg > FederatedLinda
comparison src/fdl/TupleSpace.java @ 40:046feb56a196 metacomm-worked **INVALID**
message logging
author | kono |
---|---|
date | Mon, 25 Aug 2008 14:38:18 +0900 |
parents | e7c5958fd285 |
children | 5b1f099da593 |
comparison
equal
deleted
inserted
replaced
39:81abceebc869 | 40:046feb56a196 |
---|---|
2 | 2 |
3 import java.io.IOException; | 3 import java.io.IOException; |
4 import java.nio.ByteBuffer; | 4 import java.nio.ByteBuffer; |
5 import java.nio.channels.SelectionKey; | 5 import java.nio.channels.SelectionKey; |
6 import java.nio.channels.SocketChannel; | 6 import java.nio.channels.SocketChannel; |
7 import java.util.logging.Level; | |
7 | 8 |
8 public class TupleSpace { | 9 public class TupleSpace { |
9 static final boolean debug = true; | 10 static final boolean debug = true; |
10 public static int user = 0; | 11 public static int user = 0; |
11 public byte userchar[] = new byte[2]; | 12 public byte userchar[] = new byte[2]; |
12 public Tuple[] tuple_space; | 13 public Tuple[] tuple_space; |
13 public IOHandlerHook hook = new NullIOHandlerHook(); | 14 public IOHandlerHook hook = new NullIOHandlerHook(); |
15 private FDLindaServ fds; | |
14 public static final int MAX_TUPLE_ID = 65536; | 16 public static final int MAX_TUPLE_ID = 65536; |
15 | 17 |
16 public TupleSpace() { | 18 public TupleSpace(FDLindaServ fds) { |
17 // 読みこんだデータを格納するためのリストの初期化 | 19 // 読みこんだデータを格納するためのリストの初期化 |
18 tuple_space = new Tuple[TupleSpace.MAX_TUPLE_ID]; | 20 tuple_space = new Tuple[MAX_TUPLE_ID]; |
21 this.fds = fds; | |
19 } | 22 } |
20 | 23 |
21 | 24 |
22 public void newUser() { | 25 public void newUser() { |
23 Tuple tuple; | 26 Tuple tuple; |
24 //初期生成 | 27 //初期生成 |
25 if((tuple = tuple_space[TupleSpace.MAX_TUPLE_ID-1]) == null) { | 28 if((tuple = tuple_space[MAX_TUPLE_ID-1]) == null) { |
26 tuple = tuple_space[TupleSpace.MAX_TUPLE_ID-1] = new Tuple(); | 29 tuple = tuple_space[MAX_TUPLE_ID-1] = new Tuple(); |
27 tuple.next = null; | 30 tuple.next = null; |
28 } else { | 31 } else { |
29 while(tuple.next != null) tuple = tuple.next; | 32 while(tuple.next != null) tuple = tuple.next; |
30 tuple.next = new Tuple(); | 33 tuple.next = new Tuple(); |
31 tuple = tuple.next; | 34 tuple = tuple.next; |
42 data.flip(); | 45 data.flip(); |
43 | 46 |
44 int id = TupleSpace.MAX_TUPLE_ID-1; | 47 int id = TupleSpace.MAX_TUPLE_ID-1; |
45 int seq = 0; | 48 int seq = 0; |
46 tuple.setTuple('o', id, seq, data); | 49 tuple.setTuple('o', id, seq, data); |
47 System.out.println("Server: assign id "+user); | 50 fds.log(Level.INFO,"Server: assign id "+user); |
48 } | 51 } |
49 | 52 |
50 protected void Out(SelectionKey key,ByteBuffer command, ByteBuffer data) { | 53 protected void Out(SelectionKey key,ByteBuffer command, ByteBuffer data) { |
51 Tuple tuple; | 54 Tuple tuple; |
52 int id; | 55 int id; |
56 id = (int)idc; | 59 id = (int)idc; |
57 | 60 |
58 datasize = command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET); | 61 datasize = command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET); |
59 command.rewind(); | 62 command.rewind(); |
60 | 63 |
61 if (debug) System.out.println("*** out command : id = "+id +" ***"); | 64 if (debug) fds.log(Level.INFO,"*** out command : id = "+id +" ***"); |
62 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); | 65 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); |
63 command.rewind(); | 66 command.rewind(); |
64 hook.outHook(key,id,seq,'o',data); | 67 hook.outHook(key,id,seq,'o',data); |
65 | 68 |
66 while((tuple_space[id] != null) && | 69 while((tuple_space[id] != null) && |
73 } | 76 } |
74 if(tuple_space[id] != null && tuple_space[id].mode == PSX.PSX_IN) { | 77 if(tuple_space[id] != null && tuple_space[id].mode == PSX.PSX_IN) { |
75 PSX.setAnserCommand(command, tuple_space[id].getSeq()); | 78 PSX.setAnserCommand(command, tuple_space[id].getSeq()); |
76 | 79 |
77 if(debug){ | 80 if(debug){ |
78 System.out.println("send size "+datasize+" : mode = "+(char)'a'); | 81 if (debug) fds.log(Level.INFO,"send size "+datasize+" : mode = "+(char)'a'); |
79 } | 82 } |
80 PSX.send(tuple_space[id].ch, command, data); | 83 PSX.send(tuple_space[id].ch, command, data); |
81 removeTuple(id); | 84 removeTuple(id); |
82 tuple = null; | 85 tuple = null; |
83 // Incoming Out tuple is consumed here, and wating IN tuple is also removed. | 86 // Incoming Out tuple is consumed here, and wating IN tuple is also removed. |
95 | 98 |
96 tuple.setMode('o'); | 99 tuple.setMode('o'); |
97 tuple.setSeq(seq); | 100 tuple.setSeq(seq); |
98 tuple.setData(data); | 101 tuple.setData(data); |
99 if(debug){ | 102 if(debug){ |
100 System.out.println("data inserted len = "+tuple.getdataLength()+" : id = "+id); | 103 fds.log(Level.INFO, "data inserted len = "+tuple.getdataLength()+" : id = "+id); |
101 } | 104 } |
102 } else { | 105 } else { |
103 System.out.println("Incorrect mode :"+(char)tuple_space[id].getMode()); | 106 fds.log(Level.SEVERE, "Incorrect mode :"+(char)tuple_space[id].getMode()); |
104 command.clear(); | |
105 data.clear(); | |
106 System.exit(1); | |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 private void removeTuple(int id) { | 110 private void removeTuple(int id) { |
111 Tuple tuple; | 111 Tuple tuple; |
121 char idc = (char)command.getShort(PSX.LINDA_ID_OFFSET); | 121 char idc = (char)command.getShort(PSX.LINDA_ID_OFFSET); |
122 command.rewind(); | 122 command.rewind(); |
123 id = (int)idc; | 123 id = (int)idc; |
124 | 124 |
125 if (debug) | 125 if (debug) |
126 System.out.println("*** "+(char)mode+" command : id = "+ id +" ***\n"); | 126 fds.log(Level.INFO, "*** "+(char)mode+" command : id = "+ id +" ***\n"); |
127 | 127 |
128 tuple = new Tuple(); | 128 tuple = new Tuple(); |
129 tuple.setMode(mode); | 129 tuple.setMode(mode); |
130 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); | 130 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); |
131 command.rewind(); | 131 command.rewind(); |
137 ByteBuffer buff = ByteBuffer.allocate(0); | 137 ByteBuffer buff = ByteBuffer.allocate(0); |
138 tuple.setData(buff); | 138 tuple.setData(buff); |
139 tuple.next = tuple_space[id]; | 139 tuple.next = tuple_space[id]; |
140 tuple_space[id] = tuple; | 140 tuple_space[id] = tuple; |
141 if(debug){ | 141 if(debug){ |
142 System.out.println("data inserted insert seq = "+seq +", id = "+id); | 142 fds.log(Level.INFO, "data inserted insert seq = "+seq +", id = "+id); |
143 } | 143 } |
144 } | 144 } |
145 | 145 |
146 protected void In_Rd(SelectionKey key, ByteBuffer command, int mode) | 146 protected void In_Rd(SelectionKey key, ByteBuffer command, int mode) |
147 throws IOException { | 147 throws IOException { |
165 char idc = (char)command.getShort(PSX.LINDA_ID_OFFSET); | 165 char idc = (char)command.getShort(PSX.LINDA_ID_OFFSET); |
166 id = (int)idc; | 166 id = (int)idc; |
167 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); | 167 int seq = command.getInt(PSX.LINDA_SEQ_OFFSET); |
168 command.rewind(); | 168 command.rewind(); |
169 | 169 |
170 if (debug) System.out.println("*** "+(char)mode+" command : id = "+ id +" ***\n"); | 170 if (debug) fds.log(Level.INFO, "*** "+(char)mode+" command : id = "+ id +" ***\n"); |
171 hook.inHook(key,id,seq,(char)mode); | 171 hook.inHook(key,id,seq,(char)mode); |
172 | 172 |
173 tuple = tuple_space[id]; | 173 tuple = tuple_space[id]; |
174 | 174 |
175 //wを無視 | 175 //wを無視 |
213 int id, Tuple temp) { | 213 int id, Tuple temp) { |
214 tuple.setCommand('a', tuple.seq); | 214 tuple.setCommand('a', tuple.seq); |
215 | 215 |
216 if(debug){ | 216 if(debug){ |
217 int sendsize = tuple.getdataLength()+PSX.LINDA_HEADER_SIZE; | 217 int sendsize = tuple.getdataLength()+PSX.LINDA_HEADER_SIZE; |
218 System.out.println("send size "+sendsize+" : mode = "+(char)tuple.getMode()); | 218 fds.log(Level.INFO, "send size "+sendsize+" : mode = "+(char)tuple.getMode()); |
219 } | 219 } |
220 //INの場合はremoveする | 220 //INの場合はremoveする |
221 if(mode == PSX.PSX_IN) { | 221 if(mode == PSX.PSX_IN) { |
222 if(temp != null){ | 222 if(temp != null){ |
223 temp.next = tuple.next; | 223 temp.next = tuple.next; |
247 ByteBuffer buff = ByteBuffer.allocate(0); | 247 ByteBuffer buff = ByteBuffer.allocate(0); |
248 tuple.setData(buff); | 248 tuple.setData(buff); |
249 tuple = null; | 249 tuple = null; |
250 | 250 |
251 if(debug){ | 251 if(debug){ |
252 System.out.println("wait inserted seq = "+seq +", id = "+id); | 252 fds.log(Level.INFO, "wait inserted seq = "+seq +", id = "+id); |
253 } | 253 } |
254 return tuple; | 254 return tuple; |
255 } | 255 } |
256 | 256 |
257 protected void Check(SelectionKey key, ByteBuffer command) throws IOException { | 257 protected void Check(SelectionKey key, ByteBuffer command) throws IOException { |