0
|
1 package rep;
|
|
2
|
|
3 // +-------+--------+--------+-------+--------+---------+------+
|
|
4 // | cmd | session| editor | seqid | lineno | textsiz | text |
|
|
5 // | | id | id | | | | |
|
|
6 // +-------+--------+--------+-------+--------+---------+------+
|
|
7 // o-------header section (network order)-------------o
|
|
8 /*int cmd; // command
|
|
9 int sid; // session ID
|
|
10 int eid; // editor ID
|
|
11 int seqno; // Sequence number
|
|
12 int lineno; // line number
|
|
13 int textsize; // textsize
|
|
14 byte[] text;*/
|
|
15
|
|
16 import java.io.IOException;
|
|
17 import java.net.InetSocketAddress;
|
|
18 import java.net.ServerSocket;
|
|
19 import java.nio.ByteBuffer;
|
|
20 import java.nio.channels.SelectionKey;
|
|
21 import java.nio.channels.Selector;
|
|
22 import java.nio.channels.ServerSocketChannel;
|
|
23 import java.nio.channels.SocketChannel;
|
|
24 import java.nio.charset.Charset;
|
|
25 import java.util.Iterator;
|
|
26
|
|
27 public class SessionManager {
|
|
28
|
|
29
|
|
30 private SessionList sessionlist;
|
|
31
|
|
32 public SessionManager(int port) {
|
|
33 //manager(port);
|
|
34 }
|
|
35
|
|
36 public void sessionManagerNet(int port) throws InterruptedException, IOException {
|
|
37 /**
|
|
38 * @param args
|
|
39 * @throws IOException
|
|
40 * @throws InterruptedException
|
|
41 * @throws IOException
|
|
42 * @throws InterruptedException
|
|
43 */
|
|
44 Selector selector = Selector.open();
|
|
45 ServerSocketChannel ssc = ServerSocketChannel.open();
|
|
46 ssc.configureBlocking(false);
|
|
47 ssc.socket().bind(new InetSocketAddress(port));
|
|
48 ssc.register(selector, SelectionKey.OP_ACCEPT);
|
|
49 sessionlist = new SessionList();
|
|
50
|
|
51 while(true){
|
|
52 selector.select();
|
|
53 for(SelectionKey key : selector.selectedKeys()){
|
|
54 if(key.isAcceptable()){
|
|
55 SocketChannel channel = ssc.accept();
|
|
56 if(channel == null) continue;
|
|
57 channel.configureBlocking(false);
|
|
58 channel.register(selector, SelectionKey.OP_READ);
|
|
59 //sessionlist.add(channel);
|
|
60 channel = null;
|
|
61 }
|
|
62 else if(key.isReadable()){
|
|
63 SocketChannel channel = (SocketChannel)key.channel();
|
|
64 REPPacketReceive repRec = new REPPacketReceive(channel);
|
|
65 REPCommand repCom = repRec.unpack();
|
|
66 manager(channel, repCom);
|
|
67 //Charset charset = Charset.forName("US-ASCII");
|
|
68 //ByteBuffer buffer = ByteBuffer.allocate(8192);
|
|
69 //switch(channel.read(buffer)) {
|
|
70 //case -1:
|
|
71 // channel.close();
|
|
72 // break;
|
|
73 //case 0:
|
|
74 // continue;
|
|
75 // default:
|
|
76 // buffer.flip();
|
|
77 // System.out.println(charset.decode(buffer));
|
|
78 // channel.write(charset.encode("test"));
|
|
79 // break;
|
|
80 //}
|
|
81 }
|
|
82 }
|
|
83 }
|
|
84 }
|
|
85 private void manager(SocketChannel channel, REPCommand repCmd) {
|
|
86 if(repCmd == null) return;
|
|
87 switch(repCmd.cmd){
|
|
88 case REP.SMCMD_JOIN:
|
|
89
|
|
90 int eid = sessionlist.getNumberOfEditor();
|
|
91 repCmd.setEID(eid);
|
|
92 repCmd.setCMD(repCmd.cmd + 1);
|
|
93 REPPacketSend repSend = new REPPacketSend(channel);
|
|
94 repSend.send(repCmd);
|
|
95 break;
|
|
96 case REP.SMCMD_PUT:
|
|
97 int sessionID = sessionlist.addSession(channel, repCmd.string);
|
|
98 repCmd.setSID(sessionID);
|
|
99 repCmd.setCMD(repCmd.cmd + 1);
|
|
100 //repCmd.setSID(sessionlist.getSessionID(channel));
|
|
101 REPPacketSend repSend2 = new REPPacketSend(channel);
|
|
102 repSend2.send(repCmd);
|
|
103 break;
|
|
104 case REP.SMCMD_SELECT:
|
|
105 sessionlist.addEditor(channel, repCmd.sid);
|
|
106 repCmd.setCMD(repCmd.cmd + 1);
|
|
107 REPPacketSend repSend3 = new REPPacketSend(channel);
|
|
108 repSend3.send(repCmd);
|
|
109 //case REP.REPCMD_INSERT:
|
|
110 // break;
|
|
111 default:
|
|
112 sessionlist.sendCmd(channel, repCmd);
|
|
113 break;
|
|
114
|
|
115 }
|
|
116 }
|
|
117
|
|
118 public static void main(String[] args) throws InterruptedException, IOException {
|
|
119 int port;
|
|
120 if(args.length == 1){
|
|
121 port = Integer.parseInt(args[1]);
|
|
122 }else{
|
|
123 port = 8765;
|
|
124 }
|
|
125 SessionManager sm = new SessionManager(port);
|
|
126 sm.sessionManagerNet(port);
|
|
127 }
|
|
128
|
|
129 }
|