Mercurial > hg > RemoteEditor > REPSessionManager
view rep/SessionManager.java @ 7:5067a6a93d01
*** empty log message ***
author | pin |
---|---|
date | Fri, 19 Oct 2007 05:16:38 +0900 (2007-10-18) |
parents | bdb91f4cea26 |
children | b774b87cc2c1 |
line wrap: on
line source
package rep; import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.nio.ByteBuffer; import java.nio.channels.SelectableChannel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.util.Iterator; //+-------+--------+--------+-------+--------+---------+------+ //| cmd | session| editor | seqid | lineno | textsiz | text | //| | id | id | | | | | //+-------+--------+--------+-------+--------+---------+------+ //o-------header section (network order)-------------o /*int cmd; // command int sid; // session ID int eid; // editor ID int seqno; // Sequence number int lineno; // line number int textsize; // textsize byte[] text;*/ public class SessionManager implements ConnectionListener{ private SessionList sessionlist; //SocketChannel sessionchannel; private SessionManagerGUI sessionmanagerGUI; private Selector selector; private SessionManagerList smList; //private SocketChannel sessionchannel; //private boolean co; public SessionManager(int port) { sessionmanagerGUI = new SessionManagerGUI(); } public void openSelector() throws IOException{ selector = Selector.open(); } 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); ssc.socket().bind(new InetSocketAddress(port)); ssc.register(selector, SelectionKey.OP_ACCEPT); sessionlist = new SessionList(); smList = new SessionManagerList(); while(true){ selector.select(); for(SelectionKey key : selector.selectedKeys()){ if(key.isAcceptable()){ SocketChannel channel = ssc.accept(); registerChannel (selector, channel, SelectionKey.OP_READ); channel = null; }else if(key.isReadable()){ SocketChannel channel = (SocketChannel)key.channel(); REPPacketReceive repRec = new REPPacketReceive(channel); REPCommand repCom = repRec.unpack(); manager(channel, repCom); }else if(key.isConnectable()){ System.out.println("Connectable"); } } } } private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException { if(channel == null) { return; } System.out.println("registerChannel()"); channel.configureBlocking(false); selector.wakeup(); channel.register(selector, ops); } private void manager(SocketChannel channel, REPCommand repCmd) { if(repCmd == null) return; switch(repCmd.cmd){ case REP.SMCMD_JOIN: int eid = sessionlist.getNumberOfEditor(); repCmd.setEID(eid); repCmd.setCMD(repCmd.cmd + 1); repCmd.string = sessionlist.getSessionList(); REPPacketSend repSend = new REPPacketSend(channel); repSend.send(repCmd); break; case REP.SMCMD_JOIN_ACK: break; case REP.SMCMD_PUT: int sessionID = sessionlist.addSession(channel, repCmd.string); smList.update(sessionID, repCmd.string); repCmd.setSID(sessionID); repCmd.setCMD(repCmd.cmd + 1); REPPacketSend repSend2 = new REPPacketSend(channel); repSend2.send(repCmd); sessionlist.sendAddedSession(repCmd); break; case REP.SMCMD_PUT_ACK: break; case REP.SMCMD_SELECT: sessionlist.addEditor(channel, repCmd.sid, repCmd); repCmd.setCMD(repCmd.cmd + 1); REPPacketSend repSend3 = new REPPacketSend(channel); repSend3.send(repCmd); case REP.SMCMD_SESSION: repCmd.setCMD(REP.SMCMD_SESSION_ACK); sessionlist.addSessionManager(channel, repCmd); break; case REP.SMCMD_SESSION_JOIN: smList.add(channel); repCmd.setCMD(repCmd.cmd + 1); REPPacketSend repSend4 = new REPPacketSend(channel); repSend4.send(repCmd); break; case REP.SMCMD_SESSION_JOIN_ACK: break; case REP.REPCMD_READ: sessionlist.sendCmd(channel, repCmd); break; default: sessionlist.sendCmd(channel, repCmd); break; } } public static void main(String[] args) throws InterruptedException, IOException { int port = 8766; if(args.length == 1){ port = Integer.parseInt(args[1]); } SessionManager sm = new SessionManager(port); sm.openSelector(); sm.openWindow(); sm.sessionManagerNet(port); } private void openWindow() { Thread th = new Thread( sessionmanagerGUI ); th.start(); System.out.println(sessionmanagerGUI.toString()); sessionmanagerGUI.addConnectionListener(this); } private void connectSession(String host) { int port = 8766; //SocketChannel sessionchannel; //int port = Integer.parseInt(args[2]); InetSocketAddress addr = new InetSocketAddress(host, port); try { SocketChannel sessionchannel = SocketChannel.open(); sessionchannel.configureBlocking(true); sessionchannel.connect(addr); REPPacketSend send = new REPPacketSend(sessionchannel); while(!sessionchannel.finishConnect()){ System.out.print("."); } System.out.println(""); registerChannel(selector, sessionchannel, SelectionKey.OP_READ); send.send(REPCommand.SMCMD_SESSION_JOIN); smList.add(sessionchannel); }catch (IOException e) { e.printStackTrace(); } } public void connectionOccured(ConnectionEvent event) { connectSession(event.getHost()); } }