comparison rep/SessionManager.java @ 2:02fa9a68d9a2

*** empty log message ***
author pin
date Tue, 20 Mar 2007 16:19:33 +0900
parents 3f5bf0255f5e
children f31fcac5a949
comparison
equal deleted inserted replaced
1:3f5bf0255f5e 2:02fa9a68d9a2
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.net.InetSocketAddress; 4 import java.net.InetSocketAddress;
5 import java.net.ServerSocket; 5 import java.net.ServerSocket;
6 import java.nio.ByteBuffer; 6 import java.nio.ByteBuffer;
7 import java.nio.channels.SelectableChannel;
7 import java.nio.channels.SelectionKey; 8 import java.nio.channels.SelectionKey;
8 import java.nio.channels.Selector; 9 import java.nio.channels.Selector;
9 import java.nio.channels.ServerSocketChannel; 10 import java.nio.channels.ServerSocketChannel;
10 import java.nio.channels.SocketChannel; 11 import java.nio.channels.SocketChannel;
11 import java.nio.charset.Charset; 12 import java.nio.charset.Charset;
22 int seqno; // Sequence number 23 int seqno; // Sequence number
23 int lineno; // line number 24 int lineno; // line number
24 int textsize; // textsize 25 int textsize; // textsize
25 byte[] text;*/ 26 byte[] text;*/
26 27
27 public class SessionManager { 28 public class SessionManager implements ConnectionListener{
28 29
29 30
30 private SessionList sessionlist; 31 private SessionList sessionlist;
31 SocketChannel sessionchannel; 32 SocketChannel sessionchannel;
32 public SessionManager(int port) {} 33 private SessionManagerGUI sessionmanagerGUI;
34 private Selector selector;
35 public SessionManager(int port) {
36 sessionmanagerGUI = new SessionManagerGUI();
37 }
38
39 public void openSelector() throws IOException{
40 selector = Selector.open();
41 }
33 42
34 public void sessionManagerNet(int port) throws InterruptedException, IOException { 43 public void sessionManagerNet(int port) throws InterruptedException, IOException {
35 /** 44 /**
36 * @param args 45 * @param args
37 * @throws IOException 46 * @throws IOException
38 * @throws InterruptedException 47 * @throws InterruptedException
39 * @throws IOException 48 * @throws IOException
40 * @throws InterruptedException 49 * @throws InterruptedException
41 */ 50 */
42 Selector selector = Selector.open(); 51 System.out.println("sessionManagerNet()");
52
43 ServerSocketChannel ssc = ServerSocketChannel.open(); 53 ServerSocketChannel ssc = ServerSocketChannel.open();
44 ssc.configureBlocking(false); 54 ssc.configureBlocking(false);
45 ssc.socket().bind(new InetSocketAddress(port)); 55 ssc.socket().bind(new InetSocketAddress(port));
46 ssc.register(selector, SelectionKey.OP_ACCEPT); 56 ssc.register(selector, SelectionKey.OP_ACCEPT);
47 if (sessionchannel != null) { 57 if (sessionchannel != null) {
54 while(true){ 64 while(true){
55 selector.select(); 65 selector.select();
56 for(SelectionKey key : selector.selectedKeys()){ 66 for(SelectionKey key : selector.selectedKeys()){
57 if(key.isAcceptable()){ 67 if(key.isAcceptable()){
58 SocketChannel channel = ssc.accept(); 68 SocketChannel channel = ssc.accept();
59 if(channel == null) continue; 69 //if(channel == null) continue;
60 channel.configureBlocking(false); 70 //channel.configureBlocking(false);
61 channel.register(selector, SelectionKey.OP_READ); 71 //channel.register(selector, SelectionKey.OP_READ);
72 registerChannel (selector, channel, SelectionKey.OP_READ);
62 channel = null; 73 channel = null;
63 } 74 }
64 else if(key.isReadable()){ 75 else if(key.isReadable()){
65 SocketChannel channel = (SocketChannel)key.channel(); 76 SocketChannel channel = (SocketChannel)key.channel();
66 REPPacketReceive repRec = new REPPacketReceive(channel); 77 REPPacketReceive repRec = new REPPacketReceive(channel);
69 } 80 }
70 } 81 }
71 } 82 }
72 } 83 }
73 84
85 private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
86 if(channel == null) {
87 return;
88 }
89 System.out.println("registerChannel()");
90 channel.configureBlocking(false);
91 channel.register(selector, ops);
92 }
93
74 private void manager(SocketChannel channel, REPCommand repCmd) { 94 private void manager(SocketChannel channel, REPCommand repCmd) {
75 if(repCmd == null) return; 95 if(repCmd == null) return;
76 switch(repCmd.cmd){ 96 switch(repCmd.cmd){
77 case REP.SMCMD_JOIN: 97 case REP.SMCMD_JOIN:
78 int eid = sessionlist.getNumberOfEditor(); 98 int eid = sessionlist.getNumberOfEditor();
80 repCmd.setCMD(repCmd.cmd + 1); 100 repCmd.setCMD(repCmd.cmd + 1);
81 REPPacketSend repSend = new REPPacketSend(channel); 101 REPPacketSend repSend = new REPPacketSend(channel);
82 repSend.send(repCmd); 102 repSend.send(repCmd);
83 break; 103 break;
84 case REP.SMCMD_JOIN_ACK: 104 case REP.SMCMD_JOIN_ACK:
105
85 break; 106 break;
86 case REP.SMCMD_PUT: 107 case REP.SMCMD_PUT:
87 int sessionID = sessionlist.addSession(channel, repCmd.string); 108 int sessionID = sessionlist.addSession(channel, repCmd.string);
88 repCmd.setSID(sessionID); 109 repCmd.setSID(sessionID);
89 repCmd.setCMD(repCmd.cmd + 1); 110 repCmd.setCMD(repCmd.cmd + 1);
116 int port = 8765; 137 int port = 8765;
117 138
118 if(args.length == 1){ 139 if(args.length == 1){
119 port = Integer.parseInt(args[1]); 140 port = Integer.parseInt(args[1]);
120 } 141 }
121 // SessionManagerGUI gui = new SessionManagerGUI();
122 // Thread th = new Thread( gui );
123 // th.start();
124 SessionManager sm = new SessionManager(port); 142 SessionManager sm = new SessionManager(port);
125 if(args.length == 3){ 143 sm.openSelector();
126 sm.connectSession(args); 144 sm.openWindow();
127 }
128 sm.sessionManagerNet(port); 145 sm.sessionManagerNet(port);
129 } 146 }
130 147
131 private void connectSession(String[] args) { 148 private void openWindow() {
132 // TODO Auto-generated method stub 149 Thread th = new Thread( sessionmanagerGUI );
133 int port = Integer.parseInt(args[2]); 150 th.start();
134 String host = args[1]; 151 System.out.println(sessionmanagerGUI.toString());
152 sessionmanagerGUI.addConnectionListener(this);
153 }
154
155 private void connectSession(String host) {
156 int port = 8765;
157 //int port = Integer.parseInt(args[2]);
135 InetSocketAddress addr = new InetSocketAddress(host, port); 158 InetSocketAddress addr = new InetSocketAddress(host, port);
136 try { 159 try {
137 sessionchannel = SocketChannel.open(); 160 sessionchannel = SocketChannel.open();
138 sessionchannel.configureBlocking(true); 161 sessionchannel.configureBlocking(true);
139 sessionchannel.connect(addr); 162 sessionchannel.connect(addr);
140 while(!sessionchannel.finishConnect()){ 163
141 System.out.println("afro"); 164 //sessionchannel.configureBlocking(false);
142 } 165
166 registerChannel(selector, sessionchannel, SelectionKey.OP_READ);
143 }catch (IOException e) { 167 }catch (IOException e) {
144 e.printStackTrace(); 168 e.printStackTrace();
145 } 169 }
146 } 170 }
171
172 public void connectionOccured(ConnectionEvent event) {
173 connectSession(event.getHost());
174 }
147 } 175 }