comparison rep/SessionManager.java @ 365:c432755c3555

distributed session debug continue... SELECT/SELECT_ACK loop
author kono
date Mon, 20 Oct 2008 17:00:42 +0900
parents c965ef2b5fd6
children e391433fa9f1
comparison
equal deleted inserted replaced
364:c965ef2b5fd6 365:c432755c3555
76 SessionXMLEncoder encoder = new SessionXMLEncoder(); 76 SessionXMLEncoder encoder = new SessionXMLEncoder();
77 // SocketChannel for our parent. At most one parent is allowed. 77 // SocketChannel for our parent. At most one parent is allowed.
78 private Forwarder sm_join_channel; 78 private Forwarder sm_join_channel;
79 // Routing table for session and session manager. 79 // Routing table for session and session manager.
80 private RoutingTable routingTable = new RoutingTable(); 80 private RoutingTable routingTable = new RoutingTable();
81 private SessionManagerEvent execAfterConnect = null;;
81 82
82 public static void main(String[] args) throws InterruptedException, IOException { 83 public static void main(String[] args) throws InterruptedException, IOException {
83 84
84 int port = DEFAULT_PORT; 85 int port = DEFAULT_PORT;
85 int port_s = DEFAULT_PORT; 86 int port_s = DEFAULT_PORT;
290 291
291 292
292 void updateGUI() { 293 void updateGUI() {
293 //リストのコピーをGUIに渡す 294 //リストのコピーをGUIに渡す
294 LinkedList<Session> sList = new LinkedList<Session>(sessionList.values()); 295 LinkedList<Session> sList = new LinkedList<Session>(sessionList.values());
295 LinkedList<Editor> eList = new LinkedList<Editor>(editorList.values()); 296 LinkedList<Editor> eList;
297 if (false) {
298 // local editor only
299 eList = new LinkedList<Editor>();
300 for(Editor e:editorList.values()) {
301 if (getSMID(e.eid)==smList.sessionManagerID()) {
302 eList.add(e);
303 }
304 }
305 } else {
306 eList = new LinkedList<Editor>(editorList.values());
307 }
296 //GUIに反映 308 //GUIに反映
297 Runnable doRun = new DoGUIUpdate(sList, eList, gui); 309 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
298 gui.invokeLater(doRun); 310 gui.invokeLater(doRun);
299 } 311 }
300 312
330 InetSocketAddress addr = new InetSocketAddress(host, port); 342 InetSocketAddress addr = new InetSocketAddress(host, port);
331 try { 343 try {
332 REPSocketChannel<REPCommand> sessionchannel = REPSocketChannel.<REPCommand>create(new REPCommandPacker()); 344 REPSocketChannel<REPCommand> sessionchannel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
333 sessionchannel.connect(addr); 345 sessionchannel.connect(addr);
334 while(!sessionchannel.finishConnect()); 346 while(!sessionchannel.finishConnect());
335 Forwarder sm = new Forwarder(this); 347 Forwarder sm = new FirstConnector(this);
336 registerChannel(sessionchannel, sm); 348 registerChannel(sessionchannel, sm);
337 sm_join(sm); 349 sm_join(sm);
338 }catch (IOException e) { 350 }catch (IOException e) {
339 } 351 }
340 } 352 }
372 * manager. 384 * manager.
373 */ 385 */
374 public void selectSession(SelectButtonEvent event) throws IOException { 386 public void selectSession(SelectButtonEvent event) throws IOException {
375 int sid = event.getSID(); 387 int sid = event.getSID();
376 Session session = sessionList.get(sid); 388 Session session = sessionList.get(sid);
377 389 if (session==null) throw new IOException();
378 Editor editor = (Editor)event.getEditor(); 390 Editor editor = (Editor)event.getEditor();
379 if(editor == null){
380 logger.writeLog("Error SessionManager.selectSession(): editor = " + editor);
381 return;
382 }
383 if (editor.hasSession()) return; 391 if (editor.hasSession()) return;
384 392 // assert(getSMID(editor.eid)==smList.sessionManagerID());
393 // assert(editor.channel!=null);
394 editor.setSID(sid); // mark as selected
385 selectSession(sid, session, editor.getEID(), editor); 395 selectSession(sid, session, editor.getEID(), editor);
386 } 396 }
387 397
388 /* 398 /*
389 * Select Session Protocol handler 399 * Select Session Protocol handler
390 * called from GUI or incoming SMCMD_SELECT command. 400 * called from GUI or incoming SMCMD_SELECT command.
391 */ 401 */
392 private void selectSession(int sid, Session session, int eid, Forwarder editor) { 402 private void selectSession(int sid, Session session, int eid, Forwarder editor) {
393 if(session.hasOwner()){ 403 if(session.hasOwner() && editor.channel!=null){
394 // we have selected session. 404 // we have selected session.
395 REPCommand sendCommand = new REPCommand(); 405 REPCommand sendCommand = new REPCommand();
396 if (editor.isDirect()&&editor.getEID()==eid) { 406 if (editor.isDirect()&&editor.getEID()==eid) {
397 // Found directly connected joined editor. Send join_ack(). 407 // Found directly connected joined editor. Send join_ack().
398 session.addForwarder(editor); 408 session.addForwarder(editor);
399 sendUpdate(session.getSID()); 409 sendUpdate(session.getSID());
400 sendCommand.setCMD(REP.SMCMD_JOIN_ACK); 410 sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
411 return;
401 } else { 412 } else {
402 // We have a session, but joined editor is on the other sm. 413 // We have a session, but joined editor is on the other sm.
403 // SELECT_ACK is sent to the session ring to 414 // SELECT_ACK is sent to the session ring to
404 // find out the joined editor. 415 // find out the joined editor.
405 sendCommand.setCMD(REP.SMCMD_SELECT_ACK); 416 sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
414 sendCommand.setEID(editor.getEID()); 425 sendCommand.setEID(editor.getEID());
415 sendCommand.setSID(sid); 426 sendCommand.setSID(sid);
416 sendCommand.string = session.getName(); 427 sendCommand.string = session.getName();
417 editor.send(sendCommand); 428 editor.send(sendCommand);
418 }else { 429 }else {
419 // session searching continue... 430 Forwarder next;
420 Forwarder next = routingTable.toSession(sid); 431 if (editor.channel==null) {
421 432 next = routingTable.toSessionManager(getSMID(eid));
422 // create dummy editor for this session 433 } else {
423 Forwarder f = new Editor(this, false, makeID(editorList.newEid())); 434 // session searching continue...
424 f.setChannel(editor.channel); // incoming channel 435 next = routingTable.toSession(sid);
425 f.setNext(next); 436 // create dummy editor for this session
426 f.setHost(myHost); 437 Forwarder f = new Editor(this, false, makeID(editorList.newEid()));
427 f.setSID(sid); 438 f.setChannel(editor.channel); // incoming channel
428 session.setFirstForwarder(f); 439 f.setNext(next);
429 440 f.setHost(myHost);
441 f.setSID(sid);
442 session.setFirstForwarder(f);
443 }
430 // pass the select command to the next path. 444 // pass the select command to the next path.
431 REPCommand command = new REPCommand(); 445 REPCommand command = new REPCommand();
432 command.setCMD(REP.SMCMD_SELECT); 446 command.setCMD(REP.SMCMD_SELECT);
433 command.setSID(sid); 447 command.setSID(sid);
434 command.setEID(eid); 448 command.setEID(eid);
618 632
619 case SMCMD_SELECT: 633 case SMCMD_SELECT:
620 { 634 {
621 Session session = sessionList.get(command.sid); 635 Session session = sessionList.get(command.sid);
622 if (session==null) { 636 if (session==null) {
623 sessionList.put(command.sid, 637 session = new Session(command.sid, command.string,null);
624 new Session(command.sid, command.string,null)); 638 sessionList.put(command.sid,session);
625 } 639 }
626 selectSession(command.sid, session, command.eid, forwarder); 640 selectSession(command.sid, session, command.eid, forwarder);
627 } 641 }
628 break; 642 break;
629 case SMCMD_SELECT_ACK: 643 case SMCMD_SELECT_ACK:
792 806
793 public Session getSession(int sid) { 807 public Session getSession(int sid) {
794 return sessionList.get(sid); 808 return sessionList.get(sid);
795 } 809 }
796 810
811 public void execAfterConnect(SessionManagerEvent sessionManagerEvent) {
812 execAfterConnect = sessionManagerEvent;
813 }
814
815 public void afterConnect() {
816 if (execAfterConnect!=null) execAfterConnect.exec(this);
817 execAfterConnect = null;
818 }
819
820 public void setParent(Forwarder fw) {
821 smList.setParent(fw);
822 }
823
797 824
798 } 825 }