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