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