use of com.vodafone360.people.service.transport.DecoderThread.RawResponse in project 360-Engine-for-Android by 360.
the class DecoderThread method handleResponse.
/**
* <p>
* Looks at the response and adds it to the necessary decoder.
* </p>
* TODO: this method should be worked on. The decoder should take care of
* deciding which methods are decoded in which way.
*
* @param response The server response to decode.
* @throws Exception Thrown if the returned status line was null or if the
* response was null.
*/
public void handleResponse(byte[] response) throws Exception {
InputStream is = null;
if (response != null) {
try {
is = new ByteArrayInputStream(response);
final List<RpgMessage> mRpgMessages = new ArrayList<RpgMessage>();
// Get array of RPG messages
// throws IO Exception, we pass it to the calling method
RpgHelper.splitRpgResponse(is, mRpgMessages);
byte[] body = null;
RpgHeader rpgHeader = null;
// Process each header
for (RpgMessage mRpgMessage : mRpgMessages) {
body = mRpgMessage.body();
rpgHeader = mRpgMessage.header();
// Determine RPG mssageType (internal response, push
// etc)
final int mMessageType = rpgHeader.reqType();
HttpConnectionThread.logD("DecoderThread.handleResponse()", "Non-RPG_POLL_MESSAGE");
// Reset blank header counter
final boolean mZipped = mRpgMessage.header().compression();
if (body != null && (body.length > 0)) {
switch(mMessageType) {
case RpgMessageTypes.RPG_EXT_RESP:
// External message response
HttpConnectionThread.logD("DecoderThread.handleResponse()", "RpgMessageTypes.RPG_EXT_RESP - " + "Add External Message RawResponse to Decode queue:" + rpgHeader.reqId() + "mBody.len=" + body.length);
addToDecode(new RawResponse(rpgHeader.reqId(), body, mZipped, false));
break;
case RpgMessageTypes.RPG_PUSH_MSG:
// Define push message callback to
// notify controller
HttpConnectionThread.logD("DecoderThread.handleResponse()", "RpgMessageTypes.RPG_PUSH_MSG - Add Push " + "Message RawResponse to Decode queue:" + 0 + "mBody.len=" + body.length);
addToDecode(new RawResponse(rpgHeader.reqId(), body, mZipped, true));
break;
case RpgMessageTypes.RPG_INT_RESP:
// Internal message response
HttpConnectionThread.logD("DecoderThread.handleResponse()", "RpgMessageTypes.RPG_INT_RESP - Add RawResponse to Decode queue:" + rpgHeader.reqId() + "mBody.len=" + body.length);
addToDecode(new RawResponse(rpgHeader.reqId(), body, mZipped, false));
break;
case RpgMessageTypes.RPG_PRESENCE_RESPONSE:
HttpConnectionThread.logD("DecoderThread.handleResponse()", "RpgMessageTypes.RPG_PRESENCE_RESPONSE - " + "Add RawResponse to Decode queue - mZipped[" + mZipped + "]" + "mBody.len=" + body.length);
addToDecode(new RawResponse(rpgHeader.reqId(), body, mZipped, false));
break;
default:
// error to the responsedecoder
break;
}
}
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ioe) {
HttpConnectionThread.logE("DecoderThread.handleResponse()", "Could not close IS: ", ioe);
} finally {
is = null;
}
}
}
}
}
use of com.vodafone360.people.service.transport.DecoderThread.RawResponse 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]");
}
use of com.vodafone360.people.service.transport.DecoderThread.RawResponse in project 360-Engine-for-Android by 360.
the class HttpConnectionThread method addToDecoder.
/**
* Adds a response to the response decoder.
*
* @param input The data of the response.
* @param reqIds The request IDs that a response was received for.
*/
private void addToDecoder(byte[] input, List<Integer> reqIds) {
if (input != null && mDecoder != null) {
int reqId = reqIds.size() > 0 ? reqIds.get(0) : 0;
mDecoder.addToDecode(new RawResponse(reqId, input, false, false));
logI("RpgHttpConnectionThread.handleApiResponse()", "Added response(s) to decoder: " + reqIds.toString());
}
}
use of com.vodafone360.people.service.transport.DecoderThread.RawResponse in project 360-Engine-for-Android by 360.
the class PollThread method handleResponse.
/**
* <p>
* Looks at the response and adds it to the necessary decoder.
* </p>
* TODO: this method should be worked on. The decoder should take care of
* deciding which methods are decoded in which way.
*
* @param response The server response to decode.
* @throws Exception Thrown if the returned status line was null or if the
* response was null.
*/
private void handleResponse(HttpResponse response) throws Exception {
InputStream mDataStream = null;
if (response != null) {
if (null != response.getStatusLine()) {
int respCode = response.getStatusLine().getStatusCode();
Log.d("POLLTIMETEST", "POLL Got response status: " + respCode);
switch(respCode) {
case HttpStatus.SC_OK:
try {
mDataStream = response.getEntity().getContent();
List<RpgMessage> mRpgMessages = new ArrayList<RpgMessage>();
// Get array of RPG messages
// throws IO Exception, we pass it to the calling
// method
RpgHelper.splitRpgResponse(mDataStream, mRpgMessages);
byte[] body = null;
RpgHeader rpgHeader = null;
// Process each header
for (RpgMessage mRpgMessage : mRpgMessages) {
body = mRpgMessage.body();
rpgHeader = mRpgMessage.header();
// Determine RPG mssageType (internal response,
// push
// etc)
int mMessageType = rpgHeader.reqType();
if (mMessageType == RpgMessageTypes.RPG_POLL_MESSAGE) {
mBlankHeaderCount++;
Log.e("POLLTIMETEST", "POLL handleResponse(): blank poll responses");
if (mBlankHeaderCount == Settings.BLANK_RPG_HEADER_COUNT) {
Log.e("POLLTIMETEST", "POLL handleResponse(): " + Settings.BLANK_RPG_HEADER_COUNT + " blank poll responses");
stopRpgPolling();
}
} else {
Log.d("POLLTIMETEST", "POLL handleResponse() Non-RPG_POLL_MESSAGE");
// Reset blank header counter
mBlankHeaderCount = 0;
boolean mZipped = mRpgMessage.header().compression();
if (body != null && (body.length > 0)) {
switch(mMessageType) {
case RpgMessageTypes.RPG_EXT_RESP:
// External message response
Log.d("POLLTIMETEST", "POLLhandleResponse() RpgMessageTypes.RPG_EXT_RESP - " + "Add External Message RawResponse to Decode queue:" + rpgHeader.reqId() + "mBody.len=" + body.length);
mDecoder.addToDecode(new RawResponse(rpgHeader.reqId(), body, mZipped, false));
break;
case RpgMessageTypes.RPG_PUSH_MSG:
// Define push message callback
// to
// notify controller
Log.d("POLLTIMETEST", "POLLhandleResponse() " + "RpgMessageTypes.RPG_PUSH_MSG - " + "Add Push Message RawResponse to Decode queue:" + 0 + "mBody.len=" + body.length);
mDecoder.addToDecode(new RawResponse(0, body, mZipped, true));
break;
case RpgMessageTypes.RPG_INT_RESP:
// Internal message response
Log.d("POLLTIMETEST", "POLLhandleResponse()" + " RpgMessageTypes.RPG_INT_RESP -" + " Add RawResponse to Decode queue:" + rpgHeader.reqId() + "mBody.len=" + body.length);
mDecoder.addToDecode(new RawResponse(rpgHeader.reqId(), body, mZipped, false));
break;
case RpgMessageTypes.RPG_PRESENCE_RESPONSE:
Log.d("POLLTIMETEST", "POLLhandleResponse() " + "RpgMessageTypes.RPG_PRESENCE_RESPONSE" + " - Add RawResponse to Decode queue - mZipped[" + mZipped + "]" + "mBody.len=" + body.length);
mDecoder.addToDecode(new RawResponse(rpgHeader.reqId(), body, mZipped, false));
break;
default:
// Do nothing.
break;
}
}
}
}
} finally {
if (mDataStream != null)
try {
mDataStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
mDataStream = null;
}
}
break;
default:
stopRpgPolling();
Log.e("POLLTIMETEST", "POLL handleResponse() not OK status code:" + respCode);
}
consumeResponse(response);
} else {
mMode = IDLE_MODE;
throw new Exception("POLL Response status line was null.");
}
} else {
mMode = IDLE_MODE;
throw new Exception("POLL Response was null.");
}
}
Aggregations