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