[archstudio-commits] svn commit: r7099 - /myxfw/trunk/edu.uci.isr.myx.fw/src/edu/uci/isr/ljm/LJMServer.java

archstudio-commits-owner at uci.edu archstudio-commits-owner at uci.edu
Wed May 7 15:38:24 PDT 2008


Author: shendric
Date: Wed May  7 15:38:24 2008
New Revision: 7099

Log:
Fixed: server would not release all connections upon destroy()

Modified:
    myxfw/trunk/edu.uci.isr.myx.fw/src/edu/uci/isr/ljm/LJMServer.java

Modified: myxfw/trunk/edu.uci.isr.myx.fw/src/edu/uci/isr/ljm/LJMServer.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- myxfw/trunk/edu.uci.isr.myx.fw/src/edu/uci/isr/ljm/LJMServer.java (orig=
inal)
+++ myxfw/trunk/edu.uci.isr.myx.fw/src/edu/uci/isr/ljm/LJMServer.java Wed M=
ay  7 15:38:24 2008
@@ -9,16 +9,16 @@
 import java.lang.reflect.Method;
 import java.net.ServerSocket;
 import java.net.Socket;
-import java.util.Arrays;
 import java.util.Hashtable;
 =

-public class LJMServer extends Thread{
+public class LJMServer
+	extends Thread{
 =

 	public static final int LJM_STAT_ERROR =3D 1000;
 	public static final int LJM_STAT_DONE =3D 2000;
 	public static final int LJM_STAT_RETVAL =3D 3000;
 	public static final int LJM_STAT_EXCEPTION =3D 4000;
-	=

+
 	protected ServerSocket socket;
 	protected Hashtable serverObjects =3D new Hashtable();
 =

@@ -26,28 +26,29 @@
 		//Create a socket on any available port
 		this(0);
 	}
-	=

+
 	public LJMServer(int port) throws IOException{
 		socket =3D new ServerSocket(port);
 		start();
 	}
-	=

+
 	public int getPort(){
 		return socket.getLocalPort();
 	}
-	=

+
 	public void deploy(String objectName, Object o){
 		serverObjects.put(objectName, o);
 	}
-	=

+
 	public Object getDeployedObject(String objectName){
 		return serverObjects.get(objectName);
 	}
-	=

+
 	public void undeploy(String objectName){
 		serverObjects.remove(objectName);
 	}
-	=

+
+	@Override
 	public void destroy(){
 		try{
 			socket.close();
@@ -55,58 +56,67 @@
 		catch(IOException e){
 		}
 	}
-	=

+
+	@Override
 	public void run(){
+		LJMConnectionHandler connHandler =3D null;
 		try{
 			while(true){
 				Socket slaveSocket =3D socket.accept();
-				LJMConnectionHandler connHandler =3D new LJMConnectionHandler(slaveSoc=
ket);
+				connHandler =3D new LJMConnectionHandler(slaveSocket);
 			}
 		}
 		catch(IOException e){
 			//e.printStackTrace();
 			return;
 		}
+		finally{
+			if(connHandler !=3D null){
+				connHandler.close();
+				connHandler =3D null;
+			}
+		}
 	}
 =

 	//Protocol is:
 	//1.  Read a boolean whether we want to continue or not (true =3D continu=
e, false =3D don't)
 	//If the boolean was false, we close the connection and return.
-	=

+
 	//2.  Read the object name (java.lang.String)
 	//3.  Read the method name (java.lang.String)
 	//4.  Read the parameter class list (java.lang.Class[])
 	//5.  Read the parameter value list (java.lang.Object[])
-	=

+
 	//Then we do our thing.  No matter what happens, an integer
 	//gets written to the wire:
 	//LJM_STAT_ERROR (a network, object lookup, or whatever error happened)
 	//LJM_STAT_DONE (the method ran and returned void)
 	//LJM_STAT_RETVAL (the method ran and returned a value
 	//LJM_STAT_EXCEPTION (the method ran and threw an exception)
-	=

+
 	//If LJM_STAT_ERROR happened, then an LJMException is written to the wire =

 	//If LJM_STAT_DONE happened, then that's the end of the interaction.
 	//If LJM_STAT_RETVAL happened then we write the return value to the wire =
and
 	//that's the end of the interaction
 	//If LJM_STAT_EXCEPTION happened, then we write the exception to the wire=
 and
 	//that's the end of the interaction.
-	=

+
 	//At the end of the interaction, we go back to reading the next interacti=
on.
-	=

-	class LJMConnectionHandler extends Thread{
-		=

+
+	class LJMConnectionHandler
+		extends Thread{
+
 		protected Socket slaveSocket;
 		protected InputStream is;
 		protected OutputStream os;
 		protected ObjectInputStream ois;
 		protected ObjectOutputStream oos;
-		=

+
 		public LJMConnectionHandler(Socket slaveSocket){
 			//System.out.println("Creating new LJMConnectionHandler!");
 			//new Throwable().printStackTrace();
 			int priority =3D Thread.NORM_PRIORITY;
-			this.setPriority(priority); =

+			this.setPriority(priority);
 			try{
 				this.slaveSocket =3D slaveSocket;
 				is =3D slaveSocket.getInputStream();
@@ -119,47 +129,53 @@
 				close();
 				return;
 			}
-			=

+
 			start();
 		}
-		=

+
 		public void close(){
 			try{
 				if(is !=3D null){
 					is.close();
 				}
 			}
-			catch(IOException ignored){}
-			=

+			catch(IOException ignored){
+			}
+
 			try{
 				if(ois !=3D null){
 					ois.close();
 				}
 			}
-			catch(IOException ignored2){}
-			=

+			catch(IOException ignored2){
+			}
+
 			try{
 				if(os !=3D null){
 					os.close();
 				}
 			}
-			catch(IOException ignored3){}
-			=

+			catch(IOException ignored3){
+			}
+
 			try{
 				if(oos !=3D null){
 					oos.close();
 				}
 			}
-			catch(IOException ignored4){}
-			=

+			catch(IOException ignored4){
+			}
+
 			try{
 				if(slaveSocket !=3D null){
 					slaveSocket.close();
 				}
 			}
-			catch(IOException ignored5){}
+			catch(IOException ignored5){
+			}
 		}
-		=

+
+		@Override
 		public void run(){
 			while(true){
 				//System.err.println("Processing call: ");
@@ -182,7 +198,7 @@
 					}
 					return;
 				}
-					=

+
 				catch(IOException ioe1){
 					try{
 						oos.writeInt(LJM_STAT_ERROR);
@@ -196,12 +212,12 @@
 					}
 					return;
 				}
-				=

+
 				if(!cont){
 					close();
 					return;
 				}
-				=

+
 				String objectName;
 				String methodName;
 				Class[] paramClasses;
@@ -231,7 +247,7 @@
 					}
 					close();
 					return;
-				}					=

+				}
 				catch(IOException ioe){
 					try{
 						oos.writeInt(LJM_STAT_ERROR);
@@ -259,7 +275,7 @@
 					close();
 					return;
 				}
-				=

+
 				//System.out.println("ObjectName=3D " + objectName);
 				Object targetObject =3D serverObjects.get(objectName);
 				//System.err.println("targetObject: "+targetObject);
@@ -273,10 +289,10 @@
 						close();
 						return;
 					}
-						=

+
 					continue;
 				}
