diff 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
line wrap: on
line diff
--- a/rep/ServerMainLoop.java	Wed Oct 20 21:19:24 2010 +0900
+++ b/rep/ServerMainLoop.java	Wed Oct 20 22:59:19 2010 +0900
@@ -59,6 +59,7 @@
 	}
 	
 	public void mainLoop() throws IOException {
+		int deadlock = 0;
 		while(running){
 			manager.checkWaitingCommandInMerge();
 			if (checkInputEvent() ||
@@ -69,11 +70,15 @@
 				   //continue;
 			}
 			// now we can wait for input packet or event
+			if (deadlock++>1000) deadlockDetected();
 			selector.select(1);
-			select();
+			if (select()) deadlock=0;
 		}
 	}
 
+	public void deadlockDetected() throws IOException {
+	}
+
 	void serverInit() throws IOException, SocketException,
 			ClosedChannelException {
 		selector = REPSelector.<REPCommand>create();	
@@ -148,10 +153,11 @@
 	 *     check incoming connection request and incoming packet
 	 *     A request is handled by a handler object which is attached
 	 *     to the SelectionKey.  
+	 * @return 
 	 * @throws IOException
 	 */
-	private void select() throws IOException {
-		
+	private boolean select() throws IOException {
+		boolean flag = false;
 		Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1();
 		for(REPSelectionKey<REPCommand> key : keys){
 			if(key.isAcceptable()){
@@ -162,6 +168,7 @@
 				REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker());
 				logger.writeLog("SessionManager.select() : key.isAcceptable : channel = " + channel);
 				registerChannel(channel, new FirstConnector(manager,channel));
+				flag = true;
 			} else if(key.isReadable()){
 				/*
 				 * Incoming packets are handled by a various forwarder.
@@ -172,12 +179,14 @@
 				try {
 					REPCommand command = key.channel1().read();
 					handler.handle(command, key);
+					flag = true;
 				} catch (IOException e) {
 					key.cancel();
 					handler.cancel(key.channel1());
 				}
 			}
 		}
+		return flag;
 	}
 
 	public void registerChannel(REPSocketChannel<REPCommand> channel, REPNode handler) throws IOException {