comparison rep/SessionManager.java @ 412:2c815dd5f797

add comments.
author one
date Wed, 26 Nov 2008 09:51:23 +0900
parents 19705f4b8015
children c22f6833d736
comparison
equal deleted inserted replaced
411:795ef563f2a0 412:2c815dd5f797
2 package rep; 2 package rep;
3 3
4 import java.io.IOException; 4 import java.io.IOException;
5 import java.net.InetSocketAddress; 5 import java.net.InetSocketAddress;
6 import java.nio.channels.ClosedChannelException; 6 import java.nio.channels.ClosedChannelException;
7 import java.util.LinkedList;
7 8
8 import org.xml.sax.SAXException; 9 import org.xml.sax.SAXException;
9 10
10 11
11 12
12 import rep.channel.REPLogger; 13 import rep.channel.REPLogger;
13 import rep.channel.REPSocketChannel; 14 import rep.channel.REPSocketChannel;
14 import rep.gui.CloseButtonEvent; 15 import rep.gui.CloseButtonEvent;
16 import rep.gui.DoGUIUpdate;
15 import rep.gui.SelectButtonEvent; 17 import rep.gui.SelectButtonEvent;
16 import rep.gui.SessionManagerEvent; 18 import rep.gui.SessionManagerEvent;
17 import rep.gui.SessionManagerEventListener; 19 import rep.gui.SessionManagerEventListener;
18 import rep.gui.SessionManagerGUI; 20 import rep.gui.SessionManagerGUI;
19 import rep.gui.SessionManagerGUIimpl; 21 import rep.gui.SessionManagerGUIimpl;
61 private REPNode sm_join_channel; 63 private REPNode sm_join_channel;
62 // Routing table for session and session manager. 64 // Routing table for session and session manager.
63 private RoutingTable routingTable = new RoutingTable(this); 65 private RoutingTable routingTable = new RoutingTable(this);
64 // sync option 66 // sync option
65 public boolean sync = true; 67 public boolean sync = true;
66 68 // list olny local editor in GUI
69 protected boolean listLocalEditorOnly = false;
70
67 static public REPLogger logger = REPLogger.singleton(); 71 static public REPLogger logger = REPLogger.singleton();
68 72
69 public static void main(String[] args) throws InterruptedException, IOException { 73 public static void main(String[] args) throws InterruptedException, IOException {
70 int port =ServerMainLoop.DEFAULT_PORT; 74 int port =ServerMainLoop.DEFAULT_PORT;
71 int port_s = ServerMainLoop.DEFAULT_PORT; 75 int port_s = ServerMainLoop.DEFAULT_PORT;
102 /** 106 /**
103 * GUI から、呼ばれて、Session Managerに接続する。 107 * GUI から、呼ばれて、Session Managerに接続する。
104 * Host 名のSession Manager に SM_JOIN する。自分は、Session を持っていては 108 * Host 名のSession Manager に SM_JOIN する。自分は、Session を持っていては
105 * ならない。複数のSession Managerにjoinすることは出来ない。(NATを実装するまでは)。 109 * ならない。複数のSession Managerにjoinすることは出来ない。(NATを実装するまでは)。
106 * @param host 110 * @param host
107 */ 111 * @return error massage, return null when no errors.
108 public void connectSessionManager(String host, int port) { 112 */
109 if (sm_join_channel!=null) return; 113 public String connectSessionManager(String host, int port) {
110 if (!sessionList.isEmpty()) return; 114 if (sm_join_channel!=null) return "No Channel of sessionManager, Network Error.";
111 if (!smList.isMaster()) return; 115 if (!sessionList.isEmpty()) return "Cannot connect with sessions.";
116 if (!smList.isMaster()) return "Already connected to the master.";
112 /* 117 /*
113 * IPv6 対応では、複数のアドレスを取って、それのすべてに接続を試す必要が 118 * IPv6 対応では、複数のアドレスを取って、それのすべてに接続を試す必要が
114 * ある。 119 * ある。
115 */ 120 */
116 InetSocketAddress addr = new InetSocketAddress(host, port); 121 InetSocketAddress addr = new InetSocketAddress(host, port);
121 REPNode sm = new FirstConnector(this,sessionchannel); 126 REPNode sm = new FirstConnector(this,sessionchannel);
122 registerChannel(sessionchannel, sm); 127 registerChannel(sessionchannel, sm);
123 sm_join(sm); 128 sm_join(sm);
124 }catch (IOException e) { 129 }catch (IOException e) {
125 } 130 }
131 return null;
126 } 132 }
127 133
128 public void connectSessionManager(String host) { 134 public void connectSessionManager(String host) {
129 connectSessionManager(host,parent_port); 135 connectSessionManager(host,parent_port);
130 } 136 }
262 */ 268 */
263 public void closeSession(SessionManagerEvent event) { 269 public void closeSession(SessionManagerEvent event) {
264 Session session = ((CloseButtonEvent) event).getSession(); 270 Session session = ((CloseButtonEvent) event).getSession();
265 session.closeSession(); 271 session.closeSession();
266 sessionList.remove(session); 272 sessionList.remove(session);
267 updateGUI(); 273 manager.updateGUI(this);
268 } 274 }
269 275
270 /* 276 /*
271 * Remove editors which has the cannel. 277 * Remove editors which has the cannel.
272 */ 278 */
291 removeSession(s); 297 removeSession(s);
292 } else { 298 } else {
293 s.deleteForwarder(editor); 299 s.deleteForwarder(editor);
294 editorList.remove(editor); 300 editorList.remove(editor);
295 } 301 }
296 updateGUI(); 302 manager.updateGUI(this);
297 } 303 }
298 304
299 private void removeSession(Session s0) { 305 private void removeSession(Session s0) {
300 s0.remove(this); 306 s0.remove(this);
301 sessionList.remove(s0); 307 sessionList.remove(s0);
321 smList.sendToSlaves(ackCommand); 327 smList.sendToSlaves(ackCommand);
322 registEditor(forwarder,ackCommand); 328 registEditor(forwarder,ackCommand);
323 } else { 329 } else {
324 smList.sendToMaster(command); 330 smList.sendToMaster(command);
325 } 331 }
326 updateGUI(); 332 manager.updateGUI(this);
327 } 333 }
328 334
329 break; 335 break;
330 336
331 case SMCMD_PUT_ACK: 337 case SMCMD_PUT_ACK:
337 command.setSID(command.sid); 343 command.setSID(command.sid);
338 forwarder.send(command); 344 forwarder.send(command);
339 } 345 }
340 case SMCMD_JOIN_ACK: 346 case SMCMD_JOIN_ACK:
341 registEditor(forwarder,command); 347 registEditor(forwarder,command);
342 updateGUI(); 348 manager.updateGUI(this);
343 break; 349 break;
344 350
345 case SMCMD_PUT: 351 case SMCMD_PUT:
346 { 352 {
347 // first connection or forwarded command 353 // first connection or forwarded command
359 } else { 365 } else {
360 // Pass this to the master. 366 // Pass this to the master.
361 smList.sendToMaster(command); 367 smList.sendToMaster(command);
362 // registEditor will be done by SMCMD_PUT_ACK 368 // registEditor will be done by SMCMD_PUT_ACK
363 } 369 }
364 updateGUI(); 370 manager.updateGUI(this);
365 371
366 } 372 }
367 break; 373 break;
368 374
369 case SMCMD_SELECT0: 375 case SMCMD_SELECT0:
440 break; 446 break;
441 case SMCMD_UPDATE_ACK: 447 case SMCMD_UPDATE_ACK:
442 command.setString(mergeUpdate(command)); 448 command.setString(mergeUpdate(command));
443 // 下に知らせる 449 // 下に知らせる
444 smList.sendToSlaves(command); 450 smList.sendToSlaves(command);
445 updateGUI(); 451 manager.updateGUI(this);
446 break; 452 break;
447 default: 453 default:
448 return false; 454 return false;
449 } 455 }
450 return true; 456 return true;
612 e.checkWaitingCommandInMerge(); 618 e.checkWaitingCommandInMerge();
613 } 619 }
614 620
615 } 621 }
616 622
623 /**
624 * Notify status change to our GUI
625 */
626 protected void updateGUI(ServerMainLoop serverMainLoop) {
627 //リストのコピーをGUIに渡す
628 LinkedList<Session> sList = new LinkedList<Session>(sessionList.values());
629 LinkedList<REPNode> eList;
630 if (listLocalEditorOnly ) {
631 // Do not list an editor or session outside of this session manager to
632 // avoid confusion. Actually we can joinSession to an editor outside of
633 // this manager, but is not secure to do this.
634 eList = new LinkedList<REPNode>();
635 for(REPNode e:editorList.values()) {
636 if (getSMID(e.eid)==smList.sessionManagerID()) {
637 eList.add(e);
638 }
639 }
640 } else {
641 eList = new LinkedList<REPNode>(editorList.values());
642 }
643 //GUIに反映
644 Runnable doRun = new DoGUIUpdate(sList, eList, serverMainLoop.gui);
645 serverMainLoop.gui.invokeLater(doRun);
646 }
647
617 } 648 }