0
|
1 package rep;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.InetSocketAddress;
|
11
|
5 import java.net.SocketAddress;
|
2
|
6 import java.nio.channels.SelectableChannel;
|
0
|
7 import java.nio.channels.SelectionKey;
|
|
8 import java.nio.channels.Selector;
|
|
9 import java.nio.channels.ServerSocketChannel;
|
|
10 import java.nio.channels.SocketChannel;
|
83
|
11 import java.util.LinkedList;
|
15
|
12 import java.util.StringTokenizer;
|
0
|
13
|
56
|
14 import rep.xml.SessionXMLDecoder;
|
45
|
15 import rep.xml.SessionXMLEncoder;
|
|
16
|
1
|
17 //+-------+--------+--------+-------+--------+---------+------+
|
|
18 //| cmd | session| editor | seqid | lineno | textsiz | text |
|
|
19 //| | id | id | | | | |
|
|
20 //+-------+--------+--------+-------+--------+---------+------+
|
|
21 //o-------header section (network order)-------------o
|
|
22 /*int cmd; // command
|
|
23 int sid; // session ID
|
|
24 int eid; // editor ID
|
|
25 int seqno; // Sequence number
|
|
26 int lineno; // line number
|
|
27 int textsize; // textsize
|
|
28 byte[] text;*/
|
|
29
|
8
|
30 public class SessionManager implements ConnectionListener, REPActionListener{
|
0
|
31
|
|
32
|
|
33 private SessionList sessionlist;
|
5
|
34 //SocketChannel sessionchannel;
|
83
|
35 private SessionManagerGUI gui;
|
2
|
36 private Selector selector;
|
7
|
37 private SessionManagerList smList;
|
17
|
38 private String myHost;
|
21
|
39 private boolean isMaster = true;
|
77
|
40 private EditorList allEditorList;
|
78
|
41 private String maxHost;
|
76
|
42 //private boolean addressIsGlobal;
|
6
|
43 //private SocketChannel sessionchannel;
|
7
|
44 //private boolean co;
|
2
|
45 public SessionManager(int port) {
|
83
|
46 gui = new SessionManagerGUI();
|
2
|
47 }
|
|
48
|
|
49 public void openSelector() throws IOException{
|
|
50 selector = Selector.open();
|
|
51 }
|
0
|
52
|
|
53 public void sessionManagerNet(int port) throws InterruptedException, IOException {
|
2
|
54
|
0
|
55 ServerSocketChannel ssc = ServerSocketChannel.open();
|
38
|
56 ssc.configureBlocking(false);
|
0
|
57 ssc.socket().bind(new InetSocketAddress(port));
|
|
58 ssc.register(selector, SelectionKey.OP_ACCEPT);
|
6
|
59
|
|
60
|
0
|
61 sessionlist = new SessionList();
|
7
|
62 smList = new SessionManagerList();
|
77
|
63 allEditorList = new EditorList();
|
0
|
64
|
|
65 while(true){
|
|
66 selector.select();
|
|
67 for(SelectionKey key : selector.selectedKeys()){
|
|
68 if(key.isAcceptable()){
|
28
|
69 /*** serverChannelはenableになったSelectionKeyのchannel ***/
|
|
70 ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel();
|
9
|
71 /*** EditorChannel を用いない記述 ***/
|
28
|
72 SocketChannel channel = serverChannel.accept(); //keyからchannelを取って、accept
|
2
|
73 registerChannel (selector, channel, SelectionKey.OP_READ);
|
0
|
74 channel = null;
|
9
|
75
|
|
76 /*** EditorChannel を用いた記述 ****/
|
|
77 //EditorChannel echannel = (EditorChannel) ssc.accept();
|
|
78 //echannel.setIO();
|
|
79 //registerChannel(selector, echannel, SelectionKey.OP_READ);
|
|
80 //echannel = null;
|
|
81
|
|
82 /*** SelectableEditorChannel ***/
|
|
83 //SocketChannel channel = ssc.accept();
|
|
84 //SelectableEditorChannel echannel2 = new SelectableEditorChannel(channel);
|
|
85 //registerChannel(selector, echannel2, SelectionKey.OP_READ);
|
|
86 //channel = null;
|
|
87 //echannel2 = null;
|
|
88
|
6
|
89 }else if(key.isReadable()){
|
9
|
90
|
|
91 /*** EditorChannel を用いない記述 ***/
|
0
|
92 SocketChannel channel = (SocketChannel)key.channel();
|
75
|
93 REPPacketReceive receive = new REPPacketReceive(channel); //getPacket(), putPacket() にする。
|
|
94 receive.setkey(key);
|
53
|
95 //REPCommand repCom = repRec.unpackUConv();
|
75
|
96 REPCommand receivedCommand = receive.unpack();
|
|
97 manager(channel, receivedCommand);
|
9
|
98
|
|
99 /*** EditorChannel を用いた記述 ****/
|
|
100 //EditorChannel echannel = (EditorChannel) key.channel();
|
|
101 //REPCommand command = echannel.getPacket();
|
|
102 //manager(echannel, command);
|
|
103
|
6
|
104 }else if(key.isConnectable()){
|
|
105 System.out.println("Connectable");
|
21
|
106 }
|
0
|
107 }
|
|
108 }
|
|
109 }
|
1
|
110
|
2
|
111 private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
|
|
112 if(channel == null) {
|
|
113 return;
|
|
114 }
|
9
|
115 //System.out.println("registerChannel()");
|
2
|
116 channel.configureBlocking(false);
|
6
|
117 selector.wakeup();
|
2
|
118 channel.register(selector, ops);
|
|
119 }
|
|
120
|
75
|
121 private void manager(SocketChannel channel, REPCommand receivedCommand) {
|
|
122 if(receivedCommand == null) return;
|
69
|
123 Editor editor;
|
|
124 Session session;
|
75
|
125 REPCommand sendCommand = receivedCommand.clone();
|
78
|
126 REPPacketSend send = new REPPacketSend(channel);
|
|
127 //SessionXMLEncoder encoder = new SessionXMLEncoder();
|
75
|
128
|
|
129 switch(receivedCommand.cmd){
|
38
|
130
|
0
|
131 case REP.SMCMD_JOIN:
|
83
|
132
|
|
133 int tempeid = allEditorList.addEditor(channel, receivedCommand);
|
|
134 //sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
|
|
135 //sendCommand.setEID(eid);
|
|
136 //REPPacketSend packetSend = new REPPacketSend(channel);
|
|
137 //packetSend.send(sendCommand);
|
|
138 gui.setComboEditor(tempeid, channel);
|
|
139
|
38
|
140 break;
|
31
|
141
|
1
|
142 case REP.SMCMD_JOIN_ACK:
|
31
|
143 // editorList.setEID(repCmd);
|
|
144 // editorList.sendJoinAck(repCmd);
|
|
145 // sessionmanagerGUI.setComboEditor(repCmd.eid, channel);
|
1
|
146 break;
|
31
|
147
|
0
|
148 case REP.SMCMD_PUT:
|
69
|
149 editor = new Editor(channel);
|
56
|
150 editor.setEID(1);
|
75
|
151 editor.setName(receivedCommand.string);
|
69
|
152 session = new Session(editor);
|
67
|
153 session.setOwner(true);
|
63
|
154 session.addEditor(editor);
|
56
|
155 sessionlist.addSession(session);
|
83
|
156 gui.setComboSession(session.getSID(), session.getName());
|
|
157 gui.setComboEditor(editor.getEID(), editor.getChannel());
|
56
|
158 session.addToRoutingTable(editor);
|
84
|
159 sendCommand.setCMD(REP.SMCMD_PUT_ACK);
|
|
160 sendCommand.setEID(1);
|
|
161 sendCommand.setSID(session.getSID());
|
|
162 editor.send(sendCommand);
|
56
|
163
|
|
164 //if(isMaster){
|
78
|
165 SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
|
56
|
166 REPCommand command = new REPCommand();
|
|
167 command.setSID(session.getSID());
|
78
|
168 command.setString(sessionEncoder.sessionListToXML());
|
56
|
169 if(isMaster){
|
|
170 command.setCMD(REP.SMCMD_UPDATE_ACK);
|
|
171 smList.sendToSlave(command);
|
|
172 }else{
|
|
173 command.setCMD(REP.SMCMD_UPDATE);
|
|
174 smList.sendToMaster(command);
|
31
|
175 }
|
38
|
176 break;
|
31
|
177
|
9
|
178 // case REP.SMCMD_PUT_ACK:
|
|
179 // break;
|
31
|
180
|
0
|
181 case REP.SMCMD_SELECT:
|
66
|
182 // sessionlist.addEditor(channel, repCmd.sid, repCmd); //sessionlistへ追加
|
70
|
183 editor = new Editor(channel);
|
75
|
184 session = sessionlist.getSession(receivedCommand.sid);
|
70
|
185 if(session.isOwner()){
|
83
|
186 int eid2 = session.addEditor(editor);
|
|
187 editor.setEID(eid2);
|
78
|
188 //REPPacketSend send = new REPPacketSend(channel);
|
84
|
189 //receivedCommand.setCMD(REP.SMCMD_SELECT_ACK);
|
|
190 //receivedCommand.setEID(eid2);
|
|
191 sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
|
|
192 sendCommand.setEID(eid2);
|
|
193 send.send(sendCommand);
|
66
|
194 }else {
|
|
195
|
|
196 }
|
|
197
|
8
|
198 break;
|
38
|
199
|
8
|
200 case REP.SMCMD_SELECT_ACK:
|
75
|
201 receivedCommand.setCMD(REP.SMCMD_JOIN_ACK);
|
|
202 receivedCommand.setEID(receivedCommand.eid);
|
|
203 session = sessionlist.getSession(receivedCommand.sid);
|
|
204 session.sendToEditor(receivedCommand);
|
69
|
205 //Editor editor3 = session3.getEditorList().get(0);
|
|
206 //REPPacketSend send = new REPPacketSend(editor3.getChannel());
|
|
207 //send.send(repCmd);
|
1
|
208 break;
|
38
|
209
|
8
|
210 case REP.SMCMD_SM_JOIN:
|
78
|
211
|
83
|
212 //SessionManagerのリストへ追加
|
|
213 smList.add(channel);
|
|
214
|
78
|
215 //XMLからSessionListオブジェクトを生成する。
|
77
|
216 SessionXMLDecoder decoder = new SessionXMLDecoder();
|
79
|
217 SessionList receivedSessionList = decoder.decode(receivedCommand.string);
|
78
|
218
|
83
|
219 //SessionListへ追加し変換テーブルを生成する。
|
|
220 sessionlist.update(channel, receivedSessionList);
|
|
221
|
78
|
222 //myHost を設定。
|
76
|
223 if(myHost == null) setMyHostName(getLocalHostName(channel));
|
78
|
224
|
79
|
225 //maxHost を設定。
|
82
|
226 setMaxHost(channel, receivedSessionList.getMaxHost());
|
77
|
227
|
78
|
228 //SessionListからXMLを生成。
|
|
229 //joinしてきたSessionManagerに対してACKを送信。
|
|
230 SessionXMLEncoder sessionlistEncoder = new SessionXMLEncoder(sessionlist);
|
|
231 sendCommand = new REPCommand();
|
|
232 sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
|
|
233 sendCommand.setString(sessionlistEncoder.sessionListToXML());
|
|
234 send.send(sendCommand);
|
|
235
|
83
|
236 //その他の SessionManager に対して SMCMD_UPDATEを 送信。
|
78
|
237 sendCommand = new REPCommand();
|
83
|
238 sendCommand.setCMD(REP.SMCMD_UPDATE);
|
78
|
239 sendCommand.setString(receivedCommand.string);
|
|
240 smList.sendExcept(channel, sendCommand);
|
|
241
|
83
|
242 //その他のSessionManagerに対してSMCMD_SM_JOINを送信。
|
|
243 //sendCommand = new REPCommand();
|
|
244 //sendCommand.setCMD(REP.SMCMD_SM_JOIN);
|
|
245 //sendCommand.setString(receivedCommand.string);
|
|
246 //smList.sendExcept(channel, sendCommand);
|
|
247
|
31
|
248 if(isMaster){
|
45
|
249 }else {
|
31
|
250 }
|
78
|
251
|
8
|
252 break;
|
38
|
253
|
8
|
254 case REP.SMCMD_SM_JOIN_ACK:
|
82
|
255
|
|
256 //XMLからSessionListオブジェクトを生成。
|
|
257 SessionXMLDecoder decoder2 = new SessionXMLDecoder();
|
|
258 SessionList receivedSessionList2 = decoder2.decode(receivedCommand.string);
|
|
259
|
|
260 //maxHostを決定。
|
|
261 setMaxHost(channel, receivedSessionList2.getMaxHost());
|
|
262
|
38
|
263 if(isMaster){
|
39
|
264 }else{
|
38
|
265 }
|
|
266
|
6
|
267 break;
|
38
|
268
|
8
|
269 case REP.SMCMD_UPDATE:
|
70
|
270
|
83
|
271 //SessionのownerのEditor
|
69
|
272 editor = new Editor(channel);
|
75
|
273 editor.setName(receivedCommand.string);
|
70
|
274
|
69
|
275 session = new Session(editor);
|
70
|
276 session.addEditor(editor);
|
|
277
|
69
|
278 sessionlist.addSession(session);
|
70
|
279
|
83
|
280 gui.setComboSession(session.getSID(), session.getName());
|
70
|
281
|
83
|
282 //if(isMaster){
|
|
283 // receivedCommand.setCMD(REP.SMCMD_UPDATE_ACK);
|
|
284 // smList.sendToSlave(receivedCommand);
|
|
285 //}else{
|
|
286 // receivedCommand.setCMD(REP.SMCMD_UPDATE);
|
|
287 // smList.sendToMaster(receivedCommand);
|
|
288 //}
|
9
|
289 break;
|
38
|
290
|
9
|
291 case REP.SMCMD_UPDATE_ACK:
|
75
|
292 if(receivedCommand.sid > sessionlist.getList().size()){
|
73
|
293 editor = new Editor(channel);
|
75
|
294 editor.setName(receivedCommand.string);
|
73
|
295
|
|
296 session = new Session(editor);
|
|
297 session.addEditor(editor);
|
|
298
|
|
299 sessionlist.addSession(session);
|
|
300
|
83
|
301 gui.setComboSession(session.getSID(), session.getName());
|
73
|
302 }
|
75
|
303 smList.sendToSlave(receivedCommand);
|
1
|
304 break;
|
38
|
305
|
3
|
306 case REP.REPCMD_READ:
|
17
|
307 //sessionlist.sendCmd(channel, repCmd);
|
3
|
308 break;
|
38
|
309
|
0
|
310 default:
|
9
|
311 //sessionlist.sendCmd(channel, repCmd);
|
75
|
312 sessionlist.sendToNextEditor(channel, receivedCommand);
|
0
|
313 break;
|
|
314 }
|
|
315 }
|
83
|
316
|
79
|
317 private boolean setMaxHost(SocketChannel channel, String host) {
|
81
|
318 if(maxHost == null) {
|
|
319 maxHost = myHost;
|
|
320 sessionlist.setMaxHost(maxHost);
|
|
321 }
|
82
|
322 if(host.compareTo(maxHost) > 0){
|
79
|
323 //host > MaxHost なら maxHost = host
|
|
324 //masterを設定する。
|
|
325 maxHost = host;
|
|
326 sessionlist.setMaxHost(maxHost);
|
|
327 setMaster(false, channel);
|
|
328 return true;
|
|
329 }else{
|
|
330 return false;
|
|
331 }
|
78
|
332 }
|
|
333
|
76
|
334 private void setMyHostName(String localHostName) {
|
|
335 myHost = localHostName;
|
81
|
336 if(maxHost == null) {
|
|
337 maxHost = myHost;
|
|
338 sessionlist.setMaxHost(maxHost);
|
|
339 }
|
77
|
340 allEditorList.setHost(myHost);
|
76
|
341 }
|
0
|
342
|
77
|
343 private void setMaster(boolean b, SocketChannel channel) {
|
75
|
344 isMaster = b;
|
|
345 System.out.println("isMaster = " + b);
|
77
|
346 smList.setMaster(channel);
|
75
|
347 }
|
|
348
|
0
|
349 public static void main(String[] args) throws InterruptedException, IOException {
|
6
|
350 int port = 8766;
|
1
|
351
|
82
|
352 if(args.length > 0){
|
39
|
353 port = Integer.parseInt(args[0]);
|
0
|
354 }
|
|
355 SessionManager sm = new SessionManager(port);
|
2
|
356 sm.openSelector();
|
|
357 sm.openWindow();
|
0
|
358 sm.sessionManagerNet(port);
|
|
359 }
|
|
360
|
2
|
361 private void openWindow() {
|
83
|
362 Thread th = new Thread( gui );
|
2
|
363 th.start();
|
75
|
364 //System.out.println(sessionmanagerGUI.toString());
|
83
|
365 gui.addConnectionListener(this);
|
|
366 gui.addREPActionListener(this);
|
2
|
367 }
|
|
368
|
|
369 private void connectSession(String host) {
|
6
|
370 int port = 8766;
|
1
|
371 InetSocketAddress addr = new InetSocketAddress(host, port);
|
|
372 try {
|
6
|
373 SocketChannel sessionchannel = SocketChannel.open();
|
1
|
374 sessionchannel.configureBlocking(true);
|
|
375 sessionchannel.connect(addr);
|
6
|
376 while(!sessionchannel.finishConnect()){
|
77
|
377 System.out.print("test afro");
|
6
|
378 }
|
|
379 System.out.println("");
|
2
|
380 registerChannel(selector, sessionchannel, SelectionKey.OP_READ);
|
45
|
381
|
77
|
382 sm_join(sessionchannel);
|
45
|
383
|
1
|
384 }catch (IOException e) {
|
|
385 e.printStackTrace();
|
|
386 }
|
|
387 }
|
77
|
388
|
|
389 private void sm_join(SocketChannel channel){
|
79
|
390
|
|
391 //SM_JOINコマンドを生成。
|
77
|
392 REPCommand command = new REPCommand();
|
|
393 command.setCMD(REP.SMCMD_SM_JOIN);
|
79
|
394
|
|
395 //hostnameをセット。
|
82
|
396 setMyHostName(getLocalHostName(channel));
|
|
397
|
|
398 //XMLを生成。送信コマンドにセット。
|
77
|
399 SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist);
|
|
400 String string = encoder.sessionListToXML();
|
|
401 command.setString(string);
|
|
402
|
79
|
403 //SM_JOINコマンドを送信。
|
77
|
404 REPPacketSend send = new REPPacketSend(channel);
|
|
405 send.send(command);
|
|
406
|
79
|
407 //SessionManagerのListに追加。
|
77
|
408 smList.add(channel);
|
|
409 }
|
2
|
410
|
75
|
411 private String getLocalHostName(SocketChannel channel) {
|
74
|
412 String host = null;
|
|
413 host = channel.socket().getLocalAddress().getHostName();
|
|
414 return host;
|
|
415 }
|
|
416
|
77
|
417 // private String getSocketString(SocketChannel sessionchannel) {
|
|
418 // SocketAddress socket = sessionchannel.socket().getRemoteSocketAddress();
|
|
419 // //String inetAddressString = sessionchannel.socket().getInetAddress().toString();
|
|
420 // StringTokenizer stn = new StringTokenizer(socket.toString(), "/");
|
|
421 // String socketString = null;
|
|
422 // while(stn.hasMoreTokens()){
|
|
423 // socketString = stn.nextToken();
|
|
424 // //System.out.println(socketString);
|
|
425 // }
|
|
426 // return socketString;
|
|
427 // }
|
14
|
428
|
2
|
429 public void connectionOccured(ConnectionEvent event) {
|
|
430 connectSession(event.getHost());
|
|
431 }
|
8
|
432
|
|
433 public void ActionOccured(REPActionEvent event) {
|
|
434 System.out.println("Action!");
|
|
435 SocketChannel editorChannel = event.getEditorChannel();
|
|
436 int sid = event.getSID();
|
84
|
437 //int eid = 0;
|
70
|
438 //int eid = event.getEID();
|
|
439 //sessionlist.addEditor(editorChannel, sid, eid);
|
|
440 Editor editor = new Editor(editorChannel);
|
|
441 Session session = sessionlist.getSession(sid);
|
|
442 session.addEditor(editor);
|
|
443
|
72
|
444 Editor master = session.getMaster();
|
|
445
|
|
446 REPCommand command = new REPCommand();
|
|
447 command.setCMD(REP.SMCMD_SELECT);
|
|
448 command.setSID(sid);
|
84
|
449 command.setString(editor.getHost() + ":" + editor.getPort());
|
72
|
450
|
|
451 master.send(command);
|
|
452
|
84
|
453 //REPPacketSend send = new REPPacketSend(editorChannel);
|
|
454 //send.send(new REPCommand(REP.SMCMD_SELECT_ACK, sid, eid, 0,0,0,""));
|
72
|
455
|
|
456
|
|
457
|
71
|
458 //sessionlist.sendSelect(sid);
|
8
|
459 }
|
0
|
460 }
|