Search in sources :

Example 1 with ResponseQueue

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

the class DownloadServerContactsTest method reportBackToEngine.

@Override
public void reportBackToEngine(int reqId, EngineId engine) {
    Log.d(LOG_TAG, "reportBackToEngine");
    ResponseQueue respQueue = ResponseQueue.getInstance();
    List<BaseDataType> data = new ArrayList<BaseDataType>();
    try {
        assertEquals(mEng.engineId(), engine);
        switch(mState) {
            case RUN_WITH_NO_CHANGES:
                reportBackWithNoChanges(reqId, data);
                break;
            case RUN_WITH_NEW_CONTACTS:
                reportBackWithNewContacts(reqId, data);
                break;
            case RUN_WITH_DELETED_CONTACTS:
                reportBackWithDeletedContacts(reqId, data);
                break;
            case RUN_WITH_MODIFIED_CONTACTS:
                reportBackWithModifiedContacts(reqId, data);
                break;
            case RUN_WITH_DELETED_DETAILS:
                reportBackWithDeletedDetails(reqId, data);
                break;
            default:
                fail("Unexpected request rom processor");
        }
    } catch (Throwable err) {
        ServerError serverError = new ServerError(ServerError.ErrorType.INTERNALERROR);
        serverError.errorDescription = err + "\n";
        for (int i = 0; i < err.getStackTrace().length; i++) {
            StackTraceElement v = err.getStackTrace()[i];
            serverError.errorDescription += "\t" + v + "\n";
        }
        Log.e(LOG_TAG, "Exception:\n" + serverError.errorDescription);
        data.clear();
        data.add(serverError);
    }
    respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
    mEng.onCommsInMessage();
    Log.d(LOG_TAG, "reportBackToEngine - message added to response queue");
}
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)

Example 2 with ResponseQueue

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

the class IdentityEngineTest method reportBackToEngine.

@Override
public void reportBackToEngine(int reqId, EngineId engine) {
    Log.d("TAG", "IdentityEngineTest.reportBackToEngine");
    ResponseQueue respQueue = ResponseQueue.getInstance();
    List<BaseDataType> data = new ArrayList<BaseDataType>();
    switch(mState) {
        case IDLE:
            break;
        case FETCH_IDENTITIES:
            Log.d("TAG", "IdentityEngineTest.reportBackToEngine FETCH ids");
            Identity id = new Identity();
            data.add(id);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.GET_AVAILABLE_IDENTITIES_RESPONSE.ordinal()));
            Log.d("TAG", "IdentityEngineTest.reportBackToEngine add to Q");
            mEng.onCommsInMessage();
            break;
        case GET_MY_IDENTITIES:
            Log.d("TAG", "IdentityEngineTest.reportBackToEngine Get ids");
            Identity myId = new Identity();
            data.add(myId);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.GET_AVAILABLE_IDENTITIES_RESPONSE.ordinal()));
            Log.d("TAG", "IdentityEngineTest.reportBackToEngine add to Q");
            mEng.onCommsInMessage();
            break;
        case FETCH_IDENTITIES_FAIL:
            ServerError err = new ServerError("Catastrophe");
            err.errorDescription = "Fail";
            data.add(err);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
            mEng.onCommsInMessage();
            break;
        case SET_IDENTITY_CAPABILTY:
            StatusMsg msg = new StatusMsg();
            msg.mCode = "ok";
            msg.mDryRun = false;
            msg.mStatus = true;
            data.add(msg);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.SET_IDENTITY_CAPABILITY_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            break;
        case VALIDATE_ID_CREDENTIALS_SUCCESS:
            StatusMsg msg2 = new StatusMsg();
            msg2.mCode = "ok";
            msg2.mDryRun = false;
            msg2.mStatus = true;
            data.add(msg2);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.VALIDATE_IDENTITY_CREDENTIALS_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            break;
        case VALIDATE_ID_CREDENTIALS_FAIL:
            ServerError err2 = new ServerError("Catastrophe");
            err2.errorDescription = "Fail";
            data.add(err2);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
            mEng.onCommsInMessage();
            break;
        case GET_NEXT_RUNTIME:
            break;
        case GET_CHATABLE_IDENTITIES:
        case FETCH_IDENTITIES_POPULATED:
            Identity id2 = new Identity();
            id2.mActive = true;
            id2.mAuthType = "auth";
            List<String> clist = new ArrayList<String>();
            clist.add("uk");
            clist.add("fr");
            id2.mCountryList = clist;
            id2.mCreated = new Long(0);
            id2.mDisplayName = "Facebook";
            // id2.mIcon2Mime = "jpeg";
            id2.mIconMime = "jpeg";
            try {
                id2.mIcon2Url = new URL("url2");
                id2.mIconUrl = new URL("url");
                id2.mNetworkUrl = new URL("network");
            } catch (Exception e) {
            }
            id2.mIdentityId = "fb";
            id2.mIdentityType = "type";
            id2.mName = "Facebook";
            id2.mNetwork = "Facebook";
            id2.mOrder = 0;
            id2.mPluginId = "";
            id2.mUpdated = new Long(0);
            id2.mUserId = 23;
            id2.mUserName = "user";
            data.add(id2);
            List<IdentityCapability> capList = new ArrayList<IdentityCapability>();
            IdentityCapability idcap = new IdentityCapability();
            idcap.mCapability = IdentityCapability.CapabilityID.sync_contacts;
            idcap.mDescription = "sync cont";
            idcap.mName = "sync cont";
            idcap.mValue = true;
            capList.add(idcap);
            id2.mCapabilities = capList;
            data.add(id2);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.GET_AVAILABLE_IDENTITIES_RESPONSE.ordinal()));
            Log.d("TAG", "IdentityEngineTest.reportBackToEngine add to Q");
            mEng.onCommsInMessage();
            break;
        default:
    }
}
Also used : DecodedResponse(com.vodafone360.people.service.io.ResponseQueue.DecodedResponse) StatusMsg(com.vodafone360.people.datatypes.StatusMsg) ServerError(com.vodafone360.people.datatypes.ServerError) ArrayList(java.util.ArrayList) URL(java.net.URL) IdentityCapability(com.vodafone360.people.datatypes.IdentityCapability) BaseDataType(com.vodafone360.people.datatypes.BaseDataType) ResponseQueue(com.vodafone360.people.service.io.ResponseQueue) Identity(com.vodafone360.people.datatypes.Identity)

