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