classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: Stability fix for gnu/CORBA/SocketRepository


From: Meskauskas Audrius
Subject: [cp-patches] FYI: Stability fix for gnu/CORBA/SocketRepository
Date: Mon, 31 Oct 2005 12:20:17 +0100
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

This patch fixes some hanging problems that I observed when debugging my CORBA game example. These problems stayed unnoticed during tests because the game normally lasts much longer that it takes the test to complete.

2005-10-31  Audrius Meskauskas  <address@hidden>

* gnu/CORBA/SocketRepository.java (not_reusable, gc): New methods.
(sockets): Use hashtable.
Index: gnu/CORBA/SocketRepository.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/CORBA/SocketRepository.java,v
retrieving revision 1.3
diff -u -r1.3 SocketRepository.java
--- gnu/CORBA/SocketRepository.java     2 Sep 2005 15:53:05 -0000       1.3
+++ gnu/CORBA/SocketRepository.java     31 Oct 2005 10:33:20 -0000
@@ -40,8 +40,9 @@
 
 import java.net.Socket;
 import java.net.SocketException;
-
-import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
 
 /**
  * This class caches the opened sockets that are reused during the
@@ -55,10 +56,11 @@
   /**
    * The socket map.
    */
-  private static HashMap sockets = new HashMap();
-
+  private static Hashtable sockets = new Hashtable();
+  
   /**
-   * Put a socket.
+   * Put a socket. This method also discards all not reusable sockets from
+   * the map.
    *
    * @param key as socket key.
    *
@@ -67,6 +69,36 @@
   public static void put_socket(Object key, Socket s)
   {
     sockets.put(key, s);
+    gc();
+  }
+  
+  /**
+   * Removes all non reusable sockets.
+   */
+  public static void gc()
+  {
+    Iterator iter = sockets.entrySet().iterator();
+    
+    Map.Entry e;
+    Socket sx;
+    
+    while (iter.hasNext())
+      {
+        e = (Map.Entry) iter.next();
+        sx = (Socket) e.getValue();
+        
+        if (not_reusable(sx))
+          iter.remove();
+      }
+  }
+  
+  /**
+   * Return true if the socket is no longer reusable.
+   */
+  static boolean not_reusable(Socket s)
+  {
+    return (s.isClosed() || !s.isBound() || !s.isConnected() ||
+        s.isInputShutdown() || s.isOutputShutdown());
   }
 
   /**
@@ -75,21 +107,26 @@
    * @param key a socket key.
    * 
    * @return an opened socket for reuse, null if no such available or it is
-   * closed.
+   * closed, its input or output has been shutown or otherwise the socket
+   * is not reuseable.
    */
   public static Socket get_socket(Object key)
   {
+    if (true)
+      return null;
+    
     Socket s = (Socket) sockets.get(key);
     if (s == null)
       return null;
-    else if (s.isClosed())
+    
+    // Ensure that the socket is fully reusable.
+    else if (not_reusable(s))
       {
         sockets.remove(key);
         return null;
       }
     else
       {
-        sockets.remove(key);
         try
           {
             // Set one minute time out that will be changed later.
@@ -99,6 +136,8 @@
           {
             s = null;
           }
+        
+        sockets.remove(key);
         return s;
       }
   }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]