-				=

+
 				Class targetObjectClass =3D targetObject.getClass();
 				//System.err.println("targetObjectClass: "+targetObjectClass);
 				Method targetMethod =3D null;
@@ -294,18 +310,17 @@
 						close();
 						return;
 					}
-					=

+
 					continue;
 				}
-				=

+
 				//System.out.println("Performing method invocation on true object: " +=
 targetMethod);
 				Object retVal;
 				try{
 					//System.err.println("retVal: ?");
 					retVal =3D targetMethod.invoke(targetObject, paramValues);
 					//System.err.println("retVal: "+retVal);
-					if(targetMethod.getReturnType().equals(void.class)
-						|| targetMethod.getReturnType().equals(Void.class)){
+					if(targetMethod.getReturnType().equals(void.class) || targetMethod.ge=
tReturnType().equals(Void.class)){
 						try{
 							oos.writeInt(LJM_STAT_DONE);
 							oos.flush();
@@ -314,11 +329,11 @@
 							close();
 							return;
 						}
-							=

+
 						continue;
 					}
 					else{
-						if((retVal !=3D null) && (!(retVal instanceof java.io.Serializable))=
){
+						if(retVal !=3D null && !(retVal instanceof java.io.Serializable)){
 							try{
 								oos.writeInt(LJM_STAT_ERROR);
 								oos.writeObject(new LJMException("Return type (" + retVal.getClass=
().getName() + ") is not serializable."));
@@ -328,7 +343,7 @@
 								close();
 								return;
 							}
-							=

+
 							continue;
 						}
 						else{
@@ -372,7 +387,7 @@
 							close();
 							return;
 						}
-						=

+
 						continue;
 					}
 					else{
@@ -389,9 +404,7 @@
 					}
 				}
 			}
-		}				=

+		}
 	}
-	=

-	=

-}
 =

+}




More information about the archstudio-commits mailing list