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