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
|
8
|
28 public class SessionManager implements ConnectionListener, REPActionListener{
|
0
|
29
|
|
30
|
|
31 private SessionList sessionlist;
|
5
|
32 //SocketChannel sessionchannel;
|
2
|
33 private SessionManagerGUI sessionmanagerGUI;
|
|
34 private Selector selector;
|
7
|
35 private SessionManagerList smList;
|
6
|
36 //private SocketChannel sessionchannel;
|
7
|
37 //private boolean co;
|
2
|
38 public SessionManager(int port) {
|
|
39 sessionmanagerGUI = new SessionManagerGUI();
|
|
40 }
|
|
41
|
|
42 public void openSelector() throws IOException{
|
|
43 selector = Selector.open();
|
|
44 }
|
0
|
45
|
|
46 public void sessionManagerNet(int port) throws InterruptedException, IOException {
|
|
47 /**
|
|
48 * @param args
|
|
49 * @throws IOException
|
|
50 * @throws InterruptedException
|
|
51 * @throws IOException
|
|
52 * @throws InterruptedException
|
|
53 */
|
2
|
54 System.out.println("sessionManagerNet()");
|
|
55
|
0
|
56 ServerSocketChannel ssc = ServerSocketChannel.open();
|
|
57 ssc.configureBlocking(false);
|
|
58 ssc.socket().bind(new InetSocketAddress(port));
|
|
59 ssc.register(selector, SelectionKey.OP_ACCEPT);
|
6
|
60
|
|
61
|
0
|
62 sessionlist = new SessionList();
|
7
|
63 smList = new SessionManagerList();
|
0
|
64
|
|
65 while(true){
|
|
66 selector.select();
|
|
67 for(SelectionKey key : selector.selectedKeys()){
|
|
68 if(key.isAcceptable()){
|
|
69 SocketChannel channel = ssc.accept();
|
2
|
70 registerChannel (selector, channel, SelectionKey.OP_READ);
|
0
|
71 channel = null;
|
6
|
72 }else if(key.isReadable()){
|
0
|
73 SocketChannel channel = (SocketChannel)key.channel();
|
|
74 REPPacketReceive repRec = new REPPacketReceive(channel);
|
|
75 REPCommand repCom = repRec.unpack();
|
|
76 manager(channel, repCom);
|
6
|
77 }else if(key.isConnectable()){
|
|
78 System.out.println("Connectable");
|
0
|
79 }
|
|
80 }
|
|
81 }
|
|
82 }
|
1
|
83
|
2
|
84 private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
|
|
85 if(channel == null) {
|
|
86 return;
|
|
87 }
|
|
88 System.out.println("registerChannel()");
|
|
89 channel.configureBlocking(false);
|
6
|
90 selector.wakeup();
|
2
|
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:
|
8
|
98 int eid = sessionlist.getNumberOfEditor(); //eid���擾
|
|
99 sessionmanagerGUI.setComboEditor(eid, channel); //ComboBox��Editor��lj�
|
|
100 repCmd.setEID(eid); //eid�����肵�āA
|
|
101 repCmd.setCMD(repCmd.cmd + 1); //ACK��Ԃ�
|
|
102 repCmd.string = sessionlist.getSessionList(); //Session�ꗗ��
|
|
103 REPPacketSend repSend = new REPPacketSend(channel); //Editor���֑��M
|
0
|
104 repSend.send(repCmd);
|
|
105 break;
|
1
|
106 case REP.SMCMD_JOIN_ACK:
|
2
|
107
|
1
|
108 break;
|
0
|
109 case REP.SMCMD_PUT:
|
8
|
110 int sessionID = sessionlist.addSession(channel, repCmd.string); //SID���擾
|
|
111 smList.update(sessionID, repCmd.string); //update�R�}���h:Session������ʒm
|
|
112 sessionmanagerGUI.setComboSession(sessionID, repCmd.string);
|
|
113 repCmd.setSID(sessionID); //SID���Z�b�g
|
|
114 repCmd.setCMD(repCmd.cmd + 1); //ACK��Ԃ�
|
0
|
115 REPPacketSend repSend2 = new REPPacketSend(channel);
|
|
116 repSend2.send(repCmd);
|
8
|
117 //sessionlist.sendAddedSession(repCmd);
|
1
|
118 break;
|
|
119 case REP.SMCMD_PUT_ACK:
|
0
|
120 break;
|
|
121 case REP.SMCMD_SELECT:
|
8
|
122 sessionlist.addEditor(channel, repCmd.sid, repCmd); //sessionlist�֒lj�
|
0
|
123 repCmd.setCMD(repCmd.cmd + 1);
|
|
124 REPPacketSend repSend3 = new REPPacketSend(channel);
|
8
|
125 repSend3.send(repCmd); //ACK��Ԃ�
|
|
126 break;
|
|
127 case REP.SMCMD_SELECT_ACK:
|
|
128 break;
|
1
|
129 case REP.SMCMD_SESSION:
|
8
|
130 //repCmd.setCMD(REP.SMCMD_SESSION_ACK);
|
|
131 //sessionlist.addSessionManager(channel, repCmd);
|
1
|
132 break;
|
8
|
133 case REP.SMCMD_SM_JOIN:
|
|
134 smList.add(channel); //SessionManager�̃��X�g�֒lj�
|
|
135 repCmd.setCMD(repCmd.cmd + 1);
|
6
|
136 REPPacketSend repSend4 = new REPPacketSend(channel);
|
8
|
137 repSend4.send(repCmd); //ACK
|
|
138 break;
|
|
139 case REP.SMCMD_SM_JOIN_ACK:
|
|
140 smList.add(channel); //SessionManager�̃��X�g�֒lj�
|
6
|
141 break;
|
8
|
142 case REP.SMCMD_UPDATE:
|
|
143 int sessionID2 = sessionlist.addSession(channel, repCmd.string);
|
|
144 //sessionlist.sendAddedSession(repCmd);
|
|
145 sessionmanagerGUI.setComboSession(sessionID2, repCmd.string);
|
1
|
146 break;
|
3
|
147 case REP.REPCMD_READ:
|
|
148 sessionlist.sendCmd(channel, repCmd);
|
|
149 break;
|
0
|
150 default:
|
8
|
151 sessionlist.sendCmd(channel, repCmd, true);
|
|
152 //sessionlist.sendToNextEditor(channel, repCmd);
|
0
|
153 break;
|
|
154
|
|
155 }
|
|
156 }
|
|
157
|
|
158 public static void main(String[] args) throws InterruptedException, IOException {
|
6
|
159 int port = 8766;
|
1
|
160
|
0
|
161 if(args.length == 1){
|
|
162 port = Integer.parseInt(args[1]);
|
|
163 }
|
|
164 SessionManager sm = new SessionManager(port);
|
2
|
165 sm.openSelector();
|
|
166 sm.openWindow();
|
0
|
167 sm.sessionManagerNet(port);
|
|
168 }
|
|
169
|
2
|
170 private void openWindow() {
|
|
171 Thread th = new Thread( sessionmanagerGUI );
|
|
172 th.start();
|
|
173 System.out.println(sessionmanagerGUI.toString());
|
|
174 sessionmanagerGUI.addConnectionListener(this);
|
8
|
175 sessionmanagerGUI.addREPActionListener(this);
|
2
|
176 }
|
|
177
|
|
178 private void connectSession(String host) {
|
6
|
179 int port = 8766;
|
|
180 //SocketChannel sessionchannel;
|
2
|
181 //int port = Integer.parseInt(args[2]);
|
1
|
182 InetSocketAddress addr = new InetSocketAddress(host, port);
|
|
183 try {
|
6
|
184 SocketChannel sessionchannel = SocketChannel.open();
|
1
|
185 sessionchannel.configureBlocking(true);
|
|
186 sessionchannel.connect(addr);
|
6
|
187 REPPacketSend send = new REPPacketSend(sessionchannel);
|
|
188 while(!sessionchannel.finishConnect()){
|
|
189 System.out.print(".");
|
|
190 }
|
|
191 System.out.println("");
|
2
|
192 registerChannel(selector, sessionchannel, SelectionKey.OP_READ);
|
6
|
193 send.send(REPCommand.SMCMD_SESSION_JOIN);
|
7
|
194 smList.add(sessionchannel);
|
1
|
195 }catch (IOException e) {
|
|
196 e.printStackTrace();
|
|
197 }
|
|
198 }
|
2
|
199
|
|
200 public void connectionOccured(ConnectionEvent event) {
|
|
201 connectSession(event.getHost());
|
|
202 }
|
8
|
203
|
|
204 public void ActionOccured(REPActionEvent event) {
|
|
205 // TODO Auto-generated method stub
|
|
206 System.out.println("Action!");
|
|
207 SocketChannel editorChannel = event.getEditorChannel();
|
|
208 int sid = event.getSID();
|
|
209 int eid = event.getEID();
|
|
210 sessionlist.addEditor(editorChannel, sid, eid);
|
|
211 REPPacketSend send = new REPPacketSend(editorChannel);
|
|
212 send.send(new REPCommand(REP.SMCMD_SELECT_ACK, sid, eid, 0,0,0,""));
|
|
213 sessionlist.sendSelect(sid);
|
|
214 }
|
0
|
215 }
|