Mercurial > hg > RemoteEditor > REPSessionManager
changeset 1:3f5bf0255f5e
*** empty log message ***
author | pin |
---|---|
date | Wed, 21 Feb 2007 15:08:52 +0900 |
parents | e41994ce73c7 |
children | 02fa9a68d9a2 |
files | rep/Editor.java rep/REP.java rep/REPCommand.java rep/RPanel.java rep/Session.java rep/SessionList.java rep/SessionManager.java rep/SessionManagerGUI.java rep/SessionManagerSample.java |
diffstat | 9 files changed, 197 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/Editor.java Wed Feb 21 15:08:52 2007 +0900 @@ -0,0 +1,17 @@ +package rep; + +import java.nio.channels.SocketChannel; + +public class Editor { + private int editorNo; + private SocketChannel channel; + + public Editor(int editorNo, SocketChannel channel){ + this.editorNo = editorNo; + this.channel = channel; + } + + public SocketChannel getChannel() { + return channel; + } +}
--- a/rep/REP.java Tue Feb 13 04:43:30 2007 +0900 +++ b/rep/REP.java Wed Feb 21 15:08:52 2007 +0900 @@ -27,5 +27,9 @@ public static final int SMCMD_DEREGISTER_ACK= 52; public static final int SMCMD_QUIT = 53; public static final int SMCMD_QUIT_ACK = 54; + public static final int SMCMD_SESSION = 60; + public static final int SMCMD_SESSION_ACK = 61; + public static final int SMCMD_SESSION_JOIN = 62; + public static final int SMCMD_SESSION_JOIN_ACK = 62; }
--- a/rep/REPCommand.java Tue Feb 13 04:43:30 2007 +0900 +++ b/rep/REPCommand.java Wed Feb 21 15:08:52 2007 +0900 @@ -41,4 +41,5 @@ // TODO Auto-generated method stub this.sid = sessionID; } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/RPanel.java Wed Feb 21 15:08:52 2007 +0900 @@ -0,0 +1,45 @@ +package rep; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.AbstractButton; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextArea; +import javax.swing.JTextField; + +public class RPanel extends JPanel implements ActionListener { + + private JButton button; + private JTextArea textField; + private String text; + private JLabel label; + + public RPanel() { + button = new JButton("Connect"); + textField = new JTextArea("133.13.54.54"); + //textField.setSize(100, 50); + label = new JLabel("test"); + + this.setLayout(new FlowLayout()); + this.add(textField, FlowLayout.LEFT); + this.add( button, FlowLayout.CENTER); + this.add(label, FlowLayout.RIGHT); + //this.add(label, BorderLayout.CENTER); + + button.addActionListener(this); + } + + public void actionPerformed(ActionEvent event) { + if (event.getSource() == button) { + text = textField.getText(); + + } + } + +}
--- a/rep/Session.java Tue Feb 13 04:43:30 2007 +0900 +++ b/rep/Session.java Wed Feb 21 15:08:52 2007 +0900 @@ -1,5 +1,24 @@ package rep; +import java.nio.channels.SocketChannel; +import java.util.LinkedList; +import java.util.List; + public class Session { - + Editor masterEditor; + private int sessionID; + private String sessionName; + private LinkedList<Editor> editorList = new LinkedList<Editor>(); + public Session(int sessionID, String string, SocketChannel channel) { + masterEditor = new Editor(sessionID, channel); + this.sessionID = sessionID; + this.sessionName = string; + } + public void addEditor(int editorID, SocketChannel channel) { + editorList.add(new Editor(editorID, channel)); + } + public LinkedList getEditorList() { + // TODO Auto-generated method stub + return editorList; + } }
--- a/rep/SessionList.java Tue Feb 13 04:43:30 2007 +0900 +++ b/rep/SessionList.java Wed Feb 21 15:08:52 2007 +0900 @@ -8,11 +8,12 @@ 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; + //Hashtable<Integer, LinkedList<SocketChannel>> sessions2 = new Hashtable<Integer, LinkedList<SocketChannel>>(); + Hashtable<Integer, Session> session3 = new Hashtable<Integer, Session>(); + private int sessionID = 0; + private int editorCount = 0; - private int editorCount; + private SocketChannel smchannel; public void add(SocketChannel channel) { @@ -29,17 +30,20 @@ public int addSession(SocketChannel channel, String string) { sessionID++; - sessions2.put(sessionID, new LinkedList<SocketChannel>()); + //sessions2.put(sessionID, new LinkedList<SocketChannel>()); //sessions.add(new LinkedList<SocketChannel>()); //return sessions2.size(); + session3.put(sessionID, new Session(sessionID, string, channel)); return sessionID; } - public void addEditor(SocketChannel channel, int sid) { + public void addEditor(SocketChannel channel, int sid, REPCommand repCmd) { + int editorID = repCmd.eid; //editorCount++; //sessions.get(sid-1).add(channel); - sessions2.get(sid).add(channel); + //sessions2.get(sid).add(channel); + session3.get(sid).addEditor(editorID, channel); } public int getSessionID(SocketChannel channel) { @@ -53,8 +57,10 @@ public void sendCmd(SocketChannel channel2, REPCommand repCmd) { //int sessionID = repCmd.sid; - LinkedList <SocketChannel> channelList = sessions2.get(repCmd.sid); - for(SocketChannel channel : channelList){ + //LinkedList <SocketChannel> channelList = sessions2.get(repCmd.sid); + LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList(); + for(Editor editor : editorList){ + SocketChannel channel = editor.getChannel(); if(channel.equals(channel2)) { System.out.println("equals"); continue; @@ -64,4 +70,16 @@ } } + public void addSessionManager(SocketChannel channel, REPCommand repCmd) { + smchannel = channel; + + } + + public void sendAddedSession(REPCommand repCmd) { + // TODO Auto-generated method stub + repCmd.setCMD(REP.SMCMD_SESSION_JOIN); + REPPacketSend repSend = new REPPacketSend(smchannel); + //repSend.send(repCmd); + } + }
--- a/rep/SessionManager.java Tue Feb 13 04:43:30 2007 +0900 +++ b/rep/SessionManager.java Wed Feb 21 15:08:52 2007 +0900 @@ -1,18 +1,5 @@ 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; @@ -24,14 +11,25 @@ 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 { private SessionList sessionlist; - - public SessionManager(int port) { - //manager(port); - } + SocketChannel sessionchannel; + public SessionManager(int port) {} public void sessionManagerNet(int port) throws InterruptedException, IOException { /** @@ -46,6 +44,11 @@ ssc.configureBlocking(false); ssc.socket().bind(new InetSocketAddress(port)); ssc.register(selector, SelectionKey.OP_ACCEPT); + if (sessionchannel != null) { + REPPacketSend rp = new REPPacketSend(sessionchannel); + rp.send(new REPCommand(REP.SMCMD_SESSION, 0, 0, 0, 0, 0, "")); + sessionchannel.register(selector, SelectionKey.OP_READ); + } sessionlist = new SessionList(); while(true){ @@ -56,7 +59,6 @@ if(channel == null) continue; channel.configureBlocking(false); channel.register(selector, SelectionKey.OP_READ); - //sessionlist.add(channel); channel = null; } else if(key.isReadable()){ @@ -64,50 +66,45 @@ 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_JOIN_ACK: + 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); + sessionlist.sendAddedSession(repCmd); + break; + case REP.SMCMD_PUT_ACK: break; case REP.SMCMD_SELECT: - sessionlist.addEditor(channel, repCmd.sid); + sessionlist.addEditor(channel, repCmd.sid, repCmd); repCmd.setCMD(repCmd.cmd + 1); REPPacketSend repSend3 = new REPPacketSend(channel); repSend3.send(repCmd); - //case REP.REPCMD_INSERT: - // break; + case REP.SMCMD_SESSION: + repCmd.setCMD(REP.SMCMD_SESSION_ACK); + sessionlist.addSessionManager(channel, repCmd); + break; + case REP.SMCMD_SESSION_JOIN: + + break; default: sessionlist.sendCmd(channel, repCmd); break; @@ -116,14 +113,35 @@ } public static void main(String[] args) throws InterruptedException, IOException { - int port; + int port = 8765; + if(args.length == 1){ port = Integer.parseInt(args[1]); - }else{ - port = 8765; } +// SessionManagerGUI gui = new SessionManagerGUI(); +// Thread th = new Thread( gui ); +// th.start(); SessionManager sm = new SessionManager(port); + if(args.length == 3){ + sm.connectSession(args); + } sm.sessionManagerNet(port); } + private void connectSession(String[] args) { + // TODO Auto-generated method stub + int port = Integer.parseInt(args[2]); + String host = args[1]; + InetSocketAddress addr = new InetSocketAddress(host, port); + try { + sessionchannel = SocketChannel.open(); + sessionchannel.configureBlocking(true); + sessionchannel.connect(addr); + while(!sessionchannel.finishConnect()){ + System.out.println("afro"); + } + }catch (IOException e) { + e.printStackTrace(); + } + } }
--- a/rep/SessionManagerGUI.java Tue Feb 13 04:43:30 2007 +0900 +++ b/rep/SessionManagerGUI.java Wed Feb 21 15:08:52 2007 +0900 @@ -1,5 +1,23 @@ package rep; -public class SessionManagerGUI { +import java.awt.BorderLayout; +import java.awt.Container; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; + +public class SessionManagerGUI implements Runnable{ + public static void main(String[] args) {} -} + public void run() { + JFrame frame = new JFrame("SessionManager"); + RPanel rp = new RPanel(); + Container cont = frame.getContentPane(); + cont.add(rp); + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.pack(); + frame.setVisible(true); + } +} \ No newline at end of file