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