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