0
|
1 package rep;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.InetSocketAddress;
|
11
|
5 import java.net.SocketAddress;
|
111
|
6 import java.nio.ByteBuffer;
|
|
7 import java.nio.CharBuffer;
|
2
|
8 import java.nio.channels.SelectableChannel;
|
0
|
9 import java.nio.channels.SelectionKey;
|
|
10 import java.nio.channels.Selector;
|
|
11 import java.nio.channels.ServerSocketChannel;
|
|
12 import java.nio.channels.SocketChannel;
|
111
|
13 import java.nio.charset.CharacterCodingException;
|
|
14 import java.nio.charset.Charset;
|
|
15 import java.nio.charset.CharsetEncoder;
|
83
|
16 import java.util.LinkedList;
|
144
|
17 import java.util.List;
|
|
18 import java.util.Set;
|
15
|
19 import java.util.StringTokenizer;
|
0
|
20
|
123
|
21 import rep.channel.REPServerSocketChannel;
|
133
|
22 import rep.channel.REPSocketChannel;
|
146
|
23 import rep.channel.SelectionKeySimulator;
|
|
24 import rep.channel.SelectorSimulator;
|
144
|
25 import rep.handler.PacketSet;
|
146
|
26 import rep.handler.REPHandler;
|
148
|
27 import rep.handler.REPHandlerImpl;
|
123
|
28 import rep.simulator.REPSelector;
|
56
|
29 import rep.xml.SessionXMLDecoder;
|
45
|
30 import rep.xml.SessionXMLEncoder;
|
|
31
|
1
|
32 //+-------+--------+--------+-------+--------+---------+------+
|
|
33 //| cmd | session| editor | seqid | lineno | textsiz | text |
|
|
34 //| | id | id | | | | |
|
|
35 //+-------+--------+--------+-------+--------+---------+------+
|
|
36 //o-------header section (network order)-------------o
|
|
37 /*int cmd; // command
|
101
|
38 int sid; // session ID : uniqu to editing file
|
123
|
39 int eid; // editor ID : owner editor ID = 1。Session に対して unique
|
122
|
40 int seqno; // Sequence number : sequence number はエディタごとに管理
|
1
|
41 int lineno; // line number
|
101
|
42 int textsize; // textsize : bytesize
|
1
|
43 byte[] text;*/
|
|
44
|
8
|
45 public class SessionManager implements ConnectionListener, REPActionListener{
|
0
|
46
|
|
47
|
|
48 private SessionList sessionlist;
|
144
|
49 private List<Session> sessionList;
|
83
|
50 private SessionManagerGUI gui;
|
2
|
51 private Selector selector;
|
7
|
52 private SessionManagerList smList;
|
17
|
53 private String myHost;
|
21
|
54 private boolean isMaster = true;
|
122
|
55 private EditorList ownEditorList;
|
144
|
56 private List<Editor> editorList;
|
78
|
57 private String maxHost;
|
148
|
58 private boolean isSimulation;
|
144
|
59 //private Set<SelectionKey> sessionList;
|
95
|
60 private static int temp_port;
|
|
61 private static int send_port;
|
101
|
62
|
|
63 static final int DEFAULT_PORT = 8766;
|
|
64
|
2
|
65 public SessionManager(int port) {
|
83
|
66 gui = new SessionManagerGUI();
|
2
|
67 }
|
|
68
|
|
69 public void openSelector() throws IOException{
|
123
|
70 selector = REPSelector.open();
|
2
|
71 }
|
0
|
72
|
144
|
73 public void mainLoop(int port) throws InterruptedException, IOException {
|
2
|
74
|
148
|
75 REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open();
|
123
|
76
|
122
|
77 ssc.configureBlocking(false); //reuse address 必須
|
101
|
78
|
|
79 ssc.socket().setReuseAddress(true);
|
|
80
|
0
|
81 ssc.socket().bind(new InetSocketAddress(port));
|
|
82 ssc.register(selector, SelectionKey.OP_ACCEPT);
|
6
|
83
|
|
84
|
0
|
85 sessionlist = new SessionList();
|
144
|
86 sessionList = new LinkedList<Session>();
|
7
|
87 smList = new SessionManagerList();
|
122
|
88 ownEditorList = new EditorList();
|
144
|
89 editorList = new LinkedList<Editor>();
|
0
|
90
|
|
91 while(true){
|
|
92 selector.select();
|
144
|
93 select();
|
|
94 }
|
|
95 }
|
|
96
|
|
97 private void select() throws IOException {
|
148
|
98 for(SelectionKey key : selector.selectedKeys()){
|
144
|
99 if(key.isAcceptable()){
|
|
100 /*** serverChannelはenableになったSelectionKeyのchannel ***/
|
146
|
101 REPServerSocketChannel serverChannel = (REPServerSocketChannel)key.channel();
|
|
102 REPSocketChannel channel = serverChannel.accept(); //keyからchannelを取って、accept
|
144
|
103 registerChannel (selector, channel, SelectionKey.OP_READ);
|
|
104 channel = null;
|
123
|
105
|
144
|
106 }else if(key.isReadable()){
|
|
107
|
146
|
108 // REPSocketChannel<REPCommand> channel = (REPSocketChannel<REPCommand>)key.channel();
|
|
109 // REPPacketReceive receive = new REPPacketReceive(channel);
|
|
110 // receive.setkey(key);
|
|
111 // REPCommand receivedCommand = receive.unpackUConv();
|
|
112 // manage(channel, receivedCommand);
|
144
|
113
|
146
|
114 REPHandler handler = (REPHandler)key.attachment();
|
|
115 handler.handle(key);
|
144
|
116
|
|
117 }else if(key.isConnectable()){
|
|
118 System.out.println("Connectable");
|
0
|
119 }
|
|
120 }
|
|
121 }
|
1
|
122
|
2
|
123 private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
|
|
124 if(channel == null) {
|
|
125 return;
|
|
126 }
|
|
127 channel.configureBlocking(false);
|
6
|
128 selector.wakeup();
|
148
|
129 REPHandler handler = new REPHandlerImpl(this);
|
|
130 //channel.register(selector, ops);
|
|
131 channel.register(selector, ops, handler);
|
2
|
132 }
|
|
133
|
144
|
134 public void manage(REPSocketChannel<REPCommand> channel, REPCommand receivedCommand) {
|
75
|
135 if(receivedCommand == null) return;
|
69
|
136 Session session;
|
141
|
137 REPCommand sendCommand = new REPCommand(receivedCommand);
|
78
|
138 REPPacketSend send = new REPPacketSend(channel);
|
144
|
139
|
75
|
140 switch(receivedCommand.cmd){
|
144
|
141
|
0
|
142 case REP.SMCMD_JOIN:
|
83
|
143
|
148
|
144 if(isSimulation){
|
|
145 editorList.add(new Editor(editorList.size(), channel));
|
|
146
|
|
147 }else{
|
|
148 Editor editor = new Editor(channel);
|
|
149 editor.setHost(myHost);
|
|
150 int tempeid = ownEditorList.addEditor(editor);
|
|
151 gui.setComboEditor(tempeid, channel);
|
|
152 }
|
|
153
|
144
|
154
|
38
|
155 break;
|
144
|
156
|
1
|
157 case REP.SMCMD_JOIN_ACK:
|
144
|
158
|
1
|
159 break;
|
144
|
160
|
0
|
161 case REP.SMCMD_PUT:
|
148
|
162
|
|
163 if(isSimulation){
|
|
164 //エディタのリストに追加
|
|
165 editorList.add(new Editor(editorList.size(), channel));
|
|
166 //Sessionを生成
|
|
167 int sid = sessionList.size();
|
|
168 sessionList.add(new Session(sid, new Editor(0, channel)));
|
144
|
169
|
148
|
170 }else{
|
|
171 Editor editor = new Editor(channel);
|
|
172 editor.setHost(myHost);
|
|
173 ownEditorList.addEditor(editor);
|
|
174 editor.setEID(1);
|
|
175 editor.setName(receivedCommand.string);
|
|
176 session = new Session(editor);
|
|
177 session.setOwner(true);
|
|
178 session.addEditor(editor);
|
|
179 sessionlist.addSession(session);
|
|
180 gui.setComboSession(session.getSID(), session.getName());
|
|
181 gui.setComboEditor(editor.getEID(), editor.getChannel());
|
|
182 session.addToRoutingTable(editor);
|
|
183 sendCommand.setCMD(REP.SMCMD_PUT_ACK);
|
|
184 sendCommand.setEID(1);
|
|
185 sendCommand.setSID(session.getSID());
|
|
186 editor.send(sendCommand);
|
144
|
187
|
148
|
188 SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
|
|
189 REPCommand command = new REPCommand();
|
|
190 command.setSID(session.getSID());
|
|
191 command.setString(sessionEncoder.sessionListToXML());
|
144
|
192
|
148
|
193 command.setCMD(REP.SMCMD_UPDATE);
|
|
194 smList.sendExcept(channel, command);
|
|
195 }
|
144
|
196
|
148
|
197
|
144
|
198
|
38
|
199 break;
|
133
|
200
|
0
|
201 case REP.SMCMD_SELECT:
|
94
|
202
|
148
|
203 if(isSimulation){
|
|
204 Editor editor = getEditor(channel);
|
|
205 sessionList.get(receivedCommand.sid).addEditor(editor);
|
|
206
|
|
207 }else{
|
|
208 Editor editor = new Editor(channel);
|
|
209 session = sessionlist.getSession(receivedCommand.sid);
|
|
210 if(session.isOwner()){
|
|
211 session.addEditor(editor);
|
|
212 sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
|
|
213 sendCommand.setEID(editor.getEID());
|
|
214 send.send(sendCommand);
|
|
215 }else {
|
|
216 Editor master = session.getMaster();
|
|
217 master.send(receivedCommand);
|
|
218 session.addEditor(editor);
|
|
219 }
|
|
220
|
|
221 }
|
144
|
222
|
8
|
223 break;
|
144
|
224
|
8
|
225 case REP.SMCMD_SELECT_ACK:
|
144
|
226
|
85
|
227 String hostport = receivedCommand.string;
|
122
|
228 Editor editor2 = ownEditorList.getEditor(hostport);
|
85
|
229 if(editor2 != null) {
|
|
230 REPCommand command2 = new REPCommand();
|
|
231 command2.setCMD(REP.SMCMD_JOIN_ACK);
|
93
|
232 command2.setSID(receivedCommand.sid);
|
85
|
233 command2.setEID(receivedCommand.eid);
|
|
234 editor2.send(command2);
|
|
235 }else{
|
|
236 smList.sendExcept(channel, receivedCommand);
|
|
237 }
|
144
|
238
|
1
|
239 break;
|
144
|
240
|
8
|
241 case REP.SMCMD_SM_JOIN:
|
144
|
242
|
122
|
243 //SessionManagerのリストへ追加
|
83
|
244 smList.add(channel);
|
144
|
245
|
122
|
246 //XMLからSessionListオブジェクトを生成する。
|
77
|
247 SessionXMLDecoder decoder = new SessionXMLDecoder();
|
79
|
248 SessionList receivedSessionList = decoder.decode(receivedCommand.string);
|
144
|
249
|
122
|
250 //SessionListへ追加し変換テーブルを生成する。
|
83
|
251 sessionlist.update(channel, receivedSessionList);
|
144
|
252
|
122
|
253 //myHost を設定。
|
76
|
254 if(myHost == null) setMyHostName(getLocalHostName(channel));
|
144
|
255
|
122
|
256 //maxHost を設定。
|
95
|
257 if(setMaxHost(channel, receivedSessionList.getMaxHost())){
|
|
258 sendCommand = new REPCommand();
|
|
259 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
|
|
260 sendCommand.setString(maxHost);
|
|
261 smList.sendExcept(channel, sendCommand);
|
|
262 }
|
144
|
263
|
122
|
264 //SessionListからXMLを生成。
|
|
265 //joinしてきたSessionManagerに対してACKを送信。
|
78
|
266 SessionXMLEncoder sessionlistEncoder = new SessionXMLEncoder(sessionlist);
|
|
267 sendCommand = new REPCommand();
|
|
268 sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
|
|
269 sendCommand.setString(sessionlistEncoder.sessionListToXML());
|
|
270 send.send(sendCommand);
|
144
|
271
|
122
|
272 //その他の SessionManager に対して SMCMD_UPDATEを 送信。
|
78
|
273 sendCommand = new REPCommand();
|
83
|
274 sendCommand.setCMD(REP.SMCMD_UPDATE);
|
78
|
275 sendCommand.setString(receivedCommand.string);
|
|
276 smList.sendExcept(channel, sendCommand);
|
144
|
277
|
31
|
278 if(isMaster){
|
45
|
279 }else {
|
31
|
280 }
|
144
|
281
|
8
|
282 break;
|
144
|
283
|
8
|
284 case REP.SMCMD_SM_JOIN_ACK:
|
144
|
285
|
122
|
286 //XMLからSessionListオブジェクトを生成。
|
82
|
287 SessionXMLDecoder decoder2 = new SessionXMLDecoder();
|
|
288 SessionList receivedSessionList2 = decoder2.decode(receivedCommand.string);
|
144
|
289
|
122
|
290 //maxHostを決定。
|
95
|
291 if(setMaxHost(channel, receivedSessionList2.getMaxHost())){
|
|
292 sendCommand = new REPCommand();
|
|
293 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
|
|
294 sendCommand.setString(maxHost);
|
|
295 smList.sendExcept(channel, sendCommand);
|
|
296 }
|
144
|
297
|
38
|
298 if(isMaster){
|
39
|
299 }else{
|
38
|
300 }
|
144
|
301
|
6
|
302 break;
|
144
|
303
|
8
|
304 case REP.SMCMD_UPDATE:
|
144
|
305
|
99
|
306 SessionXMLDecoder decoder3 = new SessionXMLDecoder();
|
|
307 SessionList receivedSessionList3 = decoder3.decode(receivedCommand.string);
|
144
|
308
|
122
|
309 //SessionListへ追加し変換テーブルを生成する。
|
99
|
310 sessionlist.update(channel, receivedSessionList3);
|
144
|
311
|
99
|
312 smList.sendExcept(channel, receivedCommand);
|
144
|
313
|
100
|
314 for(Session session3 : receivedSessionList3.getList()){
|
|
315 gui.setComboSession(session3.getSID(), session3.getName());
|
|
316 }
|
144
|
317
|
9
|
318 break;
|
144
|
319
|
9
|
320 case REP.SMCMD_UPDATE_ACK:
|
75
|
321 if(receivedCommand.sid > sessionlist.getList().size()){
|
148
|
322 Editor editor = new Editor(channel);
|
75
|
323 editor.setName(receivedCommand.string);
|
144
|
324
|
73
|
325 session = new Session(editor);
|
|
326 session.addEditor(editor);
|
144
|
327
|
73
|
328 sessionlist.addSession(session);
|
144
|
329
|
83
|
330 gui.setComboSession(session.getSID(), session.getName());
|
73
|
331 }
|
75
|
332 smList.sendToSlave(receivedCommand);
|
1
|
333 break;
|
144
|
334
|
|
335 // case REP.REPCMD_READ:
|
108
|
336 // //sessionlist.sendCmd(channel, repCmd);
|
|
337 // break;
|
144
|
338
|
95
|
339 case REP.SMCMD_CH_MASTER:
|
122
|
340 //maxHost を設定。
|
95
|
341 if(setMaxHost(channel, receivedCommand.string)){
|
|
342 sendCommand = new REPCommand();
|
|
343 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
|
|
344 sendCommand.setString(maxHost);
|
|
345 smList.sendExcept(channel, sendCommand);
|
|
346 }
|
|
347 break;
|
144
|
348
|
0
|
349 default:
|
144
|
350 //sid から Session を取得
|
|
351 session = getSession(receivedCommand.sid);
|
|
352 //マージの処理と次のエディタへコマンドを送信する処理
|
|
353 session.translate(channel, receivedCommand);
|
|
354 break;
|
|
355 }
|
|
356 }
|
|
357
|
|
358 private Editor getEditor(REPSocketChannel<REPCommand> channel) {
|
|
359 // TODO Auto-generated method stub
|
|
360 for(Editor editor : editorList){
|
|
361 if(editor.getChannel() == channel){
|
|
362 return editor;
|
122
|
363 }
|
0
|
364 }
|
144
|
365 return null;
|
|
366 }
|
|
367
|
|
368 private Session getSession(int sid) {
|
|
369 for(Session session : sessionList){
|
|
370 if(session.getSID() == sid) return session;
|
|
371 }
|
|
372 return null;
|
0
|
373 }
|
83
|
374
|
139
|
375 private boolean setMaxHost(REPSocketChannel channel, String maxHost2) {
|
|
376 // TODO Auto-generated method stub
|
|
377 return false;
|
|
378 }
|
|
379
|
122
|
380 private int reverseCmd(int cmd) {
|
|
381 int kindOfCmd = 0;
|
|
382 switch(cmd){
|
|
383 case REP.REPCMD_INSERT:
|
|
384 kindOfCmd = REP.REPCMD_DELETE;
|
|
385 break;
|
|
386 case REP.REPCMD_DELETE:
|
|
387 kindOfCmd = REP.REPCMD_INSERT;
|
|
388 break;
|
|
389 case REP.REPCMD_REPLACE:
|
|
390 kindOfCmd = REP.REPCMD_REPLACE;
|
|
391 break;
|
|
392 }
|
|
393 return kindOfCmd;
|
|
394 }
|
|
395
|
111
|
396
|
78
|
397
|
76
|
398 private void setMyHostName(String localHostName) {
|
95
|
399 myHost = localHostName + temp_port;
|
81
|
400 if(maxHost == null) {
|
|
401 maxHost = myHost;
|
|
402 sessionlist.setMaxHost(maxHost);
|
|
403 }
|
122
|
404 ownEditorList.setHost(myHost);
|
76
|
405 }
|
0
|
406
|
139
|
407 private void setMaster(boolean b, REPSocketChannel channel) {
|
75
|
408 isMaster = b;
|
|
409 System.out.println("isMaster = " + b);
|
77
|
410 smList.setMaster(channel);
|
75
|
411 }
|
|
412
|
0
|
413 public static void main(String[] args) throws InterruptedException, IOException {
|
101
|
414 int port = DEFAULT_PORT;
|
|
415 int port_s = DEFAULT_PORT;
|
113
|
416 //System.setProperty("file.encoding", "UTF-8");
|
82
|
417 if(args.length > 0){
|
39
|
418 port = Integer.parseInt(args[0]);
|
95
|
419 port_s = Integer.parseInt(args[1]);
|
0
|
420 }
|
95
|
421 temp_port = port;
|
|
422 send_port = port_s;
|
0
|
423 SessionManager sm = new SessionManager(port);
|
2
|
424 sm.openSelector();
|
|
425 sm.openWindow();
|
144
|
426 sm.mainLoop(port);
|
0
|
427 }
|
|
428
|
2
|
429 private void openWindow() {
|
83
|
430 Thread th = new Thread( gui );
|
2
|
431 th.start();
|
75
|
432 //System.out.println(sessionmanagerGUI.toString());
|
83
|
433 gui.addConnectionListener(this);
|
|
434 gui.addREPActionListener(this);
|
2
|
435 }
|
|
436
|
|
437 private void connectSession(String host) {
|
101
|
438 int port = DEFAULT_PORT;
|
95
|
439 port = send_port;
|
1
|
440 InetSocketAddress addr = new InetSocketAddress(host, port);
|
|
441 try {
|
148
|
442 REPSocketChannel sessionchannel = REPSocketChannel.<REPCommand>create();
|
1
|
443 sessionchannel.configureBlocking(true);
|
|
444 sessionchannel.connect(addr);
|
6
|
445 while(!sessionchannel.finishConnect()){
|
77
|
446 System.out.print("test afro");
|
6
|
447 }
|
|
448 System.out.println("");
|
2
|
449 registerChannel(selector, sessionchannel, SelectionKey.OP_READ);
|
45
|
450
|
77
|
451 sm_join(sessionchannel);
|
45
|
452
|
1
|
453 }catch (IOException e) {
|
|
454 e.printStackTrace();
|
|
455 }
|
|
456 }
|
77
|
457
|
139
|
458 private void sm_join(REPSocketChannel channel){
|
79
|
459
|
122
|
460 //SM_JOINコマンドを生成。
|
77
|
461 REPCommand command = new REPCommand();
|
|
462 command.setCMD(REP.SMCMD_SM_JOIN);
|
79
|
463
|
122
|
464 //hostnameをセット。
|
82
|
465 setMyHostName(getLocalHostName(channel));
|
|
466
|
122
|
467 //XMLを生成。送信コマンドにセット。
|
77
|
468 SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist);
|
|
469 String string = encoder.sessionListToXML();
|
|
470 command.setString(string);
|
|
471
|
122
|
472 //SM_JOINコマンドを送信。
|
77
|
473 REPPacketSend send = new REPPacketSend(channel);
|
|
474 send.send(command);
|
|
475
|
122
|
476 //SessionManagerのListに追加。
|
77
|
477 smList.add(channel);
|
|
478 }
|
2
|
479
|
139
|
480 private String getLocalHostName(REPSocketChannel channel) {
|
74
|
481 String host = null;
|
|
482 host = channel.socket().getLocalAddress().getHostName();
|
|
483 return host;
|
|
484 }
|
|
485
|
2
|
486 public void connectionOccured(ConnectionEvent event) {
|
|
487 connectSession(event.getHost());
|
|
488 }
|
8
|
489
|
|
490 public void ActionOccured(REPActionEvent event) {
|
104
|
491
|
133
|
492 REPSocketChannel channel = event.getEditorChannel();
|
107
|
493 int sid = event.getSID();
|
|
494 Session session = sessionlist.getSession(sid);
|
|
495 if(session.isOwner()){
|
148
|
496 Editor editor = new Editor(channel);
|
|
497 session.addEditor(new Editor(channel));
|
107
|
498 REPCommand sendCommand = new REPCommand();
|
|
499 sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
|
148
|
500 sendCommand.setEID(editor.getEID());
|
107
|
501 sendCommand.setSID(sid);
|
|
502 REPPacketSend sender = new REPPacketSend(channel);
|
|
503 sender.send(sendCommand);
|
|
504 }else {
|
133
|
505 REPSocketChannel editorChannel = event.getEditorChannel();
|
107
|
506 sid = event.getSID();
|
|
507 Editor editor = new Editor(editorChannel);
|
|
508 editor.setHost(myHost);
|
|
509 session = sessionlist.getSession(sid);
|
|
510 session.addEditor(editor);
|
|
511
|
|
512 Editor owner = session.getMaster();
|
|
513
|
|
514 REPCommand command = new REPCommand();
|
|
515 command.setCMD(REP.SMCMD_SELECT);
|
|
516 command.setSID(sid);
|
|
517 command.setString(editor.getHost() + ":" + editor.getPort());
|
|
518 owner.send(command);
|
|
519 }
|
72
|
520
|
|
521
|
8
|
522 }
|
122
|
523
|
144
|
524 public void addWaitingCommand(PacketSet set) {
|
|
525 // TODO Auto-generated method stub
|
|
526
|
|
527 }
|
148
|
528
|
|
529 public void undo() {
|
|
530 // TODO Auto-generated method stub
|
|
531
|
|
532 }
|
0
|
533 }
|