266
|
1
|
0
|
2 package rep;
|
|
3
|
|
4 import java.io.IOException;
|
|
5 import java.net.InetSocketAddress;
|
267
|
6 import java.nio.channels.ClosedChannelException;
|
0
|
7 import java.nio.channels.SelectionKey;
|
178
|
8 import java.util.Iterator;
|
83
|
9 import java.util.LinkedList;
|
144
|
10 import java.util.List;
|
231
|
11 import java.util.Set;
|
178
|
12 import java.util.concurrent.BlockingQueue;
|
192
|
13 import java.util.concurrent.LinkedBlockingQueue;
|
0
|
14
|
198
|
15
|
123
|
16 import rep.channel.REPServerSocketChannel;
|
133
|
17 import rep.channel.REPSocketChannel;
|
144
|
18 import rep.handler.PacketSet;
|
146
|
19 import rep.handler.REPHandler;
|
316
|
20 import rep.handler.REPEditorHandler;
|
|
21 import rep.handler.REPSessionManagerHandler;
|
158
|
22 import rep.channel.REPSelector;
|
56
|
23 import rep.xml.SessionXMLDecoder;
|
45
|
24 import rep.xml.SessionXMLEncoder;
|
198
|
25 import rep.channel.REPSelectionKey;
|
264
|
26
|
198
|
27 /*
|
264
|
28 +-------+--------+--------+-------+--------+---------+------+
|
|
29 | cmd | session| editor | seqid | lineno | textsiz | text |
|
|
30 | | id | id | | | | |
|
|
31 +-------+--------+--------+-------+--------+---------+------+
|
|
32 o---------- header section (network order) ----------o
|
|
33
|
|
34 int cmd; kind of command
|
|
35 int sid; session ID : uniqu to editing file
|
|
36 int eid; editor ID : owner editor ID = 1。Session に対して unique
|
308
|
37 -1 session manager command
|
|
38 -2 merge command
|
264
|
39 int seqno; Sequence number : sequence number はエディタごとに管理
|
|
40 int lineno; line number
|
|
41 int textsize; textsize : bytesize
|
|
42 byte[] text;
|
198
|
43 */
|
1
|
44
|
250
|
45 public class SessionManager implements SessionManagerEventListener{
|
0
|
46
|
164
|
47 private LinkedList<Session> sessionList;
|
280
|
48 private SessionManagerGUI gui;
|
198
|
49 private REPSelector<REPCommand> selector;
|
7
|
50 private SessionManagerList smList;
|
144
|
51 private List<Editor> editorList;
|
304
|
52 // editorList は、sessionList に入っているeditorとは別なeditorのlistらしい。
|
78
|
53 private String maxHost;
|
212
|
54 private List<PacketSet> waitingCommandInMerge;
|
316
|
55 REPHandler normalHandler = new REPEditorHandler(this);
|
308
|
56 private BlockingQueue<SessionManagerEvent> waitingEventQueue = new LinkedBlockingQueue<SessionManagerEvent>();;
|
|
57 private String myHost;
|
317
|
58 private LinkedList<PacketSet> writeQueue = new LinkedList<PacketSet>();
|
308
|
59 private static int receive_port;
|
|
60 private static int parent_port;
|
101
|
61 static final int DEFAULT_PORT = 8766;
|
316
|
62
|
|
63 public static void main(String[] args) throws InterruptedException, IOException {
|
|
64
|
|
65 int port = DEFAULT_PORT;
|
|
66 int port_s = DEFAULT_PORT;
|
|
67 //System.setProperty("file.encoding", "UTF-8");
|
|
68 if(args.length > 0){
|
|
69 port = Integer.parseInt(args[0]);
|
|
70 port_s = Integer.parseInt(args[1]);
|
|
71 }
|
|
72 receive_port = port;
|
|
73 parent_port = port_s;
|
|
74 SessionManager sm = new SessionManager();
|
|
75 sm.init(port,new SessionManagerGUIimpl(sm));
|
|
76
|
|
77
|
|
78 }
|
|
79
|
2
|
80
|
|
81 public void openSelector() throws IOException{
|
231
|
82 selector = REPSelector.<REPCommand>create();
|
2
|
83 }
|
280
|
84
|
|
85 public void init(int port, SessionManagerGUI gui) throws IOException, InterruptedException {
|
|
86 this.gui = gui;
|
|
87 openSelector();
|
|
88 init(port);
|
|
89 mainLoop();
|
|
90 }
|
|
91
|
|
92
|
|
93 private void init(int port) throws InterruptedException, IOException {
|
2
|
94
|
186
|
95 REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open(new REPCommandPacker());
|
122
|
96 ssc.configureBlocking(false); //reuse address 必須
|
101
|
97 ssc.socket().setReuseAddress(true);
|
212
|
98 //getAllByNameで取れた全てのアドレスに対してbindする
|
|
99 ssc.socket().bind(new InetSocketAddress(port));
|
308
|
100 ssc.register(selector, SelectionKey.OP_ACCEPT, normalHandler);
|
6
|
101
|
144
|
102 sessionList = new LinkedList<Session>();
|
7
|
103 smList = new SessionManagerList();
|
144
|
104 editorList = new LinkedList<Editor>();
|
212
|
105 waitingCommandInMerge = new LinkedList<PacketSet>();
|
228
|
106
|
215
|
107
|
155
|
108 }
|
313
|
109
|
|
110 /*
|
|
111 * We wrote everything in one thread, but we can assign
|
|
112 * one thread for each communication channel and GUI event.
|
|
113 */
|
155
|
114
|
231
|
115 public void mainLoop() throws IOException {
|
0
|
116 while(true){
|
313
|
117 if (checkInputEvent() ||
|
|
118 checkWaitingWrite() ||
|
|
119 checkWaitingCommandInMerge()) {
|
|
120 // try to do fair execution for waiting task
|
|
121 if(selector.selectNow() > 0) select();
|
|
122 continue;
|
300
|
123 }
|
313
|
124 // now we can wait for input packet or event
|
233
|
125 selector.select();
|
144
|
126 select();
|
|
127 }
|
|
128 }
|
|
129
|
313
|
130 private boolean checkInputEvent() {
|
|
131 SessionManagerEvent e;
|
|
132 if((e = waitingEventQueue.poll())!=null){
|
|
133 e.exec();
|
|
134 return true;
|
|
135 }
|
|
136 return false;
|
|
137 }
|
|
138
|
|
139 private boolean checkWaitingWrite() throws IOException {
|
317
|
140 PacketSet p = writeQueue.poll();
|
|
141 if (p!=null) {
|
|
142 REPCommand cmd = new REPCommand(p.command);
|
|
143 p.channel.write(cmd);
|
|
144 return true;
|
313
|
145 }
|
|
146 return false;
|
|
147 }
|
|
148
|
308
|
149 /**
|
|
150 * Check waiting command in merge
|
|
151 * @return true if there is a processed waiting command
|
|
152 * @throws IOException
|
|
153 */
|
|
154 private boolean checkWaitingCommandInMerge() throws IOException {
|
212
|
155 for(Iterator<PacketSet> it = waitingCommandInMerge.iterator(); it.hasNext();){
|
178
|
156 PacketSet p = it.next();
|
308
|
157 if(p.getEditor().isMerging()) { // still merging do nothing
|
178
|
158 continue;
|
|
159 }else{
|
308
|
160 // process one command and return true
|
178
|
161 manage(p.channel, p.command);
|
|
162 it.remove();
|
212
|
163 return true;
|
178
|
164 }
|
|
165 }
|
|
166 return false;
|
|
167 }
|
|
168
|
144
|
169 private void select() throws IOException {
|
231
|
170
|
|
171 Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1();
|
|
172 for(REPSelectionKey<REPCommand> key : keys){
|
144
|
173 if(key.isAcceptable()){
|
199
|
174 REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker());
|
300
|
175 System.out.println("SessionManager.select() : key.isAcceptable : channel = " + channel);
|
229
|
176 registerChannel (channel, SelectionKey.OP_READ);
|
144
|
177 channel = null;
|
123
|
178
|
144
|
179 }else if(key.isReadable()){
|
212
|
180 REPHandler handler = (REPHandler)(key.attachment());
|
267
|
181 try {
|
|
182 handler.handle(key);
|
|
183 } catch (ClosedChannelException x) {
|
|
184 key.cancel();
|
308
|
185 handler.cancel(key.channel1());
|
267
|
186 } catch (IOException x) {
|
|
187 key.cancel();
|
308
|
188 handler.cancel( key.channel1());
|
267
|
189 }
|
0
|
190 }
|
|
191 }
|
|
192 }
|
1
|
193
|
229
|
194 private void registerChannel(REPSocketChannel<REPCommand> channel, int ops) throws IOException {
|
2
|
195 if(channel == null) {
|
|
196 return;
|
|
197 }
|
|
198 channel.configureBlocking(false);
|
308
|
199 REPHandler handler = normalHandler;
|
148
|
200 channel.register(selector, ops, handler);
|
2
|
201 }
|
|
202
|
287
|
203 public void manage(REPSocketChannel<REPCommand> channel, REPCommand receivedCommand) throws IOException {
|
75
|
204 if(receivedCommand == null) return;
|
158
|
205 //Session session;
|
178
|
206 REPSocketChannel<REPCommand> send = channel;
|
144
|
207
|
75
|
208 switch(receivedCommand.cmd){
|
144
|
209
|
317
|
210 // Editor Command
|
|
211
|
316
|
212 case REPCMD_DELETE:
|
|
213 case REPCMD_INSERT:
|
|
214 case REPCMD_NOP:
|
|
215 {
|
|
216 // sid から Session を取得
|
|
217 Session session = getSession(receivedCommand.sid);
|
|
218 if (session==null) throw new IOException();
|
|
219 // 次のエディタへコマンドを送信する処理
|
|
220 Editor editor = session.getEditor(channel);
|
|
221 editor.translate(session.getNextEditor(editor), receivedCommand);
|
|
222 break;
|
|
223 }
|
317
|
224
|
271
|
225 case SMCMD_JOIN:
|
164
|
226 {
|
|
227 //どのSessionにも属さないエディタをリストに追加
|
317
|
228 //エディタとchannelは1対1 (ではない)
|
212
|
229 //エディタが新しくputする場合は新しくソケットを作る
|
317
|
230 // ここのeditorList はsessionのとは別物
|
|
231 Editor editor = new Editor(this,editorList.size(),channel);
|
164
|
232 editor.setHost(myHost);
|
|
233 editorList.add(editor);
|
144
|
234
|
259
|
235 updateGUI();
|
199
|
236
|
164
|
237 }
|
|
238
|
|
239 break;
|
144
|
240
|
271
|
241 case SMCMD_JOIN_ACK:
|
212
|
242 assert (false);
|
1
|
243 break;
|
144
|
244
|
271
|
245 case SMCMD_PUT:
|
164
|
246 {
|
|
247 //エディタのリストに追加
|
317
|
248 Editor editor = new Editor(this,editorList.size(), channel);
|
227
|
249 //editorList.add(editor);
|
164
|
250
|
|
251 //Sessionを生成
|
|
252 int sid = sessionList.size();
|
317
|
253 editor = new Editor(this,0, channel);
|
164
|
254 editor.setHost(myHost);
|
227
|
255 Session session = new Session(sid, receivedCommand.string, editor);
|
164
|
256 session.hasOwner(true);
|
227
|
257 sessionList.add(session);
|
164
|
258
|
259
|
259 updateGUI();
|
158
|
260
|
164
|
261 //エディタにAckを送信
|
316
|
262 REPCommand sendCommand = new REPCommand(receivedCommand);
|
164
|
263 sendCommand.setCMD(REP.SMCMD_PUT_ACK);
|
|
264 sendCommand.setEID(editor.getEID());
|
|
265 sendCommand.setSID(session.getSID());
|
|
266 editor.send(sendCommand);
|
144
|
267
|
164
|
268 //他のSessionManagerへSessionの追加を報告
|
212
|
269 //親に送って、親から子へ
|
164
|
270 SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
|
|
271 REPCommand command = new REPCommand();
|
|
272 command.setSID(session.getSID());
|
|
273 command.setString(sessionEncoder.sessionListToXML());
|
|
274 command.setCMD(REP.SMCMD_UPDATE);
|
|
275 smList.sendExcept(channel, command);
|
|
276
|
|
277 }
|
|
278
|
|
279 break;
|
133
|
280
|
317
|
281 // SELECT is no longer used in a editor. Select
|
|
282 // operation is handled in Session Manager Only
|
271
|
283 case SMCMD_SELECT:
|
164
|
284 {
|
178
|
285 //他のSessionManagerをエディタとしてSessionに追加
|
317
|
286 Editor editor = new Editor(this,0,channel);
|
164
|
287 Session session = getSession(receivedCommand.sid);
|
|
288 session.addEditor(editor);
|
|
289
|
|
290 if(session.hasOwner()){
|
|
291 //このSessionManagerがオーナーを持っている場合、Sessionにエディタを追加し、エディタへAckを返す
|
316
|
292 REPCommand sendCommand = new REPCommand(receivedCommand);
|
164
|
293 sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
|
|
294 sendCommand.setEID(editor.getEID());
|
|
295 editor.send(sendCommand);
|
|
296 }else{
|
|
297 //オーナーを持ってない場合は、オーナーを持っているSessionManagerへSELECTコマンドを中継する
|
316
|
298 Forwarder owner = session.getOwner();
|
164
|
299 owner.send(receivedCommand);
|
148
|
300 }
|
164
|
301 }
|
144
|
302
|
164
|
303 break;
|
144
|
304
|
271
|
305 case SMCMD_SELECT_ACK:
|
160
|
306 {
|
85
|
307 String hostport = receivedCommand.string;
|
316
|
308 Forwarder editor = getEditor(hostport);
|
164
|
309
|
160
|
310 if(editor != null) {
|
|
311 //host, port を見て、このコマンドが自分が送信したSelectコマンドのAckかどうかを判断する
|
|
312 REPCommand command = new REPCommand();
|
|
313 command.setCMD(REP.SMCMD_JOIN_ACK);
|
|
314 command.setSID(receivedCommand.sid);
|
|
315 command.setEID(receivedCommand.eid);
|
|
316 editor.send(command);
|
164
|
317
|
85
|
318 }else{
|
160
|
319 //自分が送信したコマンドでなければ、次のSessionManagerへ中継する
|
85
|
320 smList.sendExcept(channel, receivedCommand);
|
|
321 }
|
160
|
322 }
|
144
|
323
|
164
|
324 break;
|
144
|
325
|
317
|
326 // Session Manager Command
|
|
327
|
271
|
328 case SMCMD_SM_JOIN:
|
164
|
329
|
160
|
330 {
|
316
|
331 // このchannelの相手は、SessionManager なので、
|
|
332 // 特別なhandlerを接続する必要がある
|
|
333 channel.register(selector, SelectionKey.OP_READ,
|
|
334 new REPSessionManagerHandler(this));
|
|
335
|
122
|
336 //SessionManagerのリストへ追加
|
83
|
337 smList.add(channel);
|
144
|
338
|
122
|
339 //XMLからSessionListオブジェクトを生成する。
|
77
|
340 SessionXMLDecoder decoder = new SessionXMLDecoder();
|
79
|
341 SessionList receivedSessionList = decoder.decode(receivedCommand.string);
|
144
|
342
|
122
|
343 //myHost を設定。
|
178
|
344 //立ち上げ時にやるとlocalhostしか取れない
|
76
|
345 if(myHost == null) setMyHostName(getLocalHostName(channel));
|
144
|
346
|
122
|
347 //maxHost を設定。
|
95
|
348 if(setMaxHost(channel, receivedSessionList.getMaxHost())){
|
316
|
349 REPCommand sendCommand = new REPCommand();
|
95
|
350 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
|
|
351 sendCommand.setString(maxHost);
|
|
352 smList.sendExcept(channel, sendCommand);
|
|
353 }
|
144
|
354
|
122
|
355 //SessionListからXMLを生成。
|
|
356 //joinしてきたSessionManagerに対してACKを送信。
|
164
|
357 SessionXMLEncoder sessionlistEncoder = new SessionXMLEncoder(sessionList);
|
316
|
358 REPCommand sendCommand = new REPCommand();
|
78
|
359 sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
|
|
360 sendCommand.setString(sessionlistEncoder.sessionListToXML());
|
178
|
361 send.write(sendCommand);
|
144
|
362
|
122
|
363 //その他の SessionManager に対して SMCMD_UPDATEを 送信。
|
78
|
364 sendCommand = new REPCommand();
|
83
|
365 sendCommand.setCMD(REP.SMCMD_UPDATE);
|
78
|
366 sendCommand.setString(receivedCommand.string);
|
|
367 smList.sendExcept(channel, sendCommand);
|
144
|
368
|
160
|
369 }
|
164
|
370 break;
|
144
|
371
|
271
|
372 case SMCMD_SM_JOIN_ACK:
|
144
|
373
|
122
|
374 //XMLからSessionListオブジェクトを生成。
|
82
|
375 SessionXMLDecoder decoder2 = new SessionXMLDecoder();
|
|
376 SessionList receivedSessionList2 = decoder2.decode(receivedCommand.string);
|
144
|
377
|
122
|
378 //maxHostを決定。
|
95
|
379 if(setMaxHost(channel, receivedSessionList2.getMaxHost())){
|
316
|
380 REPCommand sendCommand = new REPCommand();
|
95
|
381 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
|
|
382 sendCommand.setString(maxHost);
|
|
383 smList.sendExcept(channel, sendCommand);
|
|
384 }
|
144
|
385
|
6
|
386 break;
|
144
|
387
|
271
|
388 case SMCMD_UPDATE:
|
200
|
389 {
|
99
|
390 SessionXMLDecoder decoder3 = new SessionXMLDecoder();
|
|
391 SessionList receivedSessionList3 = decoder3.decode(receivedCommand.string);
|
144
|
392
|
200
|
393 //UPDATEコマンドにより送られてきたSessionの情報を追加する
|
|
394 LinkedList<Session> list = receivedSessionList3.getList();
|
|
395 for(Session session : list){
|
|
396 session.getEditorList().get(0).setChannel(channel);
|
|
397 sessionList.add(session);
|
|
398 }
|
|
399
|
|
400 //他のSessionManagerへ中継する
|
99
|
401 smList.sendExcept(channel, receivedCommand);
|
144
|
402
|
200
|
403 //リストのコピーをGUIに渡す
|
|
404 LinkedList<Session> sList = new LinkedList<Session>(sessionList);
|
|
405 LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
|
|
406 //GUIに反映
|
|
407 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
|
279
|
408 gui.invokeLater(doRun);
|
200
|
409 }
|
9
|
410 break;
|
144
|
411
|
271
|
412 case SMCMD_UPDATE_ACK:
|
200
|
413 {
|
164
|
414 if(receivedCommand.sid > sessionList.size()){
|
317
|
415 Editor editor = new Editor(this,0,channel);
|
75
|
416 editor.setName(receivedCommand.string);
|
144
|
417
|
158
|
418 Session session = new Session(editor);
|
73
|
419 session.addEditor(editor);
|
144
|
420
|
164
|
421 sessionList.add(session);
|
200
|
422
|
|
423 //リストのコピーをGUIに渡す
|
|
424 LinkedList<Session> sList = new LinkedList<Session>(sessionList);
|
|
425 LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
|
|
426 //GUIに反映
|
|
427 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
|
279
|
428 gui.invokeLater(doRun);
|
73
|
429 }
|
75
|
430 smList.sendToSlave(receivedCommand);
|
200
|
431 }
|
1
|
432 break;
|
144
|
433
|
271
|
434 case SMCMD_CH_MASTER:
|
200
|
435 {
|
122
|
436 //maxHost を設定。
|
95
|
437 if(setMaxHost(channel, receivedCommand.string)){
|
316
|
438 REPCommand sendCommand = new REPCommand();
|
95
|
439 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
|
|
440 sendCommand.setString(maxHost);
|
|
441 smList.sendExcept(channel, sendCommand);
|
|
442 }
|
200
|
443 }
|
95
|
444 break;
|
144
|
445
|
313
|
446 case SMCMD_START_MERGE_ACK:
|
|
447 {
|
|
448 // sid から Session を取得
|
|
449 Session session = getSession(receivedCommand.sid);
|
|
450 if (session==null) throw new IOException();
|
|
451 // マージの処理と次のエディタへコマンドを送信する処理
|
|
452 Editor editor = session.getEditor(channel);
|
315
|
453 if (!editor.merge(editor,receivedCommand)) {
|
|
454 // nothing to do, send END_MERGE
|
317
|
455 editor.endMerge();
|
164
|
456 }
|
313
|
457 break;
|
164
|
458 }
|
300
|
459 case SMCMD_QUIT:
|
|
460 {
|
|
461 Session session = getSession(receivedCommand.sid);
|
|
462 if (session==null) throw new IOException();
|
|
463 session.sendToNextEditor(channel,receivedCommand);
|
|
464 break;
|
|
465 }
|
|
466 case SMCMD_QUIT_2:
|
|
467 {
|
|
468 Session session = getSession(receivedCommand.sid);
|
|
469 if (session==null) throw new IOException();
|
|
470 Editor editor = session.getEditor(channel);
|
|
471 editor.setQuit2(receivedCommand);
|
|
472 break;
|
|
473 }
|
213
|
474 default:
|
|
475 assert(false);
|
|
476 break;
|
|
477
|
144
|
478 }
|
|
479 }
|
|
480
|
259
|
481 private void updateGUI() {
|
212
|
482 //リストのコピーをGUIに渡す
|
|
483 LinkedList<Session> sList = new LinkedList<Session>(sessionList);
|
|
484 LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
|
|
485 //GUIに反映
|
|
486 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
|
279
|
487 gui.invokeLater(doRun);
|
212
|
488 }
|
|
489
|
316
|
490 private Forwarder getEditor(String hostport) {
|
178
|
491 for(Editor editor : editorList){
|
|
492 if(editor.getHost() == hostport){
|
|
493 return editor;
|
|
494 }
|
|
495 }
|
|
496 return null;
|
|
497 }
|
|
498
|
316
|
499 public Session getSession(int sid) throws IOException {
|
144
|
500 for(Session session : sessionList){
|
|
501 if(session.getSID() == sid) return session;
|
|
502 }
|
316
|
503 throw new IOException();
|
0
|
504 }
|
83
|
505
|
224
|
506 private boolean setMaxHost(REPSocketChannel<REPCommand> channel, String maxHost2) {
|
179
|
507 if(maxHost.compareTo(maxHost2) > 0){
|
|
508 return false;
|
|
509 }else{
|
|
510 maxHost = maxHost2;
|
|
511 return true;
|
|
512 }
|
139
|
513 }
|
|
514
|
76
|
515 private void setMyHostName(String localHostName) {
|
308
|
516 myHost = localHostName + receive_port;
|
81
|
517 if(maxHost == null) {
|
|
518 maxHost = myHost;
|
|
519 }
|
164
|
520 setHostToEditor(myHost);
|
|
521 }
|
|
522
|
|
523 private void setHostToEditor(String myHost2) {
|
|
524 for(Editor editor : editorList){
|
|
525 editor.setHost(myHost2);
|
|
526 }
|
76
|
527 }
|
0
|
528
|
178
|
529 public void connectSession(String host) {
|
101
|
530 int port = DEFAULT_PORT;
|
308
|
531 port = parent_port;
|
1
|
532 InetSocketAddress addr = new InetSocketAddress(host, port);
|
|
533 try {
|
186
|
534 REPSocketChannel<REPCommand> sessionchannel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
|
1
|
535 sessionchannel.configureBlocking(true);
|
|
536 sessionchannel.connect(addr);
|
6
|
537 while(!sessionchannel.finishConnect()){
|
77
|
538 System.out.print("test afro");
|
6
|
539 }
|
|
540 System.out.println("");
|
229
|
541 registerChannel(sessionchannel, SelectionKey.OP_READ);
|
45
|
542
|
77
|
543 sm_join(sessionchannel);
|
45
|
544
|
1
|
545 }catch (IOException e) {
|
|
546 e.printStackTrace();
|
|
547 }
|
|
548 }
|
77
|
549
|
164
|
550 private void sm_join(REPSocketChannel<REPCommand> channel){
|
79
|
551
|
122
|
552 //SM_JOINコマンドを生成。
|
77
|
553 REPCommand command = new REPCommand();
|
|
554 command.setCMD(REP.SMCMD_SM_JOIN);
|
79
|
555
|
122
|
556 //hostnameをセット。
|
82
|
557 setMyHostName(getLocalHostName(channel));
|
|
558
|
122
|
559 //XMLを生成。送信コマンドにセット。
|
164
|
560 SessionXMLEncoder encoder = new SessionXMLEncoder(sessionList);
|
77
|
561 String string = encoder.sessionListToXML();
|
|
562 command.setString(string);
|
|
563
|
122
|
564 //SM_JOINコマンドを送信。
|
186
|
565 channel.write(command);
|
122
|
566 //SessionManagerのListに追加。
|
77
|
567 smList.add(channel);
|
|
568 }
|
2
|
569
|
271
|
570 private String getLocalHostName(REPSocketChannel<?> channel) {
|
74
|
571 String host = null;
|
|
572 host = channel.socket().getLocalAddress().getHostName();
|
|
573 return host;
|
|
574 }
|
|
575
|
316
|
576 public void selectSession(SelectButtonEvent event) throws IOException {
|
250
|
577 REPSocketChannel<REPCommand> channel = event.getEditorChannel();
|
|
578 int sid = event.getSID();
|
|
579 int eid = event.getEID();
|
164
|
580 Session session = getSession(sid);
|
227
|
581
|
250
|
582 Editor editor = editorList.get(eid);
|
227
|
583 if(editor == null){
|
|
584 System.out.println("SessionManager.selectSession():editor = " + editor);
|
|
585 return;
|
|
586 }
|
|
587
|
|
588 session.addEditor(editor);
|
|
589
|
304
|
590 System.out.println("SessionManager.session.hasOnwer="+session.hasOwner());
|
158
|
591 if(session.hasOwner()){
|
107
|
592 REPCommand sendCommand = new REPCommand();
|
|
593 sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
|
148
|
594 sendCommand.setEID(editor.getEID());
|
107
|
595 sendCommand.setSID(sid);
|
286
|
596 sendCommand.string = "";
|
186
|
597 channel.write(sendCommand);
|
107
|
598 }else {
|
250
|
599 sid = event.getSID();
|
317
|
600 editor = new Editor(this,0,channel);
|
107
|
601 editor.setHost(myHost);
|
164
|
602 session = getSession(sid);
|
107
|
603 session.addEditor(editor);
|
|
604
|
316
|
605 Forwarder owner = session.getOwner();
|
107
|
606
|
|
607 REPCommand command = new REPCommand();
|
|
608 command.setCMD(REP.SMCMD_SELECT);
|
|
609 command.setSID(sid);
|
178
|
610 command.setString(editor.getHost());
|
107
|
611 owner.send(command);
|
|
612 }
|
8
|
613 }
|
122
|
614
|
144
|
615 public void addWaitingCommand(PacketSet set) {
|
212
|
616 waitingCommandInMerge.add(set);
|
144
|
617 }
|
148
|
618
|
222
|
619 public void buttonPressed(SessionManagerEvent event) {
|
|
620 try {
|
308
|
621 waitingEventQueue.put(event);
|
222
|
622 } catch (InterruptedException e) {}
|
|
623 selector.wakeup();
|
|
624 }
|
281
|
625
|
|
626 public void syncExec(SessionManagerEvent event) {
|
|
627 try {
|
308
|
628 waitingEventQueue.put(event);
|
281
|
629 } catch (InterruptedException e) {
|
|
630 }
|
|
631 }
|
222
|
632
|
259
|
633 public void closeSession(SessionManagerEvent event) {
|
|
634 Session session = ((CloseButtonEvent) event).getSession();
|
|
635 session.closeSession();
|
|
636 sessionList.remove(session);
|
|
637 updateGUI();
|
|
638 }
|
|
639
|
274
|
640 public void remove(REPSocketChannel<REPCommand> channel) {
|
|
641 for(Session s:sessionList) {
|
|
642 if (s.deleteEditor(channel)) {
|
|
643 return ;
|
|
644 }
|
|
645 }
|
|
646 assert(false);
|
|
647 // can be other session manager? what should I do?
|
|
648 }
|
|
649
|
317
|
650
|
|
651 public void addWriteQueue(PacketSet packetSet) {
|
|
652 writeQueue.add(packetSet);
|
|
653 }
|
|
654
|
0
|
655 }
|