Example 3 with ResponseQueue

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

the class ActivitiesEngineTest method reportBackToEngine.

@Override
public void reportBackToEngine(int reqId, EngineId engine) {
    Log.d("TAG", "IdentityEngineTest.reportBackToEngine");
    ResponseQueue respQueue = ResponseQueue.getInstance();
    List<BaseDataType> data = new ArrayList<BaseDataType>();
    switch(mState) {
        case IDLE:
            break;
        case ON_CREATE:
        case ON_DESTROY:
            break;
        case GET_ACTIVITIES_SUCCESS:
            ActivityItem item = new ActivityItem();
            data.add(item);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.GET_ACTIVITY_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            break;
        case GET_TIMELINE_EVENT_FROM_SERVER:
            ActivityItem item2 = new ActivityItem();
            ActivityContact act = new ActivityContact();
            act.mName = "Bill Fleege";
            act.mLocalContactId = new Long(8);
            List<ActivityContact> clist = new ArrayList<ActivityContact>();
            clist.add(act);
            item2.contactList = clist;
            item2.activityFlags = 2;
            item2.type = ActivityItem.Type.CONTACT_JOINED;
            item2.time = System.currentTimeMillis();
            data.add(item2);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.GET_ACTIVITY_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            break;
        case GET_POPULATED_ACTIVITIES:
            ActivityItem item3 = new ActivityItem();
            ActivityContact act2 = new ActivityContact();
            act2.mName = "Bill Fleege";
            act2.mLocalContactId = new Long(8);
            List<ActivityContact> clist2 = new ArrayList<ActivityContact>();
            clist2.add(act2);
            item3.contactList = clist2;
            item3.activityFlags = 2;
            item3.type = ActivityItem.Type.CONTACT_JOINED;
            item3.time = System.currentTimeMillis();
            item3.title = "bills new status";
            item3.description = "a description";
            data.add(item3);
            ActivityItem item4 = new ActivityItem();
            item4.contactList = clist2;
            item4.activityFlags = 5;
            item4.type = ActivityItem.Type.CONTACT_JOINED;
            item4.time = System.currentTimeMillis();
            item4.title = "bills new status";
            item4.description = "a description";
            item4.activityId = new Long(23);
            item4.hasChildren = false;
            item4.uri = "uri";
            item4.parentActivity = new Long(0);
            item4.preview = ByteBuffer.allocate(46);
            item4.preview.position(0);
            item4.preview.rewind();
            for (int i = 0; i < 23; i++) {
                item4.preview.putChar((char) i);
            }
            item4.previewMime = "jepg";
            item4.previewUrl = "storeurl";
            item4.store = "google";
            item4.visibilityFlags = 0;
            data.add(item4);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.GET_ACTIVITY_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            break;
        case GET_ACTIVITIES_SERVER_ERR:
            ServerError err = new ServerError("Catastrophe");
            err.errorDescription = "Fail";
            data.add(err);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
            mEng.onCommsInMessage();
            break;
        case GET_ACTIVITIES_UNEXPECTED_RESPONSE:
            StatusMsg msg = new StatusMsg();
            msg.mCode = "ok";
            msg.mDryRun = false;
            msg.mStatus = true;
            data.add(msg);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.LOGIN_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            break;
        case SET_STATUS:
            Identity id3 = new Identity();
            data.add(id3);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.GET_AVAILABLE_IDENTITIES_RESPONSE.ordinal()));
            mEng.onCommsInMessage();
            break;
        case ON_SYNC_COMPLETE:
            ServerError err2 = new ServerError("Catastrophe");
            err2.errorDescription = "Fail";
            data.add(err2);
            respQueue.addToResponseQueue(new DecodedResponse(reqId, data, engine, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
            mEng.onCommsInMessage();
            break;
        case GET_NEXT_RUNTIME:
            break;
        default:
    }
}
Also used : DecodedResponse(com.vodafone360.people.service.io.ResponseQueue.DecodedResponse) StatusMsg(com.vodafone360.people.datatypes.StatusMsg) ServerError(com.vodafone360.people.datatypes.ServerError) ArrayList(java.util.ArrayList) ActivityContact(com.vodafone360.people.datatypes.ActivityContact) BaseDataType(com.vodafone360.people.datatypes.BaseDataType) ResponseQueue(com.vodafone360.people.service.io.ResponseQueue) Identity(com.vodafone360.people.datatypes.Identity) ActivityItem(com.vodafone360.people.datatypes.ActivityItem)

Example 4 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) BaseDataType(com.vodafone360.people.datatypes.BaseDataType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 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)

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