comparison rep/ServerMainLoop.java @ 485:cc262a519b8a

add dead lock detection (don't forget remove )
author one
date Wed, 20 Oct 2010 22:59:19 +0900
parents d295e84c5e03
children 5945266c970d
comparison
equal deleted inserted replaced
484:7420dea70dd7 485:cc262a519b8a
57 serverInit(); 57 serverInit();
58 mainLoop(); 58 mainLoop();
59 } 59 }
60 60
61 public void mainLoop() throws IOException { 61 public void mainLoop() throws IOException {
62 int deadlock = 0;
62 while(running){ 63 while(running){
63 manager.checkWaitingCommandInMerge(); 64 manager.checkWaitingCommandInMerge();
64 if (checkInputEvent() || 65 if (checkInputEvent() ||
65 checkWaitingWrite()) { 66 checkWaitingWrite()) {
66 continue; 67 continue;
67 // try to do fair execution for waiting task 68 // try to do fair execution for waiting task
68 //if(selector.selectNow() > 0) select(); 69 //if(selector.selectNow() > 0) select();
69 //continue; 70 //continue;
70 } 71 }
71 // now we can wait for input packet or event 72 // now we can wait for input packet or event
73 if (deadlock++>1000) deadlockDetected();
72 selector.select(1); 74 selector.select(1);
73 select(); 75 if (select()) deadlock=0;
74 } 76 }
77 }
78
79 public void deadlockDetected() throws IOException {
75 } 80 }
76 81
77 void serverInit() throws IOException, SocketException, 82 void serverInit() throws IOException, SocketException,
78 ClosedChannelException { 83 ClosedChannelException {
79 selector = REPSelector.<REPCommand>create(); 84 selector = REPSelector.<REPCommand>create();
146 /** 151 /**
147 * Main Select routing 152 * Main Select routing
148 * check incoming connection request and incoming packet 153 * check incoming connection request and incoming packet
149 * A request is handled by a handler object which is attached 154 * A request is handled by a handler object which is attached
150 * to the SelectionKey. 155 * to the SelectionKey.
156 * @return
151 * @throws IOException 157 * @throws IOException
152 */ 158 */
153 private void select() throws IOException { 159 private boolean select() throws IOException {
154 160 boolean flag = false;
155 Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1(); 161 Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1();
156 for(REPSelectionKey<REPCommand> key : keys){ 162 for(REPSelectionKey<REPCommand> key : keys){
157 if(key.isAcceptable()){ 163 if(key.isAcceptable()){
158 /* 164 /*
159 * Incoming connection. We don't know which, editor or 165 * Incoming connection. We don't know which, editor or
160 * session manager. Assign FirstConnector to distinguish. 166 * session manager. Assign FirstConnector to distinguish.
161 */ 167 */
162 REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker()); 168 REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker());
163 logger.writeLog("SessionManager.select() : key.isAcceptable : channel = " + channel); 169 logger.writeLog("SessionManager.select() : key.isAcceptable : channel = " + channel);
164 registerChannel(channel, new FirstConnector(manager,channel)); 170 registerChannel(channel, new FirstConnector(manager,channel));
171 flag = true;
165 } else if(key.isReadable()){ 172 } else if(key.isReadable()){
166 /* 173 /*
167 * Incoming packets are handled by a various forwarder. 174 * Incoming packets are handled by a various forwarder.
168 * A handler throw IOException() in case of a trouble to 175 * A handler throw IOException() in case of a trouble to
169 * close the channel. 176 * close the channel.
170 */ 177 */
171 REPNode handler = (REPNode)key.attachment(); 178 REPNode handler = (REPNode)key.attachment();
172 try { 179 try {
173 REPCommand command = key.channel1().read(); 180 REPCommand command = key.channel1().read();
174 handler.handle(command, key); 181 handler.handle(command, key);
182 flag = true;
175 } catch (IOException e) { 183 } catch (IOException e) {
176 key.cancel(); 184 key.cancel();
177 handler.cancel(key.channel1()); 185 handler.cancel(key.channel1());
178 } 186 }
179 } 187 }
180 } 188 }
189 return flag;
181 } 190 }
182 191
183 public void registerChannel(REPSocketChannel<REPCommand> channel, REPNode handler) throws IOException { 192 public void registerChannel(REPSocketChannel<REPCommand> channel, REPNode handler) throws IOException {
184 if(channel == null) { 193 if(channel == null) {
185 return; 194 return;