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

ImpsTransactionManager

public class ImpsTransactionManager extends Object

Fields Summary
private ImpsConnection
mConnection
private int
mTransactionId
private HashMap
mClientTransactions
Keep track of the client initialized transactions.
private HashMap
mServerTransactionListeners
Constructors Summary
ImpsTransactionManager(ImpsConnection connection)
Constructs an instance of transaction manager.

        this.mConnection = connection;
        mClientTransactions = new HashMap<String, AsyncTransaction>();
        mServerTransactionListeners = new HashMap<String, ServerTransactionListener>();
    
Methods Summary
voidbeginClientTransaction(AsyncTransaction tx)
Originates an async transaction from the client.

param
tx

        synchronized(mClientTransactions) {
            tx.setTransactionInfo(nextTransactionId(), mConnection);
            mClientTransactions.put(tx.getId(), tx);
        }
    
voidendClientTransaction(AsyncTransaction tx)
Terminates a transaction which was originated from the client.

param
tx the transaction to terminate.

        synchronized(mClientTransactions) {
            mClientTransactions.remove(tx.getId());
        }
    
private synchronized java.lang.StringnextTransactionId()
Generates a new transaction ID.

return
a new transaction ID.

        if(mTransactionId >= 999) {
            mTransactionId = 0;
        }
        return String.valueOf(++mTransactionId);
    
public voidnotifyErrorResponse(java.lang.String transactionId, int code, java.lang.String info)
TODO: This should not be called from the DataChannel thread.

param
transactionId
param
code
param
info

        AsyncTransaction tx;
        synchronized(mClientTransactions) {
            tx = mClientTransactions.get(transactionId);
        }
        if (tx != null) {
            tx.notifyError(new ImErrorInfo(code, info));
        } else {
            ImpsLog.log("Ignoring possible server transaction error " + code + info);
        }
    
public voidnotifyIncomingPrimitive(Primitive primitive)
Notifies the TransactionManager that a new primitive from the server has arrived.

param
primitive the incoming primitive.

        String transactionId = primitive.getTransactionID();
        if (primitive.getTransactionMode() == TransactionMode.Response) {

            AsyncTransaction tx;
            synchronized(mClientTransactions) {
                tx = mClientTransactions.get(transactionId);
            }
            // The transaction might has been terminated by the client,
            // just ignore the incoming primitive in that case.
            if (tx != null) {
                tx.notifyResponse(primitive);
            }
        } else {
            ServerTransaction serverTx = new ServerTransaction(transactionId,
                    mConnection, primitive);

            ServerTransactionListener listener;
            synchronized(mServerTransactionListeners) {
                listener = mServerTransactionListeners.get(primitive.getType());
            }
            if (listener != null) {
                listener.notifyServerTransaction(serverTx);
            } else {
                ImpsLog.log("Unhandled Server transaction: " + primitive.getType());
            }
        }
    
voidreassignTransactionId(Primitive p)

        synchronized (mClientTransactions) {
            AsyncTransaction tx = mClientTransactions.remove(p.getTransactionID());
            if(tx != null) {
                String newId = nextTransactionId();
                tx.setTransactionInfo(newId, mConnection);
                p.setTransactionId(newId);
                mClientTransactions.put(newId, tx);
            }
        }
    
public voidsetTransactionListener(java.lang.String type, ServerTransactionListener listener)
Sets a ServerTransactionListener on this manager so that it will be notified when a specified transaction has been initialized by the server.

param
type the primitive type of the transaction.
param
listener the ServerTransactionListener to be notified, or null to clear the listener on specified type.

        synchronized(mServerTransactionListeners) {
            if (listener == null) {
                mServerTransactionListeners.remove(type);
            } else {
                mServerTransactionListeners.put(type, listener);
            }
        }