# HG changeset patch # User pin # Date 1171309410 -32400 # Node ID e41994ce73c7f060c7383d873a74d0afe10d5975 *** empty log message *** diff -r 000000000000 -r e41994ce73c7 .classpath --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.classpath Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,6 @@ + + + + + + diff -r 000000000000 -r e41994ce73c7 .project --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.project Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,17 @@ + + + SessionManager + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff -r 000000000000 -r e41994ce73c7 rep/REP.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/REP.java Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,31 @@ +package rep; + +public class REP { + public static final int REPCMD_OPEN = 1; + public static final int REPCMD_OPEN_ACK = 2; + public static final int REPCMD_READ = 3; + public static final int REPCMD_READ_ACK = 4; + public static final int REPCMD_INSERT = 6; + public static final int REPCMD_INSERT_ACK = 7; + public static final int REPCMD_DELETE = 9; + public static final int REPCMD_DELETE_ACK = 10; + public static final int REPCMD_CLOSE = 11; + public static final int REPCMD_CLOSE_ACK = 12; + public static final int REPCMD_REPLACE = 13; + public static final int REPCMD_REPLACE_ACK = 14; + public static final int SMCMD_JOIN = 41; + public static final int SMCMD_JOIN_ACK = 42; + public static final int SMCMD_GET = 43; + public static final int SMCMD_GET_ACK = 44; + public static final int SMCMD_PUT = 45; + public static final int SMCMD_PUT_ACK = 46; + public static final int SMCMD_SELECT = 47; + public static final int SMCMD_SELECT_ACK = 48; + public static final int SMCMD_REGISTER = 49; + public static final int SMCMD_REGISTER_ACK = 50; + public static final int SMCMD_DEREGISTER = 51; + public static final int SMCMD_DEREGISTER_ACK= 52; + public static final int SMCMD_QUIT = 53; + public static final int SMCMD_QUIT_ACK = 54; + +} diff -r 000000000000 -r e41994ce73c7 rep/REPCommand.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/REPCommand.java Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,44 @@ +package rep; + +public class REPCommand { + public int cmd; + public int sid; + public int eid; + public int seq; + public int len; + public int lineno; + public boolean stat; + + public String string; + private int textsiz; + + public REPCommand(int cmd,int sid,int eid, int seq, int lineno, int textsiz, String string) { + this.cmd = cmd; + this.sid = sid; + this.eid = eid; + this.seq = seq; + this.textsiz = textsiz; + this.lineno = lineno; + this.string = string; + } + + public String toString(){ + String repCmdString = new String(cmd + "," + sid + "," + eid + "," + seq + "," + lineno + "," + textsiz + "," + string); + return repCmdString; + } + + public void setEID(int eid2) { + // TODO Auto-generated method stub + this.eid = eid2; + } + + public void setCMD(int cmd2) { + // TODO Auto-generated method stub + this.cmd = cmd2; + } + + public void setSID(int sessionID) { + // TODO Auto-generated method stub + this.sid = sessionID; + } +} diff -r 000000000000 -r e41994ce73c7 rep/REPPacketReceive.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/REPPacketReceive.java Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,68 @@ +package rep; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; + +public class REPPacketReceive { + + SocketChannel socketchannel; + private int HEADER_SIZE = 24; + + public REPPacketReceive(SocketChannel sc){ + socketchannel = sc; + } + + + public REPCommand unpack() { + + ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); + long len = 0; + header.clear(); + try { + len = socketchannel.read(header); + if(len == -1){ + socketchannel.close(); + return null; + }else if(len == 0){ + return null; + } + } catch (IOException e1) { + e1.printStackTrace(); + } // limit = read length + if (len !=HEADER_SIZE) { + System.out.println("てす"); + // this can't happen + } + header.rewind(); // position = 0 + + String text = ""; + int cmd = header.getInt(); + int sid = header.getInt(); + int eid = header.getInt(); + int seqid = header.getInt(); + int lineno = header.getInt(); + int textsiz = header.getInt()/2; + + ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz*2); + + try { + len = socketchannel.read(textBuffer); + } catch (IOException e1) { + e1.printStackTrace(); + } // limit = read length + if (len != textsiz * 2) { + // this can't happen + System.out.println("あと"); + } + textBuffer.rewind(); + for(int i=0;i> sessions = new LinkedList>(); + Hashtable> sessions2 = new Hashtable>(); + //Hashtable editors = new Hashtable(); + private int sessionID; + + private int editorCount; + + public void add(SocketChannel channel) { + + } + + public int getEditorNumber() { + return 0; + + } + + public void add(SocketChannel channel, int sid) { + + } + + public int addSession(SocketChannel channel, String string) { + sessionID++; + sessions2.put(sessionID, new LinkedList()); + //sessions.add(new LinkedList()); + //return sessions2.size(); + return sessionID; + + } + + public void addEditor(SocketChannel channel, int sid) { + //editorCount++; + //sessions.get(sid-1).add(channel); + sessions2.get(sid).add(channel); + } + + public int getSessionID(SocketChannel channel) { + return 0; + } + + public int getNumberOfEditor() { + editorCount++; + return editorCount; + } + + public void sendCmd(SocketChannel channel2, REPCommand repCmd) { + //int sessionID = repCmd.sid; + LinkedList channelList = sessions2.get(repCmd.sid); + for(SocketChannel channel : channelList){ + if(channel.equals(channel2)) { + System.out.println("equals"); + continue; + } + REPPacketSend repSend = new REPPacketSend(channel); + repSend.send(repCmd); + } + } + +} diff -r 000000000000 -r e41994ce73c7 rep/SessionManager.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/SessionManager.java Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,129 @@ +package rep; + +// +-------+--------+--------+-------+--------+---------+------+ +// | 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;*/ + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.nio.ByteBuffer; +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; + +public class SessionManager { + + + private SessionList sessionlist; + + public SessionManager(int port) { + //manager(port); + } + + public void sessionManagerNet(int port) throws InterruptedException, IOException { + /** + * @param args + * @throws IOException + * @throws InterruptedException + * @throws IOException + * @throws InterruptedException + */ + Selector selector = Selector.open(); + ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.configureBlocking(false); + ssc.socket().bind(new InetSocketAddress(port)); + ssc.register(selector, SelectionKey.OP_ACCEPT); + sessionlist = new SessionList(); + + while(true){ + selector.select(); + for(SelectionKey key : selector.selectedKeys()){ + if(key.isAcceptable()){ + SocketChannel channel = ssc.accept(); + if(channel == null) continue; + channel.configureBlocking(false); + channel.register(selector, SelectionKey.OP_READ); + //sessionlist.add(channel); + channel = null; + } + else if(key.isReadable()){ + SocketChannel channel = (SocketChannel)key.channel(); + REPPacketReceive repRec = new REPPacketReceive(channel); + REPCommand repCom = repRec.unpack(); + manager(channel, repCom); + //Charset charset = Charset.forName("US-ASCII"); + //ByteBuffer buffer = ByteBuffer.allocate(8192); + //switch(channel.read(buffer)) { + //case -1: + // channel.close(); + // break; + //case 0: + // continue; + // default: + // buffer.flip(); + // System.out.println(charset.decode(buffer)); + // channel.write(charset.encode("test")); + // break; + //} + } + } + } + } + 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); + REPPacketSend repSend = new REPPacketSend(channel); + repSend.send(repCmd); + break; + case REP.SMCMD_PUT: + int sessionID = sessionlist.addSession(channel, repCmd.string); + repCmd.setSID(sessionID); + repCmd.setCMD(repCmd.cmd + 1); + //repCmd.setSID(sessionlist.getSessionID(channel)); + REPPacketSend repSend2 = new REPPacketSend(channel); + repSend2.send(repCmd); + break; + case REP.SMCMD_SELECT: + sessionlist.addEditor(channel, repCmd.sid); + repCmd.setCMD(repCmd.cmd + 1); + REPPacketSend repSend3 = new REPPacketSend(channel); + repSend3.send(repCmd); + //case REP.REPCMD_INSERT: + // break; + default: + sessionlist.sendCmd(channel, repCmd); + break; + + } + } + + public static void main(String[] args) throws InterruptedException, IOException { + int port; + if(args.length == 1){ + port = Integer.parseInt(args[1]); + }else{ + port = 8765; + } + SessionManager sm = new SessionManager(port); + sm.sessionManagerNet(port); + } + +} diff -r 000000000000 -r e41994ce73c7 rep/SessionManagerGUI.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/SessionManagerGUI.java Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,5 @@ +package rep; + +public class SessionManagerGUI { + +}