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