Search in sources :

Example 21 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project ion by koush.

the class StreamTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    AsyncHttpServer server = new AsyncHttpServer();
    port = server.listen(0).getLocalPort();
    server.get("/", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.code(200);
            ByteBuffer b = ByteBufferList.obtain(random.length);
            b.put(random);
            b.flip();
            ByteBufferList list = new ByteBufferList(b);
            Util.writeAll(response, list, new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    response.end();
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) ByteBuffer(java.nio.ByteBuffer)

Example 22 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project PushSms by koush.

the class GcmText method send.

public void send(GcmSocket gcmSocket, String id) {
    // serialize the message
    BEncodedDictionary message = new BEncodedDictionary();
    // mark the type as a message
    message.put("t", MessageTypes.MESSAGE);
    // grant an id to acknowledge
    message.put("id", id);
    message.put("sca", scAddr);
    BEncodedList texts = new BEncodedList();
    message.put("ts", texts);
    for (String text : this.texts) {
        texts.add(text);
    }
    gcmSocket.write(new ByteBufferList(message.toByteArray()));
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) BEncodedDictionary(org.cyanogenmod.pushsms.bencode.BEncodedDictionary) BEncodedList(org.cyanogenmod.pushsms.bencode.BEncodedList)

Example 23 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList 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)

Example 24 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project PushSms by koush.

the class MiddlewareService method parseGcmMessage.

private void parseGcmMessage(GcmSocket gcmSocket, ByteBufferList bb) {
    try {
        BEncodedDictionary message = BEncodedDictionary.parseDictionary(bb.getAllByteArray());
        String messageType = message.getString("t");
        if (MessageTypes.MESSAGE.equals(messageType)) {
            // incoming text via gcm
            GcmText gcmText = GcmText.parse(gcmSocket, message);
            if (gcmText == null)
                return;
            // ack the message
            String messageId = message.getString("id");
            if (messageId != null) {
                BEncodedDictionary ack = new BEncodedDictionary();
                ack.put("t", MessageTypes.ACK);
                ack.put("id", messageId);
                gcmSocket.write(new ByteBufferList(ack.toByteArray()));
            }
            // synthesize a fake message for the android system
            smsTransport.synthesizeMessages(gcmSocket.getNumber(), gcmText.scAddr, gcmText.texts, System.currentTimeMillis());
        } else if (MessageTypes.ACK.equals(messageType)) {
            // incoming ack
            String messageId = message.getString("id");
            if (messageId == null)
                return;
            GcmText gcmText = messagesAwaitingAck.remove(messageId);
            if (gcmText == null)
                return;
            firePendingIntents(gcmText.sentIntents);
            firePendingIntents(gcmText.deliveryIntents);
        }
    } catch (Exception e) {
        Log.e(LOGTAG, "Error handling GCM socket message", e);
    }
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) BEncodedDictionary(org.cyanogenmod.pushsms.bencode.BEncodedDictionary) RemoteException(android.os.RemoteException) IOException(java.io.IOException)

Example 25 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class LineEmitterTests method testFunnyCharacter.

public void testFunnyCharacter() {
    final String stuff = "é\n";
    LineEmitter emitter = new LineEmitter(Charsets.UTF_8);
    emitter.setLineCallback(new LineEmitter.StringCallback() {

        @Override
        public void onStringAvailable(String s) {
            assertEquals(s + '\n', stuff);
        }
    });
    assertEquals(stuff.charAt(0), 233);
    ByteBufferList bb = new ByteBufferList(ByteBuffer.wrap(stuff.getBytes(Charsets.UTF_8)));
    emitter.onDataAvailable(null, bb);
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) LineEmitter(com.koushikdutta.async.LineEmitter)

Aggregations

ByteBufferList (com.koushikdutta.async.ByteBufferList)39 DataEmitter (com.koushikdutta.async.DataEmitter)13 DataCallback (com.koushikdutta.async.callback.DataCallback)13 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)10 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)10 ByteBuffer (java.nio.ByteBuffer)10 IOException (java.io.IOException)6 Semaphore (java.util.concurrent.Semaphore)5 PushParser (com.koushikdutta.async.PushParser)4 ExecutionException (java.util.concurrent.ExecutionException)4 JsonObject (com.google.gson.JsonObject)3 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)3 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)3 GsonObjectParser (com.koushikdutta.ion.gson.GsonObjectParser)3 TimeoutException (java.util.concurrent.TimeoutException)3 BEncodedDictionary (org.cyanogenmod.pushsms.bencode.BEncodedDictionary)3 RemoteException (android.os.RemoteException)2 JsonParseException (com.google.gson.JsonParseException)2 LineEmitter (com.koushikdutta.async.LineEmitter)2 TapCallback (com.koushikdutta.async.TapCallback)2