annotate rep/SessionManager.java @ 332:36faf76a087c

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