comparison rep/SessionManager.java @ 75:a636b651dbce

*** empty log message ***
author pin
date Sun, 25 Nov 2007 03:21:17 +0900
parents 391d44c94799
children 97ca5f5447cd
comparison
equal deleted inserted replaced
74:391d44c94799 75:a636b651dbce
46 public void openSelector() throws IOException{ 46 public void openSelector() throws IOException{
47 selector = Selector.open(); 47 selector = Selector.open();
48 } 48 }
49 49
50 public void sessionManagerNet(int port) throws InterruptedException, IOException { 50 public void sessionManagerNet(int port) throws InterruptedException, IOException {
51 /**
52 * @param args
53 * @throws IOException
54 * @throws InterruptedException
55 * @throws IOException
56 * @throws InterruptedException
57 */
58 System.out.println("sessionManagerNet()");
59 51
60 ServerSocketChannel ssc = ServerSocketChannel.open(); 52 ServerSocketChannel ssc = ServerSocketChannel.open();
61 ssc.configureBlocking(false); 53 ssc.configureBlocking(false);
62 ssc.socket().bind(new InetSocketAddress(port)); 54 ssc.socket().bind(new InetSocketAddress(port));
63 ssc.register(selector, SelectionKey.OP_ACCEPT); 55 ssc.register(selector, SelectionKey.OP_ACCEPT);
93 85
94 }else if(key.isReadable()){ 86 }else if(key.isReadable()){
95 87
96 /*** EditorChannel を用いない記述 ***/ 88 /*** EditorChannel を用いない記述 ***/
97 SocketChannel channel = (SocketChannel)key.channel(); 89 SocketChannel channel = (SocketChannel)key.channel();
98 REPPacketReceive repRec = new REPPacketReceive(channel); //getPacket(), putPacket() にする。 90 REPPacketReceive receive = new REPPacketReceive(channel); //getPacket(), putPacket() にする。
99 repRec.setkey(key); 91 receive.setkey(key);
100 //REPCommand repCom = repRec.unpackUConv(); 92 //REPCommand repCom = repRec.unpackUConv();
101 REPCommand repCom = repRec.unpack(); 93 REPCommand receivedCommand = receive.unpack();
102 manager(channel, repCom); 94 manager(channel, receivedCommand);
103 95
104 /*** EditorChannel を用いた記述 ****/ 96 /*** EditorChannel を用いた記述 ****/
105 //EditorChannel echannel = (EditorChannel) key.channel(); 97 //EditorChannel echannel = (EditorChannel) key.channel();
106 //REPCommand command = echannel.getPacket(); 98 //REPCommand command = echannel.getPacket();
107 //manager(echannel, command); 99 //manager(echannel, command);
121 channel.configureBlocking(false); 113 channel.configureBlocking(false);
122 selector.wakeup(); 114 selector.wakeup();
123 channel.register(selector, ops); 115 channel.register(selector, ops);
124 } 116 }
125 117
126 private void manager(SocketChannel channel, REPCommand repCmd) { 118 private void manager(SocketChannel channel, REPCommand receivedCommand) {
127 if(repCmd == null) return; 119 if(receivedCommand == null) return;
128 Editor editor; 120 Editor editor;
129 Session session; 121 Session session;
130 switch(repCmd.cmd){ 122 REPCommand sendCommand = receivedCommand.clone();
123
124 switch(receivedCommand.cmd){
131 125
132 case REP.SMCMD_JOIN: 126 case REP.SMCMD_JOIN:
133 if(isMaster){ 127 if(isMaster){
134 int eid = editorList.addEditor(channel, repCmd); 128 int eid = editorList.addEditor(channel, receivedCommand);
135 repCmd.setEID(eid); 129 receivedCommand.setEID(eid);
136 editorList.sendJoinAck(channel, repCmd); 130 editorList.sendJoinAck(channel, receivedCommand);
137 sessionmanagerGUI.setComboEditor(eid, channel); 131 sessionmanagerGUI.setComboEditor(eid, channel);
138 }else{ 132 }else{
139 editorList.addEditor(channel); 133 editorList.addEditor(channel);
140 smList.sendJoin(repCmd); 134 smList.sendJoin(receivedCommand);
141 //sessionmanagerGUI.setComboEditor(repCmd.eid, channel); 135 //sessionmanagerGUI.setComboEditor(repCmd.eid, channel);
142 } 136 }
143 break; 137 break;
144 138
145 case REP.SMCMD_JOIN_ACK: 139 case REP.SMCMD_JOIN_ACK:
149 break; 143 break;
150 144
151 case REP.SMCMD_PUT: 145 case REP.SMCMD_PUT:
152 editor = new Editor(channel); 146 editor = new Editor(channel);
153 editor.setEID(1); 147 editor.setEID(1);
154 editor.setName(repCmd.string); 148 editor.setName(receivedCommand.string);
155 session = new Session(editor); 149 session = new Session(editor);
156 session.setOwner(true); 150 session.setOwner(true);
157 session.addEditor(editor); 151 session.addEditor(editor);
158 sessionlist.addSession(session); 152 sessionlist.addSession(session);
159 sessionmanagerGUI.setComboSession(session.getSID(), session.getName()); 153 sessionmanagerGUI.setComboSession(session.getSID(), session.getName());
160 sessionmanagerGUI.setComboEditor(editor.getEID(), editor.getChannel()); 154 sessionmanagerGUI.setComboEditor(editor.getEID(), editor.getChannel());
161 session.addToRoutingTable(editor); 155 session.addToRoutingTable(editor);
162 repCmd.setCMD(REP.SMCMD_PUT_ACK); 156 receivedCommand.setCMD(REP.SMCMD_PUT_ACK);
163 repCmd.setEID(1); 157 receivedCommand.setEID(1);
164 repCmd.setSID(session.getSID()); 158 receivedCommand.setSID(session.getSID());
165 editor.send(repCmd); 159 editor.send(receivedCommand);
166 160
167 //if(isMaster){ 161 //if(isMaster){
168 SessionXMLEncoder encoder = new SessionXMLEncoder(session); 162 SessionXMLEncoder encoder = new SessionXMLEncoder(session);
169 REPCommand command = new REPCommand(); 163 REPCommand command = new REPCommand();
170 command.setSID(session.getSID()); 164 command.setSID(session.getSID());
182 // break; 176 // break;
183 177
184 case REP.SMCMD_SELECT: 178 case REP.SMCMD_SELECT:
185 // sessionlist.addEditor(channel, repCmd.sid, repCmd); //sessionlistへ追加 179 // sessionlist.addEditor(channel, repCmd.sid, repCmd); //sessionlistへ追加
186 editor = new Editor(channel); 180 editor = new Editor(channel);
187 session = sessionlist.getSession(repCmd.sid); 181 session = sessionlist.getSession(receivedCommand.sid);
188 if(session.isOwner()){ 182 if(session.isOwner()){
189 int eid = session.addEditor(editor); 183 int eid = session.addEditor(editor);
190 editor.setEID(eid); 184 editor.setEID(eid);
191 REPPacketSend send = new REPPacketSend(channel); 185 REPPacketSend send = new REPPacketSend(channel);
192 repCmd.setCMD(REP.SMCMD_SELECT_ACK); 186 receivedCommand.setCMD(REP.SMCMD_SELECT_ACK);
193 repCmd.setEID(eid); 187 receivedCommand.setEID(eid);
194 send.send(repCmd); 188 send.send(receivedCommand);
195 }else { 189 }else {
196 190
197 } 191 }
198 192
199 break; 193 break;
200 194
201 case REP.SMCMD_SELECT_ACK: 195 case REP.SMCMD_SELECT_ACK:
202 repCmd.setCMD(REP.SMCMD_JOIN_ACK); 196 receivedCommand.setCMD(REP.SMCMD_JOIN_ACK);
203 repCmd.setEID(repCmd.eid); 197 receivedCommand.setEID(receivedCommand.eid);
204 session = sessionlist.getSession(repCmd.sid); 198 session = sessionlist.getSession(receivedCommand.sid);
205 session.sendToEditor(repCmd); 199 session.sendToEditor(receivedCommand);
206 //Editor editor3 = session3.getEditorList().get(0); 200 //Editor editor3 = session3.getEditorList().get(0);
207 //REPPacketSend send = new REPPacketSend(editor3.getChannel()); 201 //REPPacketSend send = new REPPacketSend(editor3.getChannel());
208 //send.send(repCmd); 202 //send.send(repCmd);
209 break; 203 break;
210 204
211 case REP.SMCMD_SM_JOIN: 205 case REP.SMCMD_SM_JOIN:
212 myHost = getHostName(channel); 206 myHost = getLocalHostName(channel);
213 if(myHost.compareTo(repCmd.string) > 0){ 207 if(myHost.compareTo(receivedCommand.string) < 0)
214 208 setMaster(false);
215 }else if(myHost.compareTo(repCmd.string) < 0){ 209
216 isMaster = false;
217 }
218 if(isMaster){ 210 if(isMaster){
219 smList.add(channel); 211 smList.add(channel);
220 repCmd.setCMD(REP.SMCMD_SM_JOIN_ACK); 212 receivedCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
221 smList.sendSessionList(sessionlist, repCmd); 213 smList.sendSessionList(sessionlist, receivedCommand);
222 }else { 214 }else {
223 215
224 } 216 }
225 break; 217 break;
226 218
227 case REP.SMCMD_SM_JOIN_ACK: 219 case REP.SMCMD_SM_JOIN_ACK:
228 if(isMaster){ 220 if(isMaster){
229 smList.send(channel, repCmd); 221 smList.send(channel, receivedCommand);
230 }else{ 222 }else{
231 smList.send(channel, repCmd); 223 smList.send(channel, receivedCommand);
232 } 224 }
233 225
234 break; 226 break;
235 227
236 case REP.SMCMD_UPDATE: 228 case REP.SMCMD_UPDATE:
237 SessionXMLDecoder decoder = new SessionXMLDecoder(repCmd.string); 229 SessionXMLDecoder decoder = new SessionXMLDecoder(receivedCommand.string);
238 230
239 editor = new Editor(channel); 231 editor = new Editor(channel);
240 editor.setName(repCmd.string); 232 editor.setName(receivedCommand.string);
241 233
242 session = new Session(editor); 234 session = new Session(editor);
243 session.addEditor(editor); 235 session.addEditor(editor);
244 236
245 sessionlist.addSession(session); 237 sessionlist.addSession(session);
246 238
247 sessionmanagerGUI.setComboSession(session.getSID(), session.getName()); 239 sessionmanagerGUI.setComboSession(session.getSID(), session.getName());
248 240
249 if(isMaster){ 241 if(isMaster){
250 repCmd.setCMD(REP.SMCMD_UPDATE_ACK); 242 receivedCommand.setCMD(REP.SMCMD_UPDATE_ACK);
251 smList.sendToSlave(repCmd); 243 smList.sendToSlave(receivedCommand);
252 }else{ 244 }else{
253 repCmd.setCMD(REP.SMCMD_UPDATE); 245 receivedCommand.setCMD(REP.SMCMD_UPDATE);
254 smList.sendToMaster(repCmd); 246 smList.sendToMaster(receivedCommand);
255 } 247 }
256 break; 248 break;
257 249
258 case REP.SMCMD_UPDATE_ACK: 250 case REP.SMCMD_UPDATE_ACK:
259 if(repCmd.sid > sessionlist.getList().size()){ 251 if(receivedCommand.sid > sessionlist.getList().size()){
260 editor = new Editor(channel); 252 editor = new Editor(channel);
261 editor.setName(repCmd.string); 253 editor.setName(receivedCommand.string);
262 254
263 session = new Session(editor); 255 session = new Session(editor);
264 session.addEditor(editor); 256 session.addEditor(editor);
265 257
266 sessionlist.addSession(session); 258 sessionlist.addSession(session);
267 259
268 sessionmanagerGUI.setComboSession(session.getSID(), session.getName()); 260 sessionmanagerGUI.setComboSession(session.getSID(), session.getName());
269 } 261 }
270 smList.sendToSlave(repCmd); 262 smList.sendToSlave(receivedCommand);
271 break; 263 break;
272 264
273 case REP.REPCMD_READ: 265 case REP.REPCMD_READ:
274 //sessionlist.sendCmd(channel, repCmd); 266 //sessionlist.sendCmd(channel, repCmd);
275 break; 267 break;
276 268
277 default: 269 default:
278 //sessionlist.sendCmd(channel, repCmd); 270 //sessionlist.sendCmd(channel, repCmd);
279 sessionlist.sendToNextEditor(channel, repCmd); 271 sessionlist.sendToNextEditor(channel, receivedCommand);
280 break; 272 break;
281 } 273 }
282 } 274 }
283 275
284 // private void sendJoin(REPCommand repCmd) { 276 // private void sendJoin(REPCommand repCmd) {
285 // smList.sendJoin(repCmd); 277 // smList.sendJoin(repCmd);
286 // } 278 // }
279
280 private void setMaster(boolean b) {
281 // TODO Auto-generated method stub
282 isMaster = b;
283 System.out.println("isMaster = " + b);
284 }
287 285
288 private void sendSessionManagerJoinAck(SocketChannel channel, REPCommand repCmd) { 286 private void sendSessionManagerJoinAck(SocketChannel channel, REPCommand repCmd) {
289 String socketstring = getSocketString(channel); 287 String socketstring = getSocketString(channel);
290 System.out.println(socketstring); 288 System.out.println(socketstring);
291 smList.add(channel); //SessionManagerのリストへ追加 289 smList.add(channel); //SessionManagerのリストへ追加
330 } 328 }
331 329
332 private void openWindow() { 330 private void openWindow() {
333 Thread th = new Thread( sessionmanagerGUI ); 331 Thread th = new Thread( sessionmanagerGUI );
334 th.start(); 332 th.start();
335 System.out.println(sessionmanagerGUI.toString()); 333 //System.out.println(sessionmanagerGUI.toString());
336 sessionmanagerGUI.addConnectionListener(this); 334 sessionmanagerGUI.addConnectionListener(this);
337 sessionmanagerGUI.addREPActionListener(this); 335 sessionmanagerGUI.addREPActionListener(this);
338 } 336 }
339 337
340 private void connectSession(String host) { 338 private void connectSession(String host) {
357 //sm_join_com.setString(sm_join_com.string + ":" + socketString); 355 //sm_join_com.setString(sm_join_com.string + ":" + socketString);
358 // SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist.getList()); 356 // SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist.getList());
359 357
360 REPCommand command = new REPCommand(); 358 REPCommand command = new REPCommand();
361 command.setCMD(REP.SMCMD_SM_JOIN); 359 command.setCMD(REP.SMCMD_SM_JOIN);
362 myHost = getHostName(sessionchannel); 360 myHost = getLocalHostName(sessionchannel);
363 command.setString(myHost); 361 command.setString(myHost);
364 //command.setString(encoder.sessionListToXML()); 362 //command.setString(encoder.sessionListToXML());
365 send.send(command); 363 send.send(command);
366 364
367 //send.send(REPCommand.SMCMD_SESSION_JOIN); 365 //send.send(REPCommand.SMCMD_SESSION_JOIN);
371 }catch (IOException e) { 369 }catch (IOException e) {
372 e.printStackTrace(); 370 e.printStackTrace();
373 } 371 }
374 } 372 }
375 373
376 private String getHostName(SocketChannel channel) { 374 private String getLocalHostName(SocketChannel channel) {
377 String host = null; 375 String host = null;
378 host = channel.socket().getLocalAddress().getHostName(); 376 host = channel.socket().getLocalAddress().getHostName();
379 return host; 377 return host;
380 } 378 }
381 379