FileDocCategorySizeDatePackage
ImpsSession.javaAPI DocAndroid 1.5 API15426Wed May 06 22:42:46 BST 2009com.android.im.imps

ImpsSession

public class ImpsSession extends Object
Represents the context of an IMPS session. The IMPS session is a framework in which the IMPS services are provided to the IMPS client. It's established when the client logs in and terminated when either the client logs out or the SAP decides to disconnect the session.

Fields Summary
private static final String
KEY_CIR_HTTP_ADDRESS
private static final String
KEY_CIR_TCP_PORT
private static final String
KEY_CIR_TCP_ADDRESS
private static final String
KEY_CIR_METHOD
private static final String
KEY_SERVER_POLL_MIN
private static final String
KEY_PASSWORD
private static final String
KEY_USERNAME
private static final String
KEY_KEEP_ALIVE_TIME
private static final String
KEY_SESSION_COOKIE
private static final String
KEY_SESSION_ID
private static final int
DEFAULT_TCP_PORT
private ImpsConnection
mConnection
private String
mId
private String
mCookie
private long
mKeepAliveTime
private com.android.im.imps.ImpsConnectionConfig.CirMethod
mCurrentCirMethod
private String
mCirTcpAddress
private int
mCirTcpPort
private long
mServerPollMin
private String
mCirHttpAddress
private com.android.im.engine.LoginInfo
mLoginInfo
private boolean
mCapablityRequest
private List
mSupportedCirMethod
private com.android.im.engine.Contact
mLoginUser
PrimitiveElement
mServiceTree
private boolean
mNew
Flag that indicates this is a new created session or not.
Constructors Summary
ImpsSession(ImpsConnection connection, com.android.im.engine.LoginInfo info)


         
        mConnection = connection;
        setLoginInfo(info);

        mNew = true;
        mCookie = ImpsUtils.genSessionCookie();

        mCirTcpPort = DEFAULT_TCP_PORT;
        mServerPollMin = connection.getConfig().getDefaultServerPollMin();
    
ImpsSession(ImpsConnection connection, HashMap values)

        mConnection = connection;
        mNew = false;
        mId = values.get(KEY_SESSION_ID);
        if (mId == null || mId.length() == 0) {
            throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT,
                "Missing session id");
        }
        mCookie = values.get(KEY_SESSION_COOKIE);
        if (mCookie == null || mCookie.length() == 0) {
            throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT,
                "Missing session cookie");
        }
        try {
            mKeepAliveTime = Long.parseLong(values.get(KEY_KEEP_ALIVE_TIME));
        } catch (NumberFormatException e) {
            throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT,
                "Invalid keepAliveTime");
        }
        try {
            mServerPollMin = Long.parseLong(values.get(KEY_SERVER_POLL_MIN));
        } catch (NumberFormatException e) {
            throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT,
                "Invalid serverPollMin");
        }
        String username = values.get(KEY_USERNAME);
        String password = values.get(KEY_PASSWORD);
        // Empty password might be valid
        if (username == null || username.length() == 0 || password == null) {
            throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT,
                "Invalid username or password");
        }
        setLoginInfo(new LoginInfo(username, password));

        mCurrentCirMethod = CirMethod.valueOf(values.get(KEY_CIR_METHOD));
        if (mCurrentCirMethod == CirMethod.STCP) {
            mCirTcpAddress = values.get(KEY_CIR_TCP_ADDRESS);
            if (mCirTcpAddress == null || mCirTcpAddress.length() == 0) {
                throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT,
                    "Missing CirTcpAddress");
            }
            try {
                mCirTcpPort = Integer.parseInt(values.get(KEY_CIR_TCP_PORT));
            } catch (NumberFormatException e) {
                throw new ImException(ImErrorInfo.INVALID_SESSION_CONTEXT,
                    "Invalid CirTcpPort");
            }
        } else if (mCurrentCirMethod == CirMethod.SHTTP) {
            mCirHttpAddress = values.get(KEY_CIR_HTTP_ADDRESS);
        }
    
