Mercurial > hg > RemoteEditor > REPSessionManager
changeset 2:02fa9a68d9a2
*** empty log message ***
author | pin |
---|---|
date | Tue, 20 Mar 2007 16:19:33 +0900 |
parents | 3f5bf0255f5e |
children | f31fcac5a949 |
files | rep/ConnectionEvent.java rep/ConnectionListener.java rep/Editor.java rep/GUITest.java rep/RPanel.java rep/SMConnector.java rep/Session.java rep/SessionList.java rep/SessionManager.java rep/SessionManagerGUI.java rep/SessionViewer.java |
diffstat | 11 files changed, 263 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/ConnectionEvent.java Tue Mar 20 16:19:33 2007 +0900 @@ -0,0 +1,17 @@ +package rep; + +public class ConnectionEvent { + + private String host; + + public ConnectionEvent(String host) { + // TODO Auto-generated constructor stub + this.host = host; + } + + public String getHost() { + // TODO Auto-generated method stub + return host; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/ConnectionListener.java Tue Mar 20 16:19:33 2007 +0900 @@ -0,0 +1,5 @@ +package rep; + +public interface ConnectionListener { + public void connectionOccured(ConnectionEvent event); +}
--- a/rep/Editor.java Wed Feb 21 15:08:52 2007 +0900 +++ b/rep/Editor.java Tue Mar 20 16:19:33 2007 +0900 @@ -5,6 +5,7 @@ public class Editor { private int editorNo; private SocketChannel channel; + private SocketChannel nextChannel; public Editor(int editorNo, SocketChannel channel){ this.editorNo = editorNo;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/GUITest.java Tue Mar 20 16:19:33 2007 +0900 @@ -0,0 +1,13 @@ +package rep; + +public class GUITest { + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +}
--- a/rep/RPanel.java Wed Feb 21 15:08:52 2007 +0900 +++ b/rep/RPanel.java Tue Mar 20 16:19:33 2007 +0900 @@ -2,6 +2,7 @@ import java.awt.BorderLayout; import java.awt.Component; +import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -10,36 +11,74 @@ import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.JScrollBar; +import javax.swing.JScrollPane; 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 JTextField textField; + private String host; private JLabel label; + private JTextArea textArea; + private JScrollBar scrollBar; + private JScrollPane scrollPane; + private SessionViewer sessionViewer; + private JScrollPane viewerPane; + private ConnectionListener listener; public RPanel() { button = new JButton("Connect"); - textField = new JTextArea("133.13.54.54"); - //textField.setSize(100, 50); + textField = new JTextField("133.13.54.54"); + textArea = new JTextArea(); label = new JLabel("test"); + scrollPane = new JScrollPane(textArea); + sessionViewer = new SessionViewer(); + viewerPane = new JScrollPane(sessionViewer.getTree()); + //scrollBar = new JScrollBar(JScrollBar.VERTICAL); + + + button.setBounds(160, 5, 100, 20); + textField.setBounds(5, 5, 150, 20); + textArea.setEditable(false); + textArea.setLineWrap(false); + scrollPane.setBounds(5, 30, 200, 200); + scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + viewerPane.setBounds(5, 30, 200, 200); + viewerPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + //scrollPane.setPreferredSize(new Dimension(200, 200)); + //textArea.setBounds(5, 30, 200, 200); + //textArea.add(scrollBar, BorderLayout.EAST); - this.setLayout(new FlowLayout()); - this.add(textField, FlowLayout.LEFT); - this.add( button, FlowLayout.CENTER); - this.add(label, FlowLayout.RIGHT); + this.setLayout(null); + this.add(textField); + this.add( button); + this.add(label); + //this.add(textArea); + //this.add(scrollPane, BorderLayout.CENTER); + this.add(viewerPane, BorderLayout.CENTER); //this.add(label, BorderLayout.CENTER); button.addActionListener(this); } + public void addSessionTree(int SID){ + sessionViewer.addSessionTree(SID); + } + public void actionPerformed(ActionEvent event) { if (event.getSource() == button) { - text = textField.getText(); + host = textField.getText(); + listener.connectionOccured(new ConnectionEvent(host)); } } + public void addConnectionListener(ConnectionListener listener) { + System.out.println(listener.toString()); + this.listener = listener; + } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/SMConnector.java Tue Mar 20 16:19:33 2007 +0900 @@ -0,0 +1,28 @@ +package rep; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.channels.SocketChannel; + +public class SMConnector { + private SocketChannel sessionchannel; + + public void connectSession(String host) { + int port = 8765; + //int port = Integer.parseInt(args[2]); + InetSocketAddress addr = new InetSocketAddress(host, port); + try { + sessionchannel = SocketChannel.open(); + sessionchannel.configureBlocking(true); + sessionchannel.connect(addr); + System.out.println("connect"); + //sessionchannel.configureBlocking(false); + while(!sessionchannel.finishConnect()){ + System.out.println("afro"); + } + //registerChannel(selector, sessionchannel, SelectionKey.OP_READ); + }catch (IOException e) { + e.printStackTrace(); + } + } +}
--- a/rep/Session.java Wed Feb 21 15:08:52 2007 +0900 +++ b/rep/Session.java Tue Mar 20 16:19:33 2007 +0900 @@ -18,7 +18,6 @@ editorList.add(new Editor(editorID, channel)); } public LinkedList getEditorList() { - // TODO Auto-generated method stub return editorList; } }
--- a/rep/SessionList.java Wed Feb 21 15:08:52 2007 +0900 +++ b/rep/SessionList.java Tue Mar 20 16:19:33 2007 +0900 @@ -14,6 +14,7 @@ private int editorCount = 0; private SocketChannel smchannel; + private boolean channelequals = false; public void add(SocketChannel channel) { @@ -54,7 +55,7 @@ editorCount++; return editorCount; } - + public void sendCmd(SocketChannel channel2, REPCommand repCmd) { //int sessionID = repCmd.sid; //LinkedList <SocketChannel> channelList = sessions2.get(repCmd.sid); @@ -69,6 +70,23 @@ repSend.send(repCmd); } } + + public void sendCmd(SocketChannel channel, REPCommand repCmd, boolean ring){ + LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList(); + REPPacketSend repsend = null; + for(Editor editor : editorList){ + SocketChannel channel2 = editor.getChannel(); + if(channel.equals(channel2)){ + channelequals = true; + }else if(channelequals){ + repsend = new REPPacketSend(channel2); + repsend.send(repCmd); + } + } + //repsend.send(repCmd); + //repCmd.setCMD(repCmd.cmd + 1); + //repsend.send(repCmd); + } public void addSessionManager(SocketChannel channel, REPCommand repCmd) { smchannel = channel; @@ -76,7 +94,6 @@ } 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 Wed Feb 21 15:08:52 2007 +0900 +++ b/rep/SessionManager.java Tue Mar 20 16:19:33 2007 +0900 @@ -4,6 +4,7 @@ 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; @@ -24,12 +25,20 @@ int textsize; // textsize byte[] text;*/ -public class SessionManager { +public class SessionManager implements ConnectionListener{ private SessionList sessionlist; SocketChannel sessionchannel; - public SessionManager(int port) {} + private SessionManagerGUI sessionmanagerGUI; + private Selector selector; + public SessionManager(int port) { + sessionmanagerGUI = new SessionManagerGUI(); + } + + public void openSelector() throws IOException{ + selector = Selector.open(); + } public void sessionManagerNet(int port) throws InterruptedException, IOException { /** @@ -39,7 +48,8 @@ * @throws IOException * @throws InterruptedException */ - Selector selector = Selector.open(); + System.out.println("sessionManagerNet()"); + ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); ssc.socket().bind(new InetSocketAddress(port)); @@ -56,9 +66,10 @@ 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); + //if(channel == null) continue; + //channel.configureBlocking(false); + //channel.register(selector, SelectionKey.OP_READ); + registerChannel (selector, channel, SelectionKey.OP_READ); channel = null; } else if(key.isReadable()){ @@ -71,6 +82,15 @@ } } + private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException { + if(channel == null) { + return; + } + System.out.println("registerChannel()"); + channel.configureBlocking(false); + channel.register(selector, ops); + } + private void manager(SocketChannel channel, REPCommand repCmd) { if(repCmd == null) return; switch(repCmd.cmd){ @@ -82,6 +102,7 @@ repSend.send(repCmd); break; case REP.SMCMD_JOIN_ACK: + break; case REP.SMCMD_PUT: int sessionID = sessionlist.addSession(channel, repCmd.string); @@ -118,30 +139,37 @@ if(args.length == 1){ port = Integer.parseInt(args[1]); } -// SessionManagerGUI gui = new SessionManagerGUI(); -// Thread th = new Thread( gui ); -// th.start(); SessionManager sm = new SessionManager(port); - if(args.length == 3){ - sm.connectSession(args); - } + sm.openSelector(); + sm.openWindow(); sm.sessionManagerNet(port); } - private void connectSession(String[] args) { - // TODO Auto-generated method stub - int port = Integer.parseInt(args[2]); - String host = args[1]; + 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 = 8765; + //int port = Integer.parseInt(args[2]); InetSocketAddress addr = new InetSocketAddress(host, port); try { sessionchannel = SocketChannel.open(); sessionchannel.configureBlocking(true); sessionchannel.connect(addr); - while(!sessionchannel.finishConnect()){ - System.out.println("afro"); - } + + //sessionchannel.configureBlocking(false); + + registerChannel(selector, sessionchannel, SelectionKey.OP_READ); }catch (IOException e) { e.printStackTrace(); } } + + public void connectionOccured(ConnectionEvent event) { + connectSession(event.getHost()); + } }
--- a/rep/SessionManagerGUI.java Wed Feb 21 15:08:52 2007 +0900 +++ b/rep/SessionManagerGUI.java Tue Mar 20 16:19:33 2007 +0900 @@ -8,16 +8,37 @@ import javax.swing.JPanel; public class SessionManagerGUI implements Runnable{ - public static void main(String[] args) {} + public static void main(String[] args) { + SessionManagerGUI gui = new SessionManagerGUI(); + Thread th = new Thread( gui ); + th.start(); + } + + private RPanel rp; + private JFrame frame; public void run() { - JFrame frame = new JFrame("SessionManager"); - RPanel rp = new RPanel(); + frame = new JFrame("SessionManager"); + frame.setBounds(100, 100, 400, 300); + rp = new RPanel(); Container cont = frame.getContentPane(); cont.add(rp); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.pack(); + //frame.pack(); frame.setVisible(true); } + + public void addConnectionListener(ConnectionListener listener) { + //System.out.println(rp.toString()); + while(rp == null){ + System.out.println("waiting for launching GUI."); + try { + Thread.sleep(50); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + rp.addConnectionListener(listener); + } } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/SessionViewer.java Tue Mar 20 16:19:33 2007 +0900 @@ -0,0 +1,60 @@ +package rep; + +import java.awt.Component; + +import javax.swing.JFrame; +import javax.swing.JTree; +import javax.swing.event.TreeSelectionEvent; +import javax.swing.event.TreeSelectionListener; +import javax.swing.tree.DefaultMutableTreeNode; + +public class SessionViewer implements TreeSelectionListener { + /** + * + */ + private static final long serialVersionUID = 1L; + DefaultMutableTreeNode nRoot = new DefaultMutableTreeNode("top"); + DefaultMutableTreeNode nJapan = new DefaultMutableTreeNode("Session:1"); + DefaultMutableTreeNode nUsa = new DefaultMutableTreeNode("Session:2"); + DefaultMutableTreeNode nKantoh = new DefaultMutableTreeNode("Editor:1"); + DefaultMutableTreeNode nKansai = new DefaultMutableTreeNode("Editor:2"); + DefaultMutableTreeNode nTokyo = new DefaultMutableTreeNode("Editor:3"); + private JTree tree; + SessionViewer() { + tree = new JTree(nRoot); + tree.setRootVisible(true); + tree.addTreeSelectionListener(this); + //getContentPane().add(tree); + + nRoot.add(nJapan); + nRoot.add(nUsa); + nJapan.add(nKantoh); + nJapan.add(nKansai); + nUsa.add(nTokyo); + //nJapan.remove(0); + + //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //setTitle("JTreeTest"); + //setSize(200, 150); + //setVisible(true); + } + public void valueChanged(TreeSelectionEvent e) { + JTree tree = (JTree)e.getSource(); + DefaultMutableTreeNode node = + (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); + if (node != null) { + System.out.println((String)node.getUserObject()); + } + } + public static void main(String[] args) { + new SessionViewer(); + } + public Component getTree() { + // TODO Auto-generated method stub + return tree; + } + public void addSessionTree(int sid) { + // TODO Auto-generated method stub + nRoot.add(new DefaultMutableTreeNode("Session:"+sid)); + } +}