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