Search in sources :

Example 1 with RpgHeader

use of com.vodafone360.people.service.io.rpg.RpgHeader 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 RpgHeader

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

the class PollThread method startConnection.

/**
     * Starts the connection.
     */
protected synchronized void startConnection(DecoderThread decoder) {
    mDecoder = decoder;
    setHttpClient();
    mIsConnectionRunning = true;
    try {
        URL url = new URL(SettingsManager.getProperty(Settings.RPG_SERVER_KEY) + LoginEngine.getSession().userID);
        mUrl = url.toURI();
        mHeader = new RpgHeader();
        mMode = ACTIVE_MODE;
        Thread t = new Thread(this);
        t.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : RpgHeader(com.vodafone360.people.service.io.rpg.RpgHeader) URL(java.net.URL) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) DecoderThread(com.vodafone360.people.service.transport.DecoderThread)

Example 3 with RpgHeader

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

the class ConnectionTester method getConnectionTestHessianPayload.

/**
     * Returns a byte-array containing the data needed for sending a connection
     * test to the RPG.
     * 
     * @throws IOException If there was an exception serializing the hash map to
     *             a hessian byte array.
     * @return A byte array representing the connection test request.
     */
private byte[] getConnectionTestHessianPayload() throws IOException {
    // hash table for parameters to Hessian encode
    final Hashtable<String, Object> ht = new Hashtable<String, Object>();
    final AuthSessionHolder auth = LoginEngine.getSession();
    ht.put("userid", auth.userID);
    // do Hessian encoding
    final byte[] payload = HessianEncoder.createHessianByteArray("", ht);
    payload[1] = (byte) 1;
    payload[2] = (byte) 0;
    final int reqLength = RpgHeader.HEADER_LENGTH + payload.length;
    final RpgHeader rpgHeader = new RpgHeader();
    rpgHeader.setPayloadLength(payload.length);
    rpgHeader.setReqType(RpgMessageTypes.RPG_TCP_CONNECTION_TEST);
    final byte[] rpgMsg = new byte[reqLength];
    System.arraycopy(rpgHeader.createHeader(), 0, rpgMsg, 0, RpgHeader.HEADER_LENGTH);
    if (null != payload) {
        System.arraycopy(payload, 0, rpgMsg, RpgHeader.HEADER_LENGTH, payload.length);
    }
    return rpgMsg;
}
Also used : AuthSessionHolder(com.vodafone360.people.datatypes.AuthSessionHolder) Hashtable(java.util.Hashtable) RpgHeader(com.vodafone360.people.service.io.rpg.RpgHeader)

Example 4 with RpgHeader

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

the class HeartbeatSenderThread method getHeartbeatHessianPayload.

/**
     * Returns a byte-array containing the data needed for sending a heartbeat
     * to the RPG.
     * 
     * @throws IOException If there was an exception serializing the hash map to
     *             a hessian byte array.
     * @return A byte array representing the heartbeat.
     */
private byte[] getHeartbeatHessianPayload() throws IOException {
    // hash table for parameters to Hessian encode
    final Hashtable<String, Object> ht = new Hashtable<String, Object>();
    final String timestamp = "" + ((long) System.currentTimeMillis() / MILLIS_PER_SEC);
    final AuthSessionHolder auth = LoginEngine.getSession();
    if (auth == null) {
        throw new NullPointerException();
    }
    ht.put("auth", AuthUtils.calculateAuth("", new Hashtable<String, Object>(), timestamp, auth));
    ht.put("userid", auth.userID);
    // do Hessian encoding
    final byte[] payload = HessianEncoder.createHessianByteArray("", ht);
    payload[1] = (byte) 1;
    payload[2] = (byte) 0;
    final int reqLength = RpgHeader.HEADER_LENGTH + payload.length;
    final RpgHeader rpgHeader = new RpgHeader();
    rpgHeader.setPayloadLength(payload.length);
    rpgHeader.setReqType(RpgMessageTypes.RPG_TCP_HEARTBEAT);
    final byte[] rpgMsg = new byte[reqLength];
    System.arraycopy(rpgHeader.createHeader(), 0, rpgMsg, 0, RpgHeader.HEADER_LENGTH);
    if (null != payload) {
        System.arraycopy(payload, 0, rpgMsg, RpgHeader.HEADER_LENGTH, payload.length);
    }
    return rpgMsg;
}
Also used : AuthSessionHolder(com.vodafone360.people.datatypes.AuthSessionHolder) Hashtable(java.util.Hashtable) RpgHeader(com.vodafone360.people.service.io.rpg.RpgHeader)

Example 5 with RpgHeader

use of com.vodafone360.people.service.io.rpg.RpgHeader 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)5 IOException (java.io.IOException)3 AuthSessionHolder (com.vodafone360.people.datatypes.AuthSessionHolder)2 RpgMessage (com.vodafone360.people.service.io.rpg.RpgMessage)2 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 Hashtable (java.util.Hashtable)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 DecoderThread (com.vodafone360.people.service.transport.DecoderThread)1 RawResponse (com.vodafone360.people.service.transport.DecoderThread.RawResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URL (java.net.URL)1