annotate rep/SessionManager.java @ 2:02fa9a68d9a2

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