Search in sources :

Example 1 with RpgMessage

use of com.vodafone360.people.service.io.rpg.RpgMessage 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;
                }
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RpgHeader(com.vodafone360.people.service.io.rpg.RpgHeader) RpgMessage(com.vodafone360.people.service.io.rpg.RpgMessage)

Example 2 with RpgMessage

use of com.vodafone360.people.service.io.rpg.RpgMessage 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.");
    }
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RpgHeader(com.vodafone360.people.service.io.rpg.RpgHeader) RpgMessage(com.vodafone360.people.service.io.rpg.RpgMessage) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) RawResponse(com.vodafone360.people.service.transport.DecoderThread.RawResponse)

Aggregations

RpgHeader (com.vodafone360.people.service.io.rpg.RpgHeader)2 RpgMessage (com.vodafone360.people.service.io.rpg.RpgMessage)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 RawResponse (com.vodafone360.people.service.transport.DecoderThread.RawResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1