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