Search in sources :

Example 71 with Request

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

the class HttpConnectionThread method run.

/**
 * <p>
 * Sends out synchronous requests (for authentication) to the API and
 * asynchronous calls to the RPG as soon as there are requests on the
 * request queue.
 * </p>
 * <p>
 * If there are no requests the thread is set to wait().
 * </p>
 */
public void run() {
    AuthenticationManager authMgr = null;
    authMgr = new AuthenticationManager(this);
    while (mIsConnectionRunning) {
        // loops through requests and sends them
        // out as needed
        // TODO move this out. this should be
        authMgr.handleAuthRequests();
        if (null != mPollThread) {
            if (mIsFirstTimePoll) {
                try {
                    mPollThread.invokePoll(PollThread.SHORT_POLLING_INTERVAL, PollThread.DEFAULT_BATCHSIZE, PollThread.ACTIVE_MODE);
                } catch (Exception e) {
                    // we do not do anything here as it is not a critical
                    // error
                    logI("RpgHttpConnection.run()", "Exception while doing 1st time poll!!");
                } finally {
                    mIsFirstTimePoll = false;
                }
            }
            List<Request> requests = QueueManager.getInstance().getApiRequests();
            if ((requests.size() > 0) && mPollThread.getHasCoverage()) {
                if (null == mRpgUrl) {
                    // once we have a proper authMgr
                    try {
                        mRpgUrl = new URL(SettingsManager.getProperty(Settings.RPG_SERVER_KEY) + LoginEngine.getSession().userID).toURI();
                    } catch (Exception e) {
                        logE("RpgHttpConnectionThread.run()", "Could not set up URL", e);
                    }
                }
                mRetryCount = 0;
                List<Integer> reqIds = new ArrayList<Integer>();
                try {
                    byte[] reqData = prepareRPGRequests(requests, reqIds);
                    if (null != LoginEngine.getSession()) {
                        synchronized (mSendLock) {
                            if (mIsConnectionRunning) {
                                if (Settings.sEnableProtocolTrace) {
                                    HttpConnectionThread.logI("RpgTcpConnectionThread.run()", "\n \n \nSending a request: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + HessianUtils.getInHessian(new ByteArrayInputStream(reqData), true));
                                }
                                HttpResponse response = postHTTPRequest(reqData, mRpgUrl, Settings.HTTP_HEADER_CONTENT_TYPE);
                                if (mIsConnectionRunning && SettingsManager.getBooleanProperty(Settings.ENABLE_RPG_KEY) && handleRpgResponse(response, reqIds)) {
                                    mPollThread.startRpgPolling();
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    // add error to the
                    addErrorToResponseQueue(reqIds);
                // response queue
                }
            }
        }
        try {
            synchronized (mRunLock) {
                mRunLock.wait();
            }
        } catch (InterruptedException ie) {
            LogUtils.logE("HttpConnectionThread.run() Wait was interrupted", ie);
        }
    }
}
Also used : AuthenticationManager(com.vodafone360.people.service.transport.http.authentication.AuthenticationManager) ByteArrayInputStream(java.io.ByteArrayInputStream) Request(com.vodafone360.people.service.io.Request) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URL(java.net.URL)

Example 72 with Request

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

the class HttpConnectionThread method prepareRPGRequests.

/**
 * Takes all requests objects and writes its serialized data to a byte array
 * for further posting to the RPG.
 *
 * @param requests A list of requests to serialize.
 * @param reqIds
 * @return The serialized requests with RPG headers. Returns NULL id list of requests is NULL.
 */
private byte[] prepareRPGRequests(List<Request> requests, List<Integer> reqIds) {
    if (null == requests) {
        return null;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        for (Request request : requests) {
            request.writeToOutputStream(baos, true);
            request.setActive(true);
            reqIds.add(request.getRequestId());
        }
        baos.flush();
    } catch (IOException ioe) {
        LogUtils.logE("HttpConnectionThread.prepareRPGRequests() Failed writing to BAOS", ioe);
    } finally {
        try {
            baos.close();
        } catch (IOException ioe) {
            LogUtils.logE("HttpConnectionThread.prepareRPGRequests() Failed closing BAOS", ioe);
        }
    }
    byte[] reqData = baos.toByteArray();
    return reqData;
}
Also used : Request(com.vodafone360.people.service.io.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 73 with Request

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

the class HttpConnectionThread method addErrorToResponseQueue.

/**
 * Adds errors to the response queue whenever there is an HTTP error on the
 * backend.
 *
 * @param reqIds The request IDs the error happened for.
 */
public void addErrorToResponseQueue(List<Integer> reqIds) {
    EngineId source = null;
    QueueManager requestQueue = QueueManager.getInstance();
    ResponseQueue responseQueue = ResponseQueue.getInstance();
    for (Integer reqId : reqIds) {
        // attempt to get type from request
        Request req = requestQueue.getRequest(reqId);
        if (req != null)
            source = req.mEngineId;
        responseQueue.addToResponseQueue(new DecodedResponse(reqId, null, source, DecodedResponse.ResponseType.SERVER_ERROR.ordinal()));
    }
}
Also used : DecodedResponse(com.vodafone360.people.service.io.ResponseQueue.DecodedResponse) EngineId(com.vodafone360.people.engine.EngineManager.EngineId) Request(com.vodafone360.people.service.io.Request) ResponseQueue(com.vodafone360.people.service.io.ResponseQueue) QueueManager(com.vodafone360.people.service.io.QueueManager)

Example 74 with Request

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

the class HeartbeatSenderThread method sendHeartbeat.

/**
 * Prepares the necessary Hessian payload and writes it directly to the open
 * output-stream of the socket.
 *
 * @throws Exception Thrown if there was an unknown problem writing to the
 *             output-stream.
 * @throws IOException Thrown if there was a problem regarding IO while
 *             writing to the output-stream.
 */
public void sendHeartbeat() throws IOException, Exception {
    byte[] rpgMsg = null;
    try {
        rpgMsg = getHeartbeatHessianPayload();
    } catch (NullPointerException e) {
        // Stop connnection and log out
        ConnectionManager.getInstance().onLoginStateChanged(false);
        EngineManager.getInstance().getLoginEngine().logoutAndRemoveUser();
    }
    try {
        // Try and issue the request
        if (Settings.sEnableProtocolTrace) {
            Long userID = null;
            AuthSessionHolder auth = LoginEngine.getSession();
            if (auth != null) {
                userID = auth.userID;
            }
            HttpConnectionThread.logI("RpgTcpHeartbeatSender.sendHeartbeat()", "\n  * Sending a heartbeat for user ID " + userID + "----------------------------------------" + HessianUtils.getInHessian(new ByteArrayInputStream(rpgMsg), true) + "\n");
        }
        if (null != mOs) {
            synchronized (mOs) {
                mOs.write(rpgMsg);
                mOs.flush();
            }
        }
    } catch (IOException ioe) {
        HttpConnectionThread.logE("RpgTcpHeartbeatSender.sendHeartbeat()", "Could not write HB to OS!", ioe);
        throw ioe;
    } catch (Exception e) {
        HttpConnectionThread.logE("RpgTcpHeartbeatSender.sendHeartbeat()", "Could not send HB to OS! Unknown: ", e);
        throw e;
    } finally {
        rpgMsg = null;
    }
}
Also used : AuthSessionHolder(com.vodafone360.people.datatypes.AuthSessionHolder) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 75 with Request

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

the class TimeOutWatcher method invalidateAllRequests.

/**
 * Sends a timeout event for all the requests.
 */
public void invalidateAllRequests() {
    synchronized (QueueManager.getInstance().lock) {
        LogUtils.logV("TimeOutWatcher.invalidateAllRequests()");
        if (mRequests == null)
            return;
        while (mRequests.size() > 0) {
            final Request request = mRequests.get(0);
            LogUtils.logV("TimeOutWatcher.invalidateAllRequests(): " + "forcing a timeout for reqId=[" + request.getRequestId() + "] and timeout=" + request.getTimeout() + " milliseconds");
            fireRequestExpired(request);
        // no need to remove the request, this happened during previous
        // method call... (removeRequest is called when adding a
        // response)
        }
    }
}
Also used : Request(com.vodafone360.people.service.io.Request)

Aggregations

Request (com.vodafone360.people.service.io.Request)43 QueueManager (com.vodafone360.people.service.io.QueueManager)27 ServiceStatus (com.vodafone360.people.service.ServiceStatus)16 ArrayList (java.util.ArrayList)16 BaseDataType (com.vodafone360.people.datatypes.BaseDataType)12 ServiceUiRequest (com.vodafone360.people.service.ServiceUiRequest)12 DecodedResponse (com.vodafone360.people.service.io.ResponseQueue.DecodedResponse)11 Suppress (android.test.suitebuilder.annotation.Suppress)10 DatabaseHelper (com.vodafone360.people.database.DatabaseHelper)9 IEngineEventCallback (com.vodafone360.people.engine.IEngineEventCallback)9 IContactSyncCallback (com.vodafone360.people.engine.contactsync.IContactSyncCallback)9 ProcessorFactory (com.vodafone360.people.engine.contactsync.ProcessorFactory)9 ServerError (com.vodafone360.people.datatypes.ServerError)6 IOException (java.io.IOException)6 Identity (com.vodafone360.people.datatypes.Identity)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Bundle (android.os.Bundle)4 AuthSessionHolder (com.vodafone360.people.datatypes.AuthSessionHolder)4 ContactChanges (com.vodafone360.people.datatypes.ContactChanges)4 ResponseQueue (com.vodafone360.people.service.io.ResponseQueue)4