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