Methods Summary
private PrimitivebuildCapabilityRequest()

        Primitive capabilityRequest = new Primitive(ImpsTags.ClientCapability_Request);
        PrimitiveElement list = capabilityRequest.addElement(ImpsTags.CapabilityList);
        list.addChild(ImpsTags.ClientType, ImpsClientCapability.getClientType());
        list.addChild(ImpsTags.AcceptedContentLength, Integer
                .toString(ImpsClientCapability.getAcceptedContentLength()));
        list.addChild(ImpsTags.ParserSize,
                Integer.toString(ImpsClientCapability.getParserSize()));
        list.addChild(ImpsTags.MultiTrans,
                Integer.toString(ImpsClientCapability.getMultiTrans()));

        // TODO: MultiTransPerMessage is IMPS 1.3
        //list.addChild(ImpsTags.MultiTransPerMessage,
        //        Integer.toString(ImpsClientCapability.getMultiTransPerMessage()));
        list.addChild(ImpsTags.InitialDeliveryMethod,
                ImpsClientCapability.getInitialDeliveryMethod());
        list.addChild(ImpsTags.ServerPollMin, Long.toString(mServerPollMin));

        for(TransportType supportedBear : ImpsClientCapability.getSupportedBearers()) {
            list.addChild(ImpsTags.SupportedBearer, supportedBear.toString());
        }

        for(CirMethod supportedCirMethod : ImpsClientCapability.getSupportedCirMethods()) {
            list.addChild(ImpsTags.SupportedCIRMethod, supportedCirMethod.toString());
            if (CirMethod.SUDP.equals(supportedCirMethod)) {
                list.addChild(ImpsTags.UDPPort,
                        Integer.toString(mConnection.getConfig().getUdpPort()));
            }
        }

        return capabilityRequest;
    
private PrimitivebuildServiceRequest()

        Primitive serviceRequest = new Primitive(ImpsTags.Service_Request);
        PrimitiveElement functions = serviceRequest.addElement(ImpsTags.Functions);
        PrimitiveElement features = functions.addChild(ImpsTags.WVCSPFeat);
        features.addChild(ImpsTags.FundamentalFeat);
        features.addChild(ImpsTags.PresenceFeat);
        features.addChild(ImpsTags.IMFeat);
        features.addChild(ImpsTags.GroupFeat);
        serviceRequest.addElement(ImpsTags.AllFunctionsRequest, true);
        return serviceRequest;
    
voidextractCapability(Primitive capabilityResponse)

        mSupportedCirMethod = new ArrayList<CirMethod>();

        PrimitiveElement agreedList = capabilityResponse.getContentElement()
                .getFirstChild();
        for (PrimitiveElement element : agreedList.getChildren()) {
            String tag = element.getTagName();
            if (tag.equals(ImpsTags.SupportedCIRMethod)) {
                try {
                    mSupportedCirMethod.add(CirMethod.valueOf(element.getContents()));
                } catch (IllegalArgumentException e) {
                    ImpsLog.log("Unrecognized CIR method " + element.getContents());
                }
            } else if (tag.equals(ImpsTags.TCPAddress)) {
                mCirTcpAddress = element.getContents();
            } else if (tag.equals(ImpsTags.TCPPort)) {
                mCirTcpPort = (int)ImpsUtils.parseLong(element.getContents(),
                        DEFAULT_TCP_PORT);
            } else if (tag.equals(ImpsTags.ServerPollMin)) {
                long defaultPollMin = mConnection.getConfig().getDefaultServerPollMin();
                mServerPollMin = ImpsUtils.parseLong(element.getContents(),
                        defaultPollMin);
                if (mServerPollMin <= 0) {
                    mServerPollMin = defaultPollMin;
                }
            } else if (tag.equals(ImpsTags.CIRHTTPAddress)
                    || tag.equals(ImpsTags.CIRURL)) {
                // This tag is CIRHTTPAddress in 1.3 and CIRURL in 1.2
                mCirHttpAddress = element.getChildContents(ImpsTags.URL);
            }
        }
    
public java.lang.StringgetCirHttpAddress()
Gets the URL used for standalone HTTP binding of CIR channel.

return
the URL.

        return mCirHttpAddress;
    
public java.lang.StringgetCirTcpAddress()
Gets the IP address for standalone TCP/IP CIR method.

return
the IP address for standalone TCP/IP CIR method

        return mCirTcpAddress;
    
public intgetCirTcpPort()
Gets the port number for the standalone TCP/IP CIR method.

return
the port number for the standalone TCP/IP CIR method.

        return mCirTcpPort;
    
