Search in sources :

Example 1 with GcmSocket

use of org.cyanogenmod.pushsms.socket.GcmSocket in project PushSms by koush.

the class MiddlewareService method findOrCreateGcmSocket.

// given a registration, find/create the gcm socket that manages
// the secure connection between the two devices.
private GcmSocket findOrCreateGcmSocket(Registration registration) {
    GcmSocket ret = gcmConnectionManager.findGcmSocket(registration);
    if (ret == null) {
        final GcmSocket gcmSocket = ret = gcmConnectionManager.createGcmSocket(registration, getNumber());
        // parse data from the gcm connection as we get it
        ret.setDataCallback(new DataCallback() {

            @Override
            public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                parseGcmMessage(gcmSocket, bb);
                // save the registration info (sequence numbers changed, etc)
                registry.register(gcmSocket.registration.endpoint, gcmSocket.registration);
            }
        });
        // on error, fail over any pending messages for this number
        ret.setEndCallback(new CompletedCallback() {

            @Override
            public void onCompleted(Exception ex) {
                for (String pending : new ArrayList<String>(messagesAwaitingAck.keySet())) {
                    String numberPart = pending.split(":")[0];
                    if (!PhoneNumberUtils.compare(MiddlewareService.this, numberPart, gcmSocket.getNumber()))
                        continue;
                    GcmText gcmText = messagesAwaitingAck.get(pending);
                    if (gcmText == null)
                        continue;
                    gcmText.manageFailure(handler, smsManager);
                }
            }
        });
    }
    return ret;
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) GcmSocket(org.cyanogenmod.pushsms.socket.GcmSocket) DataEmitter(com.koushikdutta.async.DataEmitter) DataCallback(com.koushikdutta.async.callback.DataCallback) RemoteException(android.os.RemoteException) IOException(java.io.IOException)

Aggregations

RemoteException (android.os.RemoteException)1 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 DataEmitter (com.koushikdutta.async.DataEmitter)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 DataCallback (com.koushikdutta.async.callback.DataCallback)1 IOException (java.io.IOException)1 GcmSocket (org.cyanogenmod.pushsms.socket.GcmSocket)1