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