public java.util.HashMapgetContext()

        HashMap<String, String> values = new HashMap<String, String>();

        values.put(KEY_SESSION_ID, mId);
        values.put(KEY_SESSION_COOKIE, mCookie);
        values.put(KEY_KEEP_ALIVE_TIME, Long.toString(mKeepAliveTime));
        values.put(KEY_USERNAME, mLoginInfo.getUserName());
        values.put(KEY_PASSWORD, mLoginInfo.getPassword());
        values.put(KEY_SERVER_POLL_MIN, Long.toString(mServerPollMin));

        values.put(KEY_CIR_METHOD, mCurrentCirMethod.name());
        if(mCurrentCirMethod == CirMethod.STCP) {
            values.put(KEY_CIR_TCP_ADDRESS, mCirTcpAddress);
            values.put(KEY_CIR_TCP_PORT, Integer.toString(mCirTcpPort));
        } else if (mCurrentCirMethod == CirMethod.SHTTP) {
            values.put(KEY_CIR_HTTP_ADDRESS, mCirHttpAddress);
        }
        return values;
    
public java.lang.StringgetCookie()

        return mCookie;
    
public com.android.im.imps.ImpsConnectionConfig.CirMethodgetCurrentCirMethod()

        return mCurrentCirMethod;
    
public java.lang.StringgetID()
Gets the unique id of the session.

return
the unique id of the session.

        return mId;
    
public longgetKeepAliveTime()
Gets the auto logout timer value.

return
the auto logout timer value.

        return mKeepAliveTime;
    
public com.android.im.engine.LoginInfogetLoginInfo()

        return mLoginInfo;
    
public com.android.im.engine.ContactgetLoginUser()

        return mLoginUser;
    
public ImpsUserAddressgetLoginUserAddress()

        return (ImpsUserAddress) mLoginUser.getAddress();
    
public java.lang.StringgetPassword()

        return mLoginInfo.getPassword();
    
public longgetServerPollMin()
Gets the minimum time interval (in seconds) that MUST pass before two subsequent PollingRequest transactions.

return
the minimum time interval in seconds.

        return mServerPollMin;
    
public PrimitiveElementgetServiceTree()
Gets the service tree of the features and functions that the server supports.

return
the service tree.

        return mServiceTree;
    
public java.util.ListgetSupportedCirMethods()
Gets a collection of CIR methods that are supported by both the client and the server.

return
a collection of supported CIR methods

        return mSupportedCirMethod;
    
public java.lang.StringgetUserName()

        return mLoginInfo.getUserName();
    
public booleanisCapablityRequestRequired()
Gets if further capability request is required in the session.

return
true if further capability request is required.

        return mCapablityRequest || mNew;
    
public voidnegotiateCapabilityAsync(AsyncCompletion completion)
Perform client capability negotiation with the server asynchronously.

param
completion Async completion object.

        Primitive capabilityRequest = buildCapabilityRequest();

        AsyncTransaction tx = new AsyncTransaction(
                mConnection.getTransactionManager(), completion) {

            @Override
            public void onResponseOk(Primitive response) {
                extractCapability(response);
            }

            @Override
            public void onResponseError(ImpsErrorInfo error) { }
        };

        tx.sendRequest(capabilityRequest);
    
public voidnegotiateServiceAsync(AsyncCompletion completion)
Perform service negotiation with the server asynchronously.

param
completion Async completion object.

        Primitive serviceRequest = buildServiceRequest();
        AsyncTransaction tx = new AsyncTransaction(
                mConnection.getTransactionManager(), completion) {

            @Override
            public void onResponseOk(Primitive response) {
                mServiceTree = response.getElement(ImpsTags.AllFunctions).getFirstChild();
            }

            @Override
            public void onResponseError(ImpsErrorInfo error) { }
        };

        tx.sendRequest(serviceRequest);
    
public voidsetCapablityRequestRequired(boolean required)

        mCapablityRequest = required;
    
public voidsetCurrentCirMethod(com.android.im.imps.ImpsConnectionConfig.CirMethod cirMethod)

        mCurrentCirMethod = cirMethod;
    
public voidsetId(java.lang.String id)

        mId = id;
    
public voidsetKeepAliveTime(long keepAliveTime)

        mKeepAliveTime = keepAliveTime;
    
private voidsetLoginInfo(com.android.im.engine.LoginInfo loginInfo)
Sets the Login information. After login successfully, the login information should be saved in the session context so that we can auto login when reconnect to the server.

param
loginInfo the login information.
throws
ImException

        try {
            ImpsAddress address = new ImpsUserAddress(loginInfo.getUserName());
            mLoginUser = new Contact(address, address.getScreenName());
            mLoginInfo = loginInfo;
        } catch (IllegalArgumentException e) {
            throw new ImException(ImErrorInfo.INVALID_USERNAME,
                    "Invalid username");
        }