Mercurial > hg > RemoteEditor > REPSessionManager
diff rep/SessionManager.java @ 75:a636b651dbce
*** empty log message ***
author | pin |
---|---|
date | Sun, 25 Nov 2007 03:21:17 +0900 |
parents | 391d44c94799 |
children | 97ca5f5447cd |
line wrap: on
line diff
--- a/rep/SessionManager.java Sun Nov 25 02:26:23 2007 +0900 +++ b/rep/SessionManager.java Sun Nov 25 03:21:17 2007 +0900 @@ -48,14 +48,6 @@ } public void sessionManagerNet(int port) throws InterruptedException, IOException { - /** - * @param args - * @throws IOException - * @throws InterruptedException - * @throws IOException - * @throws InterruptedException - */ - System.out.println("sessionManagerNet()"); ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); @@ -95,11 +87,11 @@ /*** EditorChannel を用いない記述 ***/ SocketChannel channel = (SocketChannel)key.channel(); - REPPacketReceive repRec = new REPPacketReceive(channel); //getPacket(), putPacket() にする。 - repRec.setkey(key); + REPPacketReceive receive = new REPPacketReceive(channel); //getPacket(), putPacket() にする。 + receive.setkey(key); //REPCommand repCom = repRec.unpackUConv(); - REPCommand repCom = repRec.unpack(); - manager(channel, repCom); + REPCommand receivedCommand = receive.unpack(); + manager(channel, receivedCommand); /*** EditorChannel を用いた記述 ****/ //EditorChannel echannel = (EditorChannel) key.channel(); @@ -123,21 +115,23 @@ channel.register(selector, ops); } - private void manager(SocketChannel channel, REPCommand repCmd) { - if(repCmd == null) return; + private void manager(SocketChannel channel, REPCommand receivedCommand) { + if(receivedCommand == null) return; Editor editor; Session session; - switch(repCmd.cmd){ + REPCommand sendCommand = receivedCommand.clone(); + + switch(receivedCommand.cmd){ case REP.SMCMD_JOIN: if(isMaster){ - int eid = editorList.addEditor(channel, repCmd); - repCmd.setEID(eid); - editorList.sendJoinAck(channel, repCmd); + int eid = editorList.addEditor(channel, receivedCommand); + receivedCommand.setEID(eid); + editorList.sendJoinAck(channel, receivedCommand); sessionmanagerGUI.setComboEditor(eid, channel); }else{ editorList.addEditor(channel); - smList.sendJoin(repCmd); + smList.sendJoin(receivedCommand); //sessionmanagerGUI.setComboEditor(repCmd.eid, channel); } break; @@ -151,7 +145,7 @@ case REP.SMCMD_PUT: editor = new Editor(channel); editor.setEID(1); - editor.setName(repCmd.string); + editor.setName(receivedCommand.string); session = new Session(editor); session.setOwner(true); session.addEditor(editor); @@ -159,10 +153,10 @@ sessionmanagerGUI.setComboSession(session.getSID(), session.getName()); sessionmanagerGUI.setComboEditor(editor.getEID(), editor.getChannel()); session.addToRoutingTable(editor); - repCmd.setCMD(REP.SMCMD_PUT_ACK); - repCmd.setEID(1); - repCmd.setSID(session.getSID()); - editor.send(repCmd); + receivedCommand.setCMD(REP.SMCMD_PUT_ACK); + receivedCommand.setEID(1); + receivedCommand.setSID(session.getSID()); + editor.send(receivedCommand); //if(isMaster){ SessionXMLEncoder encoder = new SessionXMLEncoder(session); @@ -184,14 +178,14 @@ case REP.SMCMD_SELECT: // sessionlist.addEditor(channel, repCmd.sid, repCmd); //sessionlistへ追加 editor = new Editor(channel); - session = sessionlist.getSession(repCmd.sid); + session = sessionlist.getSession(receivedCommand.sid); if(session.isOwner()){ int eid = session.addEditor(editor); editor.setEID(eid); REPPacketSend send = new REPPacketSend(channel); - repCmd.setCMD(REP.SMCMD_SELECT_ACK); - repCmd.setEID(eid); - send.send(repCmd); + receivedCommand.setCMD(REP.SMCMD_SELECT_ACK); + receivedCommand.setEID(eid); + send.send(receivedCommand); }else { } @@ -199,26 +193,24 @@ break; case REP.SMCMD_SELECT_ACK: - repCmd.setCMD(REP.SMCMD_JOIN_ACK); - repCmd.setEID(repCmd.eid); - session = sessionlist.getSession(repCmd.sid); - session.sendToEditor(repCmd); + receivedCommand.setCMD(REP.SMCMD_JOIN_ACK); + receivedCommand.setEID(receivedCommand.eid); + session = sessionlist.getSession(receivedCommand.sid); + session.sendToEditor(receivedCommand); //Editor editor3 = session3.getEditorList().get(0); //REPPacketSend send = new REPPacketSend(editor3.getChannel()); //send.send(repCmd); break; case REP.SMCMD_SM_JOIN: - myHost = getHostName(channel); - if(myHost.compareTo(repCmd.string) > 0){ - - }else if(myHost.compareTo(repCmd.string) < 0){ - isMaster = false; - } + myHost = getLocalHostName(channel); + if(myHost.compareTo(receivedCommand.string) < 0) + setMaster(false); + if(isMaster){ smList.add(channel); - repCmd.setCMD(REP.SMCMD_SM_JOIN_ACK); - smList.sendSessionList(sessionlist, repCmd); + receivedCommand.setCMD(REP.SMCMD_SM_JOIN_ACK); + smList.sendSessionList(sessionlist, receivedCommand); }else { } @@ -226,18 +218,18 @@ case REP.SMCMD_SM_JOIN_ACK: if(isMaster){ - smList.send(channel, repCmd); + smList.send(channel, receivedCommand); }else{ - smList.send(channel, repCmd); + smList.send(channel, receivedCommand); } break; case REP.SMCMD_UPDATE: - SessionXMLDecoder decoder = new SessionXMLDecoder(repCmd.string); + SessionXMLDecoder decoder = new SessionXMLDecoder(receivedCommand.string); editor = new Editor(channel); - editor.setName(repCmd.string); + editor.setName(receivedCommand.string); session = new Session(editor); session.addEditor(editor); @@ -247,18 +239,18 @@ sessionmanagerGUI.setComboSession(session.getSID(), session.getName()); if(isMaster){ - repCmd.setCMD(REP.SMCMD_UPDATE_ACK); - smList.sendToSlave(repCmd); + receivedCommand.setCMD(REP.SMCMD_UPDATE_ACK); + smList.sendToSlave(receivedCommand); }else{ - repCmd.setCMD(REP.SMCMD_UPDATE); - smList.sendToMaster(repCmd); + receivedCommand.setCMD(REP.SMCMD_UPDATE); + smList.sendToMaster(receivedCommand); } break; case REP.SMCMD_UPDATE_ACK: - if(repCmd.sid > sessionlist.getList().size()){ + if(receivedCommand.sid > sessionlist.getList().size()){ editor = new Editor(channel); - editor.setName(repCmd.string); + editor.setName(receivedCommand.string); session = new Session(editor); session.addEditor(editor); @@ -267,7 +259,7 @@ sessionmanagerGUI.setComboSession(session.getSID(), session.getName()); } - smList.sendToSlave(repCmd); + smList.sendToSlave(receivedCommand); break; case REP.REPCMD_READ: @@ -276,7 +268,7 @@ default: //sessionlist.sendCmd(channel, repCmd); - sessionlist.sendToNextEditor(channel, repCmd); + sessionlist.sendToNextEditor(channel, receivedCommand); break; } } @@ -285,6 +277,12 @@ // smList.sendJoin(repCmd); // } + private void setMaster(boolean b) { + // TODO Auto-generated method stub + isMaster = b; + System.out.println("isMaster = " + b); + } + private void sendSessionManagerJoinAck(SocketChannel channel, REPCommand repCmd) { String socketstring = getSocketString(channel); System.out.println(socketstring); @@ -332,7 +330,7 @@ private void openWindow() { Thread th = new Thread( sessionmanagerGUI ); th.start(); - System.out.println(sessionmanagerGUI.toString()); + //System.out.println(sessionmanagerGUI.toString()); sessionmanagerGUI.addConnectionListener(this); sessionmanagerGUI.addREPActionListener(this); } @@ -359,7 +357,7 @@ REPCommand command = new REPCommand(); command.setCMD(REP.SMCMD_SM_JOIN); - myHost = getHostName(sessionchannel); + myHost = getLocalHostName(sessionchannel); command.setString(myHost); //command.setString(encoder.sessionListToXML()); send.send(command); @@ -373,7 +371,7 @@ } } - private String getHostName(SocketChannel channel) { + private String getLocalHostName(SocketChannel channel) { String host = null; host = channel.socket().getLocalAddress().getHostName(); return host;