Mercurial > hg > RemoteEditor > REPSessionManager
annotate rep/SessionManager.java @ 366:e391433fa9f1
*** empty log message ***
author | kono |
---|---|
date | Mon, 20 Oct 2008 17:58:02 +0900 |
parents | c432755c3555 |
children | 1bde894edd83 |
rev | line source |
---|---|
266 | 1 |
0 | 2 package rep; |
3 | |
4 import java.io.IOException; | |
5 import java.net.InetSocketAddress; | |
267 | 6 import java.nio.channels.ClosedChannelException; |
0 | 7 import java.nio.channels.SelectionKey; |
83 | 8 import java.util.LinkedList; |
144 | 9 import java.util.List; |
231 | 10 import java.util.Set; |
178 | 11 import java.util.concurrent.BlockingQueue; |
192 | 12 import java.util.concurrent.LinkedBlockingQueue; |
0 | 13 |
346 | 14 import org.xml.sax.SAXException; |
15 | |
198 | 16 |
353 | 17 |
337 | 18 import rep.channel.REPLogger; |
123 | 19 import rep.channel.REPServerSocketChannel; |
133 | 20 import rep.channel.REPSocketChannel; |
144 | 21 import rep.handler.PacketSet; |
146 | 22 import rep.handler.REPHandler; |
158 | 23 import rep.channel.REPSelector; |
56 | 24 import rep.xml.SessionXMLDecoder; |
45 | 25 import rep.xml.SessionXMLEncoder; |
198 | 26 import rep.channel.REPSelectionKey; |
264 | 27 |
198 | 28 /* |
264 | 29 +-------+--------+--------+-------+--------+---------+------+ |
30 | cmd | session| editor | seqid | lineno | textsiz | text | | |
31 | | id | id | | | | | | |
32 +-------+--------+--------+-------+--------+---------+------+ | |
33 o---------- header section (network order) ----------o | |
34 | |
35 int cmd; kind of command | |
36 int sid; session ID : uniqu to editing file | |
37 int eid; editor ID : owner editor ID = 1。Session に対して unique | |
308 | 38 -1 session manager command |
39 -2 merge command | |
264 | 40 int seqno; Sequence number : sequence number はエディタごとに管理 |
41 int lineno; line number | |
42 int textsize; textsize : bytesize | |
43 byte[] text; | |
198 | 44 */ |
1 | 45 |
250 | 46 public class SessionManager implements SessionManagerEventListener{ |
337 | 47 static public REPLogger logger = REPLogger.singleton(); |
48 | |
363 | 49 SessionList sessionList; |
280 | 50 private SessionManagerGUI gui; |
363 | 51 // Main nio.Selector of this server |
198 | 52 private REPSelector<REPCommand> selector; |
363 | 53 // Known Session Manager List, At most one parent. No parent means master. |
319 | 54 SessionManagerList smList; |
363 | 55 // Known Editor list. Connected Editor has a channel. |
56 // Session Manager Channel may have dummy editors. | |
356 | 57 EditorList editorList; |
363 | 58 // Commands for busy editor are kept in this queue. |
212 | 59 private List<PacketSet> waitingCommandInMerge; |
363 | 60 // Command from gui. Synchronization is required. |
61 private BlockingQueue<SessionManagerEvent> waitingEventQueue | |
62 = new LinkedBlockingQueue<SessionManagerEvent>();; | |
63 // host name of this server. One of connecting SocketChannel's hostname | |
319 | 64 String myHost; |
363 | 65 // Single threaded write queueu. To avoid dead lock with too many writes. |
317 | 66 private LinkedList<PacketSet> writeQueue = new LinkedList<PacketSet>(); |
336 | 67 private int receive_port; |
68 private int parent_port; | |
101 | 69 static final int DEFAULT_PORT = 8766; |
363 | 70 // Queue limit for debugging purpose. |
332 | 71 private static final int packetLimit = 200; |
358 | 72 |
363 | 73 // globalSessionID = SessionManagerID * MAXID + localSessionID |
358 | 74 private static final int MAXID = 10000; |
353 | 75 SessionXMLDecoder decoder = new SessionXMLDecoder(); |
358 | 76 SessionXMLEncoder encoder = new SessionXMLEncoder(); |
363 | 77 // SocketChannel for our parent. At most one parent is allowed. |
355 | 78 private Forwarder sm_join_channel; |
363 | 79 // Routing table for session and session manager. |
358 | 80 private RoutingTable routingTable = new RoutingTable(); |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
81 private SessionManagerEvent execAfterConnect = null;; |
358 | 82 |
316 | 83 public static void main(String[] args) throws InterruptedException, IOException { |
84 | |
85 int port = DEFAULT_PORT; | |
86 int port_s = DEFAULT_PORT; | |
87 //System.setProperty("file.encoding", "UTF-8"); | |
88 if(args.length > 0){ | |
363 | 89 if (args.length!=2) { |
90 logger.writeLog("Usage: sessionManager our_port parent_port"); | |
91 return; | |
92 } | |
316 | 93 port = Integer.parseInt(args[0]); |
94 port_s = Integer.parseInt(args[1]); | |
95 } | |
96 SessionManager sm = new SessionManager(); | |
336 | 97 sm.setReceivePort(port); |
98 sm.setParentPort(port_s); | |
363 | 99 // Ok start main loop |
316 | 100 sm.init(port,new SessionManagerGUIimpl(sm)); |
101 } | |
102 | |
336 | 103 public void setReceivePort(int port) { |
104 receive_port = port; | |
105 } | |
106 | |
280 | 107 public void init(int port, SessionManagerGUI gui) throws IOException, InterruptedException { |
108 this.gui = gui; | |
109 init(port); | |
110 mainLoop(); | |
111 } | |
112 | |
113 private void init(int port) throws InterruptedException, IOException { | |
363 | 114 selector = REPSelector.<REPCommand>create(); |
186 | 115 REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open(new REPCommandPacker()); |
363 | 116 ssc.configureBlocking(false); // Selector requires this |
117 ssc.socket().setReuseAddress(true); //reuse address 必須 | |
212 | 118 //getAllByNameで取れた全てのアドレスに対してbindする |
119 ssc.socket().bind(new InetSocketAddress(port)); | |
349 | 120 ssc.register(selector, SelectionKey.OP_ACCEPT, |
121 new Forwarder(this)); | |
6 | 122 |
358 | 123 sessionList = new SessionList(); |
7 | 124 smList = new SessionManagerList(); |
356 | 125 editorList = new EditorList(); |
212 | 126 waitingCommandInMerge = new LinkedList<PacketSet>(); |
228 | 127 |
215 | 128 |
155 | 129 } |
313 | 130 |
131 /* | |
363 | 132 * The main loop. |
133 * Check incoming events and waiting writes. | |
134 * Do select and call select() to check in coming packets. | |
313 | 135 * We wrote everything in one thread, but we can assign |
136 * one thread for each communication channel and GUI event. | |
137 */ | |
231 | 138 public void mainLoop() throws IOException { |
0 | 139 while(true){ |
328 | 140 checkWaitingCommandInMerge(); |
313 | 141 if (checkInputEvent() || |
328 | 142 checkWaitingWrite()) { |
313 | 143 // try to do fair execution for waiting task |
144 if(selector.selectNow() > 0) select(); | |
145 continue; | |
300 | 146 } |
313 | 147 // now we can wait for input packet or event |
233 | 148 selector.select(); |
144 | 149 select(); |
150 } | |
151 } | |
152 | |
363 | 153 /* |
154 * Synchronize GUI event in the main loop. | |
155 */ | |
313 | 156 private boolean checkInputEvent() { |
157 SessionManagerEvent e; | |
158 if((e = waitingEventQueue.poll())!=null){ | |
334 | 159 e.exec(this); |
313 | 160 return true; |
161 } | |
162 return false; | |
163 } | |
164 | |
363 | 165 /* |
166 * Write a packet during the main loop. | |
167 */ | |
313 | 168 private boolean checkWaitingWrite() throws IOException { |
317 | 169 PacketSet p = writeQueue.poll(); |
170 if (p!=null) { | |
327 | 171 p.channel.write(p.command); |
317 | 172 return true; |
313 | 173 } |
174 return false; | |
175 } | |
176 | |
308 | 177 /** |
178 * Check waiting command in merge | |
179 * @return true if there is a processed waiting command | |
180 * @throws IOException | |
181 */ | |
346 | 182 private void checkWaitingCommandInMerge() { |
328 | 183 List<PacketSet> w = waitingCommandInMerge; |
184 waitingCommandInMerge = new LinkedList<PacketSet>(); | |
185 for(PacketSet p: w) { | |
186 Editor e = p.getEditor(); | |
353 | 187 if(e.isMerging()) { // still merging do nothing |
328 | 188 waitingCommandInMerge.add(p); |
189 } else { | |
346 | 190 try { |
353 | 191 if (sessionManage(e, p.command)) { // we don't need this |
192 assert false; | |
193 return; | |
194 } | |
195 e.manage(p.command); | |
347 | 196 } catch (Exception e1) { |
349 | 197 // should be e.close()? |
346 | 198 close(p.channel); |
199 } | |
328 | 200 } |
201 } | |
202 } | |
363 | 203 |
204 /* | |
205 * If we have waiting write commands, further sent commands also | |
206 * wait to avoid out of order packet sending. | |
207 */ | |
208 public boolean hasWaitingCommand(REPSocketChannel<REPCommand>c) { | |
209 for(PacketSet p:waitingCommandInMerge) { | |
210 if (p.channel==c) { | |
211 return true; | |
212 } | |
213 } | |
214 return false; | |
215 } | |
328 | 216 |
363 | 217 /* |
218 * Close a channel in case of exception or close. | |
219 */ | |
346 | 220 private void close(REPSocketChannel<REPCommand> channel) { |
221 REPSelectionKey<REPCommand>key = channel.keyFor1(selector); | |
222 REPHandler handler = (REPHandler)key.attachment(); | |
223 key.cancel(); | |
224 handler.cancel(channel); | |
225 // we have to remove session/enditor | |
226 } | |
227 | |
228 | |
363 | 229 /* |
230 * Do select operation on the Selector. Each key has a forwarder. | |
231 * A forwarder can be a firstConnector, a forwarder for Session Manager | |
232 * or an Editor. | |
233 */ | |
144 | 234 private void select() throws IOException { |
231 | 235 |
236 Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1(); | |
237 for(REPSelectionKey<REPCommand> key : keys){ | |
144 | 238 if(key.isAcceptable()){ |
363 | 239 /* |
240 * Incoming connection. We don't know which, editor or | |
241 * session manager. Assign FirstConnector to distinguish. | |
242 */ | |
199 | 243 REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker()); |
337 | 244 logger.writeLog("SessionManager.select() : key.isAcceptable : channel = " + channel); |
363 | 245 registerChannel(channel, new FirstConnector(this)); |
144 | 246 channel = null; |
247 }else if(key.isReadable()){ | |
363 | 248 /* |
249 * Incoming packets are handled by a various forwarder. | |
250 * A hadler throw IOException() in case of a trouble to | |
251 * close the channel. | |
252 */ | |
212 | 253 REPHandler handler = (REPHandler)(key.attachment()); |
267 | 254 try { |
255 handler.handle(key); | |
358 | 256 } catch (IOException e) { |
267 | 257 key.cancel(); |
308 | 258 handler.cancel(key.channel1()); |
267 | 259 } |
0 | 260 } |
261 } | |
262 } | |
1 | 263 |
355 | 264 void registerChannel(REPSocketChannel<REPCommand> channel,Forwarder handler) throws IOException { |
2 | 265 if(channel == null) { |
266 return; | |
267 } | |
349 | 268 handler.setChannel(channel); |
2 | 269 channel.configureBlocking(false); |
355 | 270 channel.register(selector, SelectionKey.OP_READ, handler); |
2 | 271 } |
272 | |
363 | 273 /* |
274 * After loop detection, we give up session manager join. | |
275 */ | |
276 private void cancel_sm_join() { | |
364 | 277 logger.writeLog("Loop detected "+this); |
355 | 278 removeChannel(sm_join_channel); |
279 sm_join_channel=null; | |
280 } | |
281 | |
282 | |
363 | 283 private void removeChannel(Forwarder channel) { |
284 REPSelectionKey<REPCommand> key = channel.channel.keyFor1(selector); | |
355 | 285 key.cancel(); |
286 try { | |
364 | 287 channel.channel.close1(); |
355 | 288 } catch (IOException e) { |
289 } | |
349 | 290 } |
291 | |
320 | 292 |
319 | 293 void updateGUI() { |
212 | 294 //リストのコピーをGUIに渡す |
358 | 295 LinkedList<Session> sList = new LinkedList<Session>(sessionList.values()); |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
296 LinkedList<Editor> eList; |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
297 if (false) { |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
298 // local editor only |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
299 eList = new LinkedList<Editor>(); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
300 for(Editor e:editorList.values()) { |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
301 if (getSMID(e.eid)==smList.sessionManagerID()) { |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
302 eList.add(e); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
303 } |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
304 } |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
305 } else { |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
306 eList = new LinkedList<Editor>(editorList.values()); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
307 } |
212 | 308 //GUIに反映 |
309 Runnable doRun = new DoGUIUpdate(sList, eList, gui); | |
279 | 310 gui.invokeLater(doRun); |
212 | 311 } |
312 | |
83 | 313 |
139 | 314 |
353 | 315 void setMyHostName(String localHostName) { |
308 | 316 myHost = localHostName + receive_port; |
164 | 317 setHostToEditor(myHost); |
318 } | |
319 | |
320 private void setHostToEditor(String myHost2) { | |
358 | 321 for(Editor editor : editorList.values()){ |
322 if (editor.channel!=null) | |
323 editor.setHost(myHost2); | |
164 | 324 } |
76 | 325 } |
0 | 326 |
355 | 327 |
328 /** | |
363 | 329 * GUI から、呼ばれて、Session Managerに接続する。 |
355 | 330 * Host 名のSession Manager に SM_JOIN する。自分は、Session を持っていては |
331 * ならない。複数のSession Managerにjoinすることは出来ない。(NATを実装するまでは)。 | |
332 * @param host | |
333 */ | |
364 | 334 public void connectSession(String host, int port) { |
355 | 335 if (sm_join_channel!=null) return; |
336 if (!sessionList.isEmpty()) return; | |
337 if (!smList.isMaster()) return; | |
363 | 338 /* |
339 * IPv6 対応では、複数のアドレスを取って、それのすべてに接続を試す必要が | |
340 * ある。 | |
341 */ | |
1 | 342 InetSocketAddress addr = new InetSocketAddress(host, port); |
343 try { | |
186 | 344 REPSocketChannel<REPCommand> sessionchannel = REPSocketChannel.<REPCommand>create(new REPCommandPacker()); |
1 | 345 sessionchannel.connect(addr); |
337 | 346 while(!sessionchannel.finishConnect()); |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
347 Forwarder sm = new FirstConnector(this); |
355 | 348 registerChannel(sessionchannel, sm); |
349 | 349 sm_join(sm); |
1 | 350 }catch (IOException e) { |
351 } | |
352 } | |
364 | 353 |
354 public void connectSession(String host) { | |
355 connectSession(host,parent_port); | |
356 } | |
77 | 357 |
363 | 358 /** |
359 * channel に SMCMD_SM_JOIN command を送る。 | |
360 * @param channel | |
361 */ | |
349 | 362 private void sm_join(Forwarder channel){ |
355 | 363 sm_join_channel = channel; |
122 | 364 //SM_JOINコマンドを生成。 |
77 | 365 REPCommand command = new REPCommand(); |
366 command.setCMD(REP.SMCMD_SM_JOIN); | |
349 | 367 command.setEID(-1); // request Parent SessionManagerID |
368 command.setSID(-1); // request SessionManagerID | |
79 | 369 |
122 | 370 //hostnameをセット。 |
349 | 371 setMyHostName(channel.getLocalHostName()); |
82 | 372 |
355 | 373 String string = myHost; |
77 | 374 command.setString(string); |
375 | |
122 | 376 //SM_JOINコマンドを送信。 |
349 | 377 channel.send(command); |
363 | 378 // ack を受け取ったら、SessionManagerのListに追加。ここではやらない。 |
77 | 379 } |
349 | 380 |
361 | 381 /* |
382 * Select Session from Manager button | |
383 * selected editor is joined editor directly connected to this session | |
384 * manager. | |
385 */ | |
316 | 386 public void selectSession(SelectButtonEvent event) throws IOException { |
250 | 387 int sid = event.getSID(); |
358 | 388 Session session = sessionList.get(sid); |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
389 if (session==null) throw new IOException(); |
320 | 390 Editor editor = (Editor)event.getEditor(); |
324 | 391 if (editor.hasSession()) return; |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
392 // assert(getSMID(editor.eid)==smList.sessionManagerID()); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
393 // assert(editor.channel!=null); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
394 editor.setSID(sid); // mark as selected |
358 | 395 selectSession(sid, session, editor.getEID(), editor); |
396 } | |
397 | |
361 | 398 /* |
399 * Select Session Protocol handler | |
363 | 400 * called from GUI or incoming SMCMD_SELECT command. |
361 | 401 */ |
358 | 402 private void selectSession(int sid, Session session, int eid, Forwarder editor) { |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
403 if(session.hasOwner() && editor.channel!=null){ |
363 | 404 // we have selected session. |
107 | 405 REPCommand sendCommand = new REPCommand(); |
358 | 406 if (editor.isDirect()&&editor.getEID()==eid) { |
363 | 407 // Found directly connected joined editor. Send join_ack(). |
361 | 408 session.addForwarder(editor); |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
409 sendUpdate(session.getSID()); |
358 | 410 sendCommand.setCMD(REP.SMCMD_JOIN_ACK); |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
411 return; |
358 | 412 } else { |
363 | 413 // We have a session, but joined editor is on the other sm. |
358 | 414 // SELECT_ACK is sent to the session ring to |
363 | 415 // find out the joined editor. |
358 | 416 sendCommand.setCMD(REP.SMCMD_SELECT_ACK); |
361 | 417 // Do not directly addForwarder(forwarder). It may be |
418 // shared among sessions. | |
419 Forwarder f = new Editor(this, false, makeID(editorList.newEid())); | |
420 f.setChannel(editor.channel); // incoming channel | |
421 f.setHost(myHost); | |
422 f.setSID(sid); | |
363 | 423 session.addForwarder(f); // f.next is set up here. |
358 | 424 } |
148 | 425 sendCommand.setEID(editor.getEID()); |
107 | 426 sendCommand.setSID(sid); |
361 | 427 sendCommand.string = session.getName(); |
349 | 428 editor.send(sendCommand); |
107 | 429 }else { |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
430 Forwarder next; |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
431 if (editor.channel==null) { |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
432 next = routingTable.toSessionManager(getSMID(eid)); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
433 } else { |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
434 // session searching continue... |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
435 next = routingTable.toSession(sid); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
436 // create dummy editor for this session |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
437 Forwarder f = new Editor(this, false, makeID(editorList.newEid())); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
438 f.setChannel(editor.channel); // incoming channel |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
439 f.setNext(next); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
440 f.setHost(myHost); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
441 f.setSID(sid); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
442 session.setFirstForwarder(f); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
443 } |
363 | 444 // pass the select command to the next path. |
107 | 445 REPCommand command = new REPCommand(); |
446 command.setCMD(REP.SMCMD_SELECT); | |
447 command.setSID(sid); | |
358 | 448 command.setEID(eid); |
178 | 449 command.setString(editor.getHost()); |
358 | 450 next.send(command); |
107 | 451 } |
8 | 452 } |
122 | 453 |
363 | 454 /* |
455 * Create and send UPDATE command. | |
456 */ | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
457 private void sendUpdate(int sid) { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
458 REPCommand command = makeREPCommandWithSessionList(REP.SMCMD_UPDATE); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
459 command.setSID(sid); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
460 command.setEID(REP.SM_EID.id); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
461 smList.sendToMaster(command); |
358 | 462 } |
463 | |
363 | 464 /* |
465 * Create new editor in this sessin manager. A dummy editor | |
466 * is created also. | |
467 */ | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
468 public Editor newEditor(REPSocketChannel<REPCommand> channel) { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
469 int eid = makeID(editorList.newEid()); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
470 Editor editor = new Editor(this, eid, channel); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
471 editorList.add(editor); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
472 return editor; |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
473 } |
363 | 474 |
475 /* | |
476 * Create new session. | |
477 */ | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
478 public Session newSession(Forwarder master) { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
479 int sid= makeID(sessionList.newSessionID()); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
480 Session session = new Session(sid, master); |
360 | 481 sessionList.put(sid, session); |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
482 return session; |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
483 } |
358 | 484 |
144 | 485 public void addWaitingCommand(PacketSet set) { |
212 | 486 waitingCommandInMerge.add(set); |
144 | 487 } |
148 | 488 |
363 | 489 /* |
490 * Synchronize GUI command in this session manager. | |
491 */ | |
222 | 492 public void buttonPressed(SessionManagerEvent event) { |
493 try { | |
308 | 494 waitingEventQueue.put(event); |
222 | 495 } catch (InterruptedException e) {} |
496 selector.wakeup(); | |
497 } | |
281 | 498 |
363 | 499 /* |
500 * Execute incoming event during the initialization for | |
501 * testing purpose. | |
502 */ | |
281 | 503 public void syncExec(SessionManagerEvent event) { |
504 try { | |
308 | 505 waitingEventQueue.put(event); |
281 | 506 } catch (InterruptedException e) { |
507 } | |
508 } | |
222 | 509 |
363 | 510 /* |
511 * GUI command interface for close session. | |
512 */ | |
259 | 513 public void closeSession(SessionManagerEvent event) { |
514 Session session = ((CloseButtonEvent) event).getSession(); | |
515 session.closeSession(); | |
516 sessionList.remove(session); | |
517 updateGUI(); | |
518 } | |
519 | |
363 | 520 /* |
521 * Remove editors which has the cannel. | |
522 */ | |
274 | 523 public void remove(REPSocketChannel<REPCommand> channel) { |
358 | 524 int i = 0; |
525 for(Session s:sessionList.values()) { | |
274 | 526 if (s.deleteEditor(channel)) { |
358 | 527 i++; |
274 | 528 } |
529 } | |
358 | 530 assert(i==1); |
274 | 531 // can be other session manager? what should I do? |
532 } | |
533 | |
317 | 534 |
535 public void addWriteQueue(PacketSet packetSet) { | |
324 | 536 writeQueue.addLast(packetSet); |
323 | 537 assert(writeQueue.size()<packetLimit) ; |
317 | 538 } |
539 | |
318 | 540 |
541 public void remove(Editor editor) { | |
358 | 542 Session s = sessionList.get(editor.getSID()); |
362 | 543 if (s==null) { |
544 assert(false); | |
545 editorList.remove(editor); | |
546 } else if (editor.isMaster()) { | |
358 | 547 removeSession(s); |
548 } else { | |
549 s.deleteForwarder(editor); | |
550 editorList.remove(editor); | |
318 | 551 } |
341 | 552 updateGUI(); |
553 } | |
554 | |
555 private void removeSession(Session s0) { | |
358 | 556 s0.remove(this); |
341 | 557 sessionList.remove(s0); |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
558 sendUpdate(s0.getSID()); |
318 | 559 } |
560 | |
334 | 561 public void setParentPort(int port) { |
562 parent_port = port; | |
563 } | |
564 public int getParentPort() { | |
565 return parent_port; | |
566 } | |
567 | |
568 public int getPort() { | |
569 return receive_port; | |
570 } | |
571 | |
353 | 572 |
355 | 573 boolean sessionManage(Forwarder forwarder, REPCommand command) throws ClosedChannelException, |
358 | 574 IOException { |
355 | 575 switch(command.cmd){ |
353 | 576 |
577 // Session Manager Command | |
578 | |
579 case SMCMD_JOIN: | |
580 { | |
358 | 581 // first connection or forwarded command |
582 if(isMaster()) { | |
583 REPCommand ackCommand = new REPCommand(); | |
584 ackCommand.setCMD(REP.SMCMD_JOIN_ACK); | |
585 ackCommand.setEID(command.eid); | |
360 | 586 ackCommand.setSID(command.sid); |
358 | 587 ackCommand.string = command.string; |
588 smList.sendToSlaves(ackCommand); | |
589 registEditor(forwarder,ackCommand); | |
590 } else { | |
591 routingTable.add(forwarder,getSMID(command.eid),command.sid); | |
592 smList.sendToMaster(command); | |
593 } | |
360 | 594 updateGUI(); |
353 | 595 } |
596 | |
597 break; | |
598 | |
361 | 599 case SMCMD_PUT_ACK: |
366 | 600 if (forwarder.isDirect()) { |
601 // send put_ack to the editor now. | |
602 command.setCMD(REP.SMCMD_PUT_ACK); | |
603 command.string = command.string; | |
604 command.setEID(command.eid); | |
605 command.setSID(command.sid); | |
606 forwarder.send(command); | |
607 } | |
353 | 608 case SMCMD_JOIN_ACK: |
358 | 609 registEditor(forwarder,command); |
360 | 610 updateGUI(); |
353 | 611 break; |
612 | |
613 case SMCMD_PUT: | |
614 { | |
358 | 615 // first connection or forwarded command |
366 | 616 routingTable.add(forwarder,getSMID(command.eid),command.sid); |
358 | 617 if(isMaster()) { |
618 command.setCMD(REP.SMCMD_PUT_ACK); | |
619 command.string = command.string; | |
620 command.setEID(command.eid); | |
621 command.setSID(command.sid); | |
622 smList.sendToSlaves(command); | |
623 registEditor(forwarder,command); | |
624 } else { | |
625 smList.sendToMaster(command); | |
361 | 626 // registEditor will be done by SMCMD_PUT_ACK |
627 } | |
360 | 628 updateGUI(); |
353 | 629 |
630 } | |
631 break; | |
358 | 632 |
353 | 633 case SMCMD_SELECT: |
361 | 634 { |
635 Session session = sessionList.get(command.sid); | |
636 if (session==null) { | |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
637 session = new Session(command.sid, command.string,null); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
638 sessionList.put(command.sid,session); |
361 | 639 } |
640 selectSession(command.sid, session, command.eid, forwarder); | |
641 } | |
642 break; | |
353 | 643 case SMCMD_SELECT_ACK: |
644 { | |
358 | 645 Session session = sessionList.get(command.sid); |
361 | 646 selectSession(command.sid, session, command.eid, |
647 session.getFirstForwarder()); | |
353 | 648 } |
358 | 649 break; |
650 | |
353 | 651 case SMCMD_SM_JOIN: |
652 { | |
355 | 653 // SM_JOIN中にSMCMD_SM_JOINが来たら、これはループなので、 |
654 ///自分のSM_JOINを取り消す。 | |
655 if (sm_join_channel!=null) cancel_sm_join(); | |
353 | 656 // SMCMD_SM_JOIN は、master まで上昇する。 |
657 // masterでなければ、自分のparentに転送する。 | |
355 | 658 if(isMaster()) { |
353 | 659 // master であれば、SessionManager IDを決めて、 |
660 // 自分のsmList に登録 | |
355 | 661 Forwarder sm; |
662 int psid = command.eid; | |
663 if (forwarder.sid!=-1) { | |
664 // すでに channelはSessionManager Idを持っていて、 | |
665 // direct link ではないので、 | |
666 // channel を持たないForwarderとして登録する | |
667 sm = new Forwarder(this); | |
668 } else { | |
669 sm = forwarder; | |
670 } | |
671 int sid = smList.addNewSessionManager(sm,command); | |
672 REPCommand sendCommand = makeREPCommandWithSessionList(REP.SMCMD_SM_JOIN_ACK); | |
673 // command.eid==smList.sesionManagerID() の場合は、 | |
674 // 待っている自分の下のsessionManagerにsidをassignする必要がある。 | |
353 | 675 sendCommand.setSID(sid); // new Session manager ID |
676 // 複数のSM_JOIN_ACKを識別するには、最初にSM_JOINを受け付けた | |
677 // Session manager IDを使う。 | |
355 | 678 sendCommand.setEID(psid); |
679 send_sm_join_ack(psid, sid, sendCommand); | |
680 } else { | |
681 if (forwarder.sid==-1) { | |
682 // direct link の場合は、識別のために、EIDに直上の | |
683 // smid を入れておく。 | |
684 command.setEID(smList.sessionManagerID()); | |
685 } | |
686 smList.sendToMaster(command); | |
353 | 687 } |
688 } | |
689 break; | |
690 | |
355 | 691 case SMCMD_SM_JOIN_ACK: |
692 send_sm_join_ack(command.eid, command.sid, command); | |
353 | 693 break; |
694 | |
695 case SMCMD_UPDATE: | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
696 if (!isMaster()) { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
697 command.setString(mergeUpdate(command)); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
698 // 上に知らせる |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
699 smList.sendToMaster(command); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
700 break; |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
701 } |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
702 // fall thru |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
703 command.setCMD(REP.SMCMD_UPDATE_ACK); |
353 | 704 case SMCMD_UPDATE_ACK: |
358 | 705 command.setString(mergeUpdate(command)); |
706 // 下に知らせる | |
355 | 707 smList.sendToSlaves(command); |
358 | 708 updateGUI(); |
353 | 709 break; |
710 default: | |
711 return false; | |
712 } | |
713 return true; | |
714 } | |
715 | |
355 | 716 |
363 | 717 /** |
718 * UPDATE/UPDATE_ACKにより送られてきたSessionの情報を追加する | |
719 * @param command | |
720 * @return | |
721 * @throws IOException | |
722 */ | |
358 | 723 private String mergeUpdate(REPCommand command) throws IOException { |
724 SessionList receivedSessionList; | |
725 try { | |
726 receivedSessionList = decoder.decode(command.string); | |
727 } catch (SAXException e) { | |
728 throw new IOException(); | |
729 } | |
363 | 730 // 受け取った情報と自分の情報を混ぜる。 |
731 sessionList.merge(receivedSessionList); | |
358 | 732 //XMLを生成。送信コマンドにセット。 |
733 return encoder.sessionListToXML(sessionList); | |
734 | |
735 } | |
736 | |
737 /* | |
738 * id has SessionManager ID part | |
739 */ | |
740 private int makeID(int newid) { | |
741 return newid+smList.sessionManagerID()*MAXID; | |
742 } | |
743 | |
744 private int getSMID(int id) { | |
745 return id/MAXID; | |
746 } | |
747 | |
748 | |
361 | 749 /** |
750 * Register Editor to our editorList. No connection is made. | |
751 * @param forwarder Editor to be add | |
752 * @param command | |
753 */ | |
358 | 754 private void registEditor(Forwarder forwarder,REPCommand command) { |
755 // make ack for PUT/JOIN. Do not send this to the editor, | |
756 // before select. After select, ack is sent to the editor. | |
757 routingTable.add(forwarder,getSMID(command.eid),command.sid); | |
758 Editor editor; | |
361 | 759 if (getSMID(command.sid)==smList.sessionManagerID() |
358 | 760 && forwarder.isDirect()) { |
761 // direct link だった | |
762 editor = (Editor)forwarder; | |
763 } else { | |
764 editor = new Editor(this, command.cmd==REP.SMCMD_PUT_ACK, command.eid); | |
765 } | |
766 editor.setName(command.string); | |
767 editor.setSID(command.sid); | |
768 if (!editorList.hasEid(command.eid)) { | |
769 editorList.add(editor); | |
770 } | |
771 // we don't join ack to the direct linked editor. We | |
772 // have to wait select command | |
773 } | |
774 | |
775 | |
355 | 776 void send_sm_join_ack(int psid, int sid,REPCommand sendCommand) { |
777 if (psid==smList.sessionManagerID()) { | |
778 // 直下のsessionManagerにIDを割り振る必要がある。 | |
779 smList.assignSessionManagerIDtoWaitingSM(sid); | |
780 // ここで smList に一つだけ追加されるので | |
358 | 781 // 待っている最初のsm一つにだけ、sm_join_ackが新たに送られる。 |
355 | 782 } |
783 smList.sendToSlaves(sendCommand); | |
784 } | |
785 | |
786 | |
787 private REPCommand makeREPCommandWithSessionList(REP cmd) { | |
788 //SessionListからXMLを生成。 | |
789 //joinしてきたSessionManagerに対してACKを送信。 | |
790 REPCommand sendCommand = new REPCommand(); | |
791 sendCommand.setCMD(cmd); | |
358 | 792 sendCommand.setString(encoder.sessionListToXML(sessionList)); |
355 | 793 return sendCommand; |
794 } | |
795 | |
796 | |
797 public boolean isMaster() { | |
798 return smList.isMaster(); | |
799 } | |
800 | |
801 | |
802 public void setSessionManagerID(int sid) { | |
803 smList.setSessionManagerID(sid); | |
804 } | |
805 | |
358 | 806 |
807 public Session getSession(int sid) { | |
808 return sessionList.get(sid); | |
809 } | |
810 | |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
811 public void execAfterConnect(SessionManagerEvent sessionManagerEvent) { |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
812 execAfterConnect = sessionManagerEvent; |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
813 } |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
814 |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
815 public void afterConnect() { |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
816 if (execAfterConnect!=null) execAfterConnect.exec(this); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
817 execAfterConnect = null; |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
818 } |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
819 |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
820 public void setParent(Forwarder fw) { |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
821 smList.setParent(fw); |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
822 } |
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
364
diff
changeset
|
823 |
364 | 824 |
0 | 825 } |