Show
Ignore:
Timestamp:
07/14/07 11:22:05 PM (18 months ago)
Author:
octorian
Message:

Implemented connection tracking and cleanup (#28)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/LogicMail/src/org/logicprobe/LogicMail/util/Connection.java

    r129 r130  
    6363import java.io.InputStream; 
    6464import java.io.OutputStream; 
     65import java.util.Vector; 
    6566import javax.microedition.io.SocketConnection; 
    6667import javax.microedition.io.StreamConnection; 
     
    114115     */ 
    115116    private int count; 
    116      
    117     /** 
    118      * Holds the position of the next byte to be read from the buffer. 
    119      */ 
    120     //private int position; 
     117 
     118    /** 
     119     * Holds a list of open connections 
     120     */ 
     121    private static Vector openConnections = new Vector(); 
    121122     
    122123    public Connection(String serverName, int serverPort, boolean useSSL, boolean deviceSide) { 
     
    136137    public void open() throws IOException { 
    137138        close(); 
     139 
     140        synchronized(openConnections) { 
     141            if(!openConnections.contains(this)) { 
     142                openConnections.addElement(this); 
     143            } 
     144        } 
     145         
    138146        String protocolStr = (useSSL ? "ssl" : "socket"); 
    139147        // This param, which allows bypassing the MDS proxy, should probably 
     
    166174            socket.close(); 
    167175            socket = null; 
     176        } 
     177 
     178        synchronized(openConnections) { 
     179            if(openConnections.contains(this)) { 
     180                openConnections.removeElement(this); 
     181            } 
     182        } 
     183    } 
     184     
     185    /** 
     186     * Determine whether open connections exist 
     187     * 
     188     * @return True if there are open connections 
     189     */ 
     190    public static boolean hasOpenConnections() { 
     191        boolean result; 
     192        synchronized(openConnections) { 
     193            result = !openConnections.isEmpty(); 
     194        } 
     195        return result; 
     196    } 
     197     
     198    /** 
     199     * Close all open connections 
     200     */ 
     201    public static void closeAllConnections() { 
     202        synchronized(openConnections) { 
     203            int size = openConnections.size(); 
     204            for(int i = 0; i < size; i++) { 
     205                try { 
     206                    ((Connection)openConnections.elementAt(i)).close(); 
     207                } catch (IOException e) { } 
     208            } 
     209            openConnections.removeAllElements(); 
    168210        } 
    169211    }