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);
}
}
}
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;
}
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()));
}
}
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;
}
}
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)
}
}
}
Aggregations