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