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