Mercurial > hg > RemoteEditor > REPSessionManager
changeset 0:e41994ce73c7
*** empty log message ***
author | pin |
---|---|
date | Tue, 13 Feb 2007 04:43:30 +0900 |
parents | |
children | 3f5bf0255f5e |
files | .classpath .project rep/REP.java rep/REPCommand.java rep/REPPacketReceive.java rep/REPPacketSend.java rep/ServerSample.java rep/Session.java rep/SessionList.java rep/SessionManager.java rep/SessionManagerGUI.java |
diffstat | 11 files changed, 501 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.classpath Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path=""/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="output" path=""/> +</classpath>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.project Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>SessionManager</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription>
--- /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; + +}
--- /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; + } +}
--- /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<textsiz;i++) { + text +=textBuffer.getChar(); + } + String string = text; + //System.out.println(string); + REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); + System.out.println("received command: " + repcommand.toString()); + return repcommand; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/REPPacketSend.java Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,36 @@ +package rep; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; + +public class REPPacketSend { + SocketChannel socketchannel; + + public REPPacketSend(SocketChannel sc){ + socketchannel = sc; + } + + public ByteBuffer pack(REPCommand command){ + System.out.println("send command: " + command.toString()); + ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2); + buffer.clear(); // position = 0 + buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid); + buffer.putInt(command.seq); buffer.putInt(command.lineno); + buffer.putInt(command.string.length()*2); + for(int i=0;i<command.string.length();i++) { + buffer.putChar(command.string.charAt(i)); + } + buffer.flip(); // limit = current position, position = 0 + return buffer; + } + + public void send(REPCommand command){ + try { + socketchannel.write(pack(command)); + //System.out.println(command.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/ServerSample.java Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,93 @@ +package rep; +import java.nio.*; +import java.nio.channels.*; +import java.nio.charset.*; +import java.net.*; + +public class ServerSample +{ +public static void main(String[] argv) +throws Exception +{ +// セレクタの用意 +Selector selector = Selector.open(); + +// サーバソケットチャンネルを作成。5100番ポートを受付ポートに指定 +// (非ブロックモードに設定:重要) +ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); +serverSocketChannel.configureBlocking(false); +serverSocketChannel.socket().bind(new InetSocketAddress(5100)); + +// セレクタにサーバソケットチャンネルを登録。サーバへの受付を監視 +serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); + +// セレクタにイベントが通知されるごとに処理 +while (true) { + +// セレクタにイベントが発生するまでブロック +selector.select(); + +// 獲得したイベントごとに処理を実行 +for (SelectionKey selectionKey : selector.selectedKeys()) { + +// サーバの受付処理: +// イベントが受付可能である場合、受け付けるべき対象があれば +// セレクタに取得したソケットチャンネルを登録 +if (selectionKey.isAcceptable()) { + +// サーバソケットチャンネルからソケットチャンネルを獲得 +// ソケットチャンネルを経由してクライアントと通信できる +SocketChannel socketChannel = serverSocketChannel.accept(); + +// 接続先がなくてもここに処理が飛ぶことがある。対象が +// nullの場合は処理を抜ける +if (null == socketChannel) continue; + +// ソケットチャンネルを非ブロックモードに設定(重要)し、 +// セレクタに読み込みを対象として登録 +socketChannel.configureBlocking(false); +socketChannel.register(selector, SelectionKey.OP_READ); +socketChannel = null; +} + +// クライアントとの通信処理 +// 読込み可能である場合、内容物を読みこんで標準出力に表示。 +// メッセージをクライアントに送信して、コネクションを切断。 +// セレクタから登録を解除 +else if (selectionKey.isReadable()) { + +// 登録されているソケットチャンネルを取得 +SocketChannel socketChannel = +(SocketChannel)selectionKey.channel(); + +Charset charset = Charset.forName("US-ASCII"); +ByteBuffer byteBuffer = ByteBuffer.allocate(8192); + +// クライアントからメッセージの受信 +switch (socketChannel.read(byteBuffer)) { +case -1: +// クライアント側が接続を切断していた場合は、サーバも +// 接続を切断。セレクタから登録を削除 +socketChannel.close(); +break; +case 0: +// 読み込むべきメッセージは届いていないので処理を飛ばす +continue; +default: +// クライアントからメッセージを取得し、標準出力へ +byteBuffer.flip(); +System.out.print("EEE: " + charset.decode(byteBuffer)); + +// クライアントへメッセージを送信 +socketChannel.write(charset.encode("Good bye!\r\n")); + +// クライアントとの接続を切断。セレクタから登録を削除 +//socketChannel.close(); +break; +} +} +System.out.println(selectionKey.toString()); +} +} +} +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/Session.java Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,5 @@ +package rep; + +public class Session { + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/SessionList.java Tue Feb 13 04:43:30 2007 +0900 @@ -0,0 +1,67 @@ +package rep; + +import java.nio.channels.SocketChannel; +import java.util.Hashtable; +import java.util.LinkedList; +import java.util.List; + +public class SessionList { + + //List<LinkedList<SocketChannel>> sessions = new LinkedList<LinkedList<SocketChannel>>(); + Hashtable<Integer, LinkedList<SocketChannel>> sessions2 = new Hashtable<Integer, LinkedList<SocketChannel>>(); + //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<SocketChannel>()); + //sessions.add(new LinkedList<SocketChannel>()); + //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 <SocketChannel> 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); + } + } + +}
--- /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); + } + +}