FileDocCategorySizeDatePackage
StreamConnectionPool.javaAPI DocJ2ME MIDP 2.08697Thu Nov 07 12:02:22 GMT 2002com.sun.midp.io.j2me.http

StreamConnectionPool

public class StreamConnectionPool extends Object
This class implements the necessary functionality for the HTTP persistent connection pool. This class contains individual http connection elements (or containers) including whether or/not a that particular connection is active. If a connection becomes inactive - its is available for reuse by another http connection session or the same one. This reduces the connection time for each supsequent connection.
author
Kerry Finn
version
1.0

Fields Summary
private long
m_connectionLingerTime
How long a connection can linger after its last use.
private Vector
m_connections
internal connection hash table
private int
m_max_connections
maximum connections
Constructors Summary
StreamConnectionPool(int number_of_connections, long connectionLingerTime)
Create a new instance of this class. We are initially unconnected this will create the first connection.

param
number_of_connections initial number of connections must greater than zero.
param
connectionLingerTime how many millseconds a connection should stay in the pool after its last use

        this.m_max_connections = number_of_connections;
        this.m_connectionLingerTime = connectionLingerTime;
        m_connections = new Vector(m_max_connections);
    
Methods Summary
synchronized booleanadd(java.lang.String p_protocol, java.lang.String p_host, int p_port, javax.microedition.io.StreamConnection sc, java.io.DataOutputStream dos, java.io.DataInputStream dis)
Tries to add a reuseable connection to the connection pool. Replace any not in use connections to the same host and port. Will not add to the same host and port in use. Will replace oldest not in use element (if any) if the pool is full.

param
p_protocol The protocol for the connection
param
p_host The Hostname for the connection
param
p_port The port number for the connection
param
sc The base stream connection
param
dos The data output stream from the base connection
param
dis The data input stream from the base connection
return
true if the connection was added, otherwise false


        StreamConnectionElement oldestNotInUse = null;

        // find the last unused element
        Enumeration cons = m_connections.elements();
        while (cons.hasMoreElements()) {
            StreamConnectionElement sce =
                (StreamConnectionElement)cons.nextElement();

            if (sce.m_in_use) {
                if (p_host.equals(sce.m_host) && p_port == sce.m_port) {
                    return false;
                }

                continue;
            }

            // if the connection is a duplicate, delete it
            if (p_host.equals(sce.m_host) && p_port == sce.m_port) {
                // no protocol duplicates on host and port
                sce.close();
                m_connections.removeElement(sce);
                break;
            } else {
                if (oldestNotInUse == null ||
                    sce.m_time < oldestNotInUse.m_time) {
                    // save the oldest not in use, it may be removed later
                    oldestNotInUse = sce;
                }
            }
        }

        /*
         * first check and see if the maximum number of connections
         * has been reached - if so delete the first one in the list (FIFO)
         *   or
         * if this port and host are already in the pool.
         */
        if (m_connections.size() >= m_max_connections) {
            if (oldestNotInUse == null) {
                return false;
            }

            oldestNotInUse.close();
            m_connections.removeElement(oldestNotInUse);
        }
     
        m_connections.addElement(new StreamConnectionElement(p_protocol,
                          p_host, p_port, sc, dos, dis));
        return true;
    
synchronized StreamConnectionElementget(java.lang.String p_protocol, java.lang.String p_host, int p_port)
get an available connection and set the boolean flag to true (unavailable) in the connection pool. Also removes any stale connections, since this method gets called more than add or remove.

param
p_protocol The protocol for the connection
param
p_host The Hostname for the connection
param
p_port The port number for the connection
return
A stream connection element or null if not found


        StreamConnectionElement result = null;
        long c_time = System.currentTimeMillis();

        Enumeration cons = m_connections.elements();
        while (cons.hasMoreElements()) {
            StreamConnectionElement sce =
                (StreamConnectionElement)cons.nextElement();
            
            if ((c_time - sce.m_time) > m_connectionLingerTime) {
                if (!sce.m_in_use) {
                    sce.close();
                } else {
                    // signal returnToUse() to close
                    sce.m_removed = true;
                }

                m_connections.removeElement(sce);
                continue;
            }

            if (p_host.equals(sce.m_host) && p_port == sce.m_port &&
                    p_protocol.equals(sce.m_protocol) && !sce.m_in_use) {
                result = sce;

                // do not break out so old connections can be removed
                continue;
            }
        }

        if (result != null) {
            result.m_in_use = true;
        }

        return result;
    
synchronized voidremove(StreamConnectionElement sce)
Close connection and remove an instance of the stream connection element from the connection pool.

param
sce The stream connection element to remove

	sce.close();
        m_connections.removeElement(sce);
    
synchronized voidreturnForReuse(StreamConnectionElement returned)
Return an instance of the stream connection element to the connection pool so it can be reused. It is done in the method so it can be synchronized with the get method.

param
returned The stream connection element to return

        returned.m_in_use = false;

        if (returned.m_removed) {
            // the connection was out too long
            returned.close();
            return;
        }

        returned.m_time = System.currentTimeMillis();