Search in sources :

Example 6 with ResponseQueue

use of com.vodafone360.people.service.io.ResponseQueue in project 360-Engine-for-Android by 360.

the class DecoderThread method run.

/**
 * Thread's run function If the decoding queue contains any entries we
 * decode the first response and add the decoded data to the response queue.
 * If the decode queue is empty, the thread will become inactive. It is
 * resumed when a raw data entry is added to the decode queue.
 */
public void run() {
    LogUtils.logI("DecoderThread.run() [Start thread]");
    while (mRunning) {
        EngineId engineId = EngineId.UNDEFINED;
        Type type = Type.PUSH_MSG;
        int reqId = -1;
        try {
            if (mResponses.size() > 0) {
                LogUtils.logI("DecoderThread.run() Decoding [" + mResponses.size() + "x] responses");
                // Decode first entry in queue
                RawResponse decode = mResponses.get(0);
                reqId = decode.mReqId;
                if (!decode.mIsPushMessage) {
                    // Attempt to get type from request
                    Request request = QueueManager.getInstance().getRequest(reqId);
                    if (request != null) {
                        type = request.mType;
                        engineId = request.mEngineId;
                        long backendResponseTime = decode.mTimeStamp - request.getAuthTimestamp();
                        LogUtils.logD("Backend response time was " + backendResponseTime + "ms");
                    } else {
                        type = Type.COMMON;
                    }
                }
                DecodedResponse response = mHessianDecoder.decodeHessianByteArray(reqId, decode.mData, type, decode.mIsCompressed, engineId);
                // if we have a push message let's try to find out to which engine it should be routed
                if ((response.getResponseType() == DecodedResponse.ResponseType.PUSH_MESSAGE.ordinal()) && (response.mDataTypes.get(0) != null)) {
                    // for push messages we have to override the engine id as it is parsed inside the hessian decoder
                    engineId = ((PushEvent) response.mDataTypes.get(0)).mEngineId;
                    response.mSource = engineId;
                // TODO mSource should get the engineId inside the decoder once types for mDataTypes is out. see PAND-1805.
                }
                // the request ID.
                if (type == Type.PUSH_MSG && reqId != 0 && engineId == EngineId.UNDEFINED) {
                    Request request = QueueManager.getInstance().getRequest(reqId);
                    if (request != null) {
                        engineId = request.mEngineId;
                    }
                }
                if (engineId == EngineId.UNDEFINED) {
                    LogUtils.logE("DecoderThread.run() Unknown engine for message with type[" + type.name() + "]");
                // TODO: Throw Exception for undefined messages, as
                // otherwise they might always remain on the Queue?
                }
                // Add data to response queue
                HttpConnectionThread.logV("DecoderThread.run()", "Add message[" + decode.mReqId + "] to ResponseQueue for engine[" + engineId + "] with data [" + response.mDataTypes + "]");
                mRespQueue.addToResponseQueue(response);
                // Remove item from our list of responses.
                mResponses.remove(0);
                // be nice to the other threads
                Thread.sleep(THREAD_SLEEP_TIME);
            } else {
                synchronized (this) {
                    // No waiting responses, so the thread should sleep.
                    try {
                        LogUtils.logV("DecoderThread.run() [Waiting for more responses]");
                        wait();
                    } catch (InterruptedException ie) {
                    // Do nothing
                    }
                }
            }
        } catch (Throwable t) {
            /*
                 * Keep thread running regardless of error. When something goes
                 * wrong we should remove response from queue and report error
                 * back to engine.
                 */
            if (mResponses.size() > 0) {
                mResponses.remove(0);
            }
            if (type != Type.PUSH_MSG && engineId != EngineId.UNDEFINED) {
                List<BaseDataType> list = new ArrayList<BaseDataType>();
                // this error type was chosen to make engines remove request
                // or retry
                // we may consider using other error code later
                ServerError error = new ServerError(ServerError.ErrorType.INTERNALERROR);
                error.errorDescription = "Decoder thread was unable to decode server message";
                list.add(error);
                mRespQueue.addToResponseQueue(new DecodedResponse(reqId, list, engineId, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
            }
            LogUtils.logE("DecoderThread.run() Throwable on reqId[" + reqId + "]", t);
        }
    }
    LogUtils.logI("DecoderThread.run() [End thread]");
}
Also used : DecodedResponse(com.vodafone360.people.service.io.ResponseQueue.DecodedResponse) EngineId(com.vodafone360.people.engine.EngineManager.EngineId) ServerError(com.vodafone360.people.datatypes.ServerError) Request(com.vodafone360.people.service.io.Request) BaseDataType(com.vodafone360.people.datatypes.BaseDataType) Type(com.vodafone360.people.service.io.Request.Type) ArrayList(java.util.ArrayList) List(java.util.List) BaseDataType(com.vodafone360.people.datatypes.BaseDataType)

Example 7 with ResponseQueue

use of com.vodafone360.people.service.io.ResponseQueue in project 360-Engine-for-Android by 360.

the class RequestQueue method clearActiveRequests.

/**
 * Return the current (i.e. most recently generated) request id.
 *
 * @return the current request id.
 */
/*
     * public synchronized int getCurrentId(){ return mCurrentRequestId; }
     */
/**
 * Clear active requests (i.e add dummy response to response queue).
 *
 * @param rpgOnly
 */
protected void clearActiveRequests(boolean rpgOnly) {
    synchronized (QueueManager.getInstance().lock) {
        ResponseQueue rQ = ResponseQueue.getInstance();
        for (int i = 0; i < mRequests.size(); i++) {
            Request request = mRequests.get(i);
            if (request.isActive() && (!rQ.responseExists(request.getRequestId()))) {
                if (!rpgOnly || (rpgOnly && ((request.getAuthenticationType() == Request.USE_RPG) || (request.getAuthenticationType() == Request.USE_BOTH)))) {
                    LogUtils.logE("RequestQueue.clearActiveRequests() Deleting request " + request.getRequestId());
                    mRequests.remove(i);
                    // necessarily times out before)
                    if (request.getExpiryDate() > 0) {
                        mTimeOutWatcher.removeRequest(request);
                    }
                    i--;
                    rQ.addToResponseQueue(new DecodedResponse(request.getRequestId(), null, request.mEngineId, DecodedResponse.ResponseType.TIMED_OUT_RESPONSE.ordinal()));
                }
            }
        }
    }
}
Also used : DecodedResponse(com.vodafone360.people.service.io.ResponseQueue.DecodedResponse)

Example 8 with ResponseQueue

use of com.vodafone360.people.service.io.ResponseQueue in project 360-Engine-for-Android by 360.

the class EngineManager method onCommsInMessage.

/**
 * Respond to incoming message received from Comms layer. If this message
 * has a valid engine id it is routed to that engine, otherwise The
 * {@link EngineManager} will try to get the next response.
 *
 * @param source EngineId associated with incoming message.
 */
public void onCommsInMessage(EngineId source) {
    BaseEngine engine = null;
    if (source != null) {
        engine = mEngineList.get(source.ordinal());
    }
    if (engine != null) {
        engine.onCommsInMessage();
    } else {
        LogUtils.logE("EngineManager.onCommsInMessage - " + "Cannot dispatch message, unknown source " + source);
        final ResponseQueue queue = ResponseQueue.getInstance();
        queue.getNextResponse(source);
    }
}
Also used : ResponseQueue(com.vodafone360.people.service.io.ResponseQueue)

Example 9 with ResponseQueue

use of com.vodafone360.people.service.io.ResponseQueue in project 360-Engine-for-Android by 360.

the class LoginEngineTest method reportBackToEngine.

@Override
public void reportBackToEngine(int reqId, EngineId engine) {
    Log.d("TAG", "LoginEngineTest.reportBackToEngine");
    ResponseQueue respQueue = ResponseQueue.getInstance();
    List<BaseDataType> data = new ArrayList<BaseDataType>();
    switch(mState) {
        case IDLE:
            break;
        case LOGIN_REQUEST:
        case SMS_RESPONSE_SIGNIN:
            Log.d("TAG", "IdentityEngineTest.reportBackToEngine FETCH ids");
            StatusMsg msg = new StatusMsg();
            data.add(msg);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.LOGIN_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            mState = LoginTestState.LOGIN_REQUEST_VALID;
            break;
        case LOGIN_REQUEST_VALID:
            Log.d("TAG", "IdentityEngineTest.reportBackToEngine FETCH ids");
            AuthSessionHolder sesh = new AuthSessionHolder();
            data.add(sesh);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.LOGIN_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            mState = LoginTestState.SMS_RESPONSE_SIGNIN;
            break;
        case REGISTRATION:
            Log.d("TAG", "IdentityEngineTest.reportBackToEngine Registration");
            data.add(mTestModule.createDummyContactData());
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.SIGNUP_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            break;
        case REGISTRATION_ERROR:
            data.add(new ServerError(ServerError.ErrorType.UNKNOWN));
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
            mEng.onCommsInMessage();
            break;
        case GET_T_AND_C:
        case FETCH_PRIVACY:
        case USER_NAME_STATE:
            SimpleText txt = new SimpleText();
            txt.addText("Simple text");
            data.add(txt);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.GET_T_AND_C_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            break;
        case SERVER_ERROR:
            data.add(new ServerError(ServerError.ErrorType.UNKNOWN));
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
            mEng.onCommsInMessage();
            break;
        default:
    }
}
Also used : DecodedResponse(com.vodafone360.people.service.io.ResponseQueue.DecodedResponse) AuthSessionHolder(com.vodafone360.people.datatypes.AuthSessionHolder) StatusMsg(com.vodafone360.people.datatypes.StatusMsg) SimpleText(com.vodafone360.people.datatypes.SimpleText) ServerError(com.vodafone360.people.datatypes.ServerError) ArrayList(java.util.ArrayList) BaseDataType(com.vodafone360.people.datatypes.BaseDataType) ResponseQueue(com.vodafone360.people.service.io.ResponseQueue)

Example 10 with ResponseQueue

use of com.vodafone360.people.service.io.ResponseQueue in project 360-Engine-for-Android by 360.

the class EngineTestFramework method reportBackToFramework.

@Override
public void reportBackToFramework(int reqId, EngineId engine) {
    Log.d("TAG", "EngineTestFramework.reportBackToFramework");
    mObserver.reportBackToEngine(reqId, engine);
    final QueueManager reqQ = QueueManager.getInstance();
    final ResponseQueue respQ = ResponseQueue.getInstance();
    if (reqQ.getRequest(reqId) != null) {
        List<BaseDataType> dataTypeList = new ArrayList<BaseDataType>();
        ServerError err = new ServerError(ServerError.ErrorType.UNKNOWN);
        dataTypeList.add(err);
        respQ.addToResponseQueue(new DecodedResponse(reqId, dataTypeList, engine, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
    }
}
Also used : DecodedResponse(com.vodafone360.people.service.io.ResponseQueue.DecodedResponse) ServerError(com.vodafone360.people.datatypes.ServerError) ArrayList(java.util.ArrayList) BaseDataType(com.vodafone360.people.datatypes.BaseDataType) ResponseQueue(com.vodafone360.people.service.io.ResponseQueue) QueueManager(com.vodafone360.people.service.io.QueueManager)

Aggregations

DecodedResponse (com.vodafone360.people.service.io.ResponseQueue.DecodedResponse)11 ResponseQueue (com.vodafone360.people.service.io.ResponseQueue)10 BaseDataType (com.vodafone360.people.datatypes.BaseDataType)8 ServerError (com.vodafone360.people.datatypes.ServerError)8 ArrayList (java.util.ArrayList)8 StatusMsg (com.vodafone360.people.datatypes.StatusMsg)3 Identity (com.vodafone360.people.datatypes.Identity)2 EngineId (com.vodafone360.people.engine.EngineManager.EngineId)2 QueueManager (com.vodafone360.people.service.io.QueueManager)2 Request (com.vodafone360.people.service.io.Request)2 ActivityContact (com.vodafone360.people.datatypes.ActivityContact)1 ActivityItem (com.vodafone360.people.datatypes.ActivityItem)1 AuthSessionHolder (com.vodafone360.people.datatypes.AuthSessionHolder)1 IdentityCapability (com.vodafone360.people.datatypes.IdentityCapability)1 SimpleText (com.vodafone360.people.datatypes.SimpleText)1 Type (com.vodafone360.people.service.io.Request.Type)1 URL (java.net.URL)1 List (java.util.List)1