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