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();
}
});
}
});
}
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()));
}
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;
}
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);
}
}
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);
}
Aggregations