use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class HttpServerTests method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
httpServer = new AsyncHttpServer();
httpServer.setErrorCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
fail();
}
});
httpServer.listen(AsyncServer.getDefault(), 5000);
httpServer.get("/hello", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
assertNotNull(request.getHeaders().get("Host"));
response.send("hello");
}
});
httpServer.post("/echo", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
try {
assertNotNull(request.getHeaders().get("Host"));
JSONObject json = new JSONObject();
if (request.getBody() instanceof UrlEncodedFormBody) {
UrlEncodedFormBody body = (UrlEncodedFormBody) request.getBody();
for (NameValuePair pair : body.get()) {
json.put(pair.getName(), pair.getValue());
}
} else if (request.getBody() instanceof JSONObjectBody) {
json = ((JSONObjectBody) request.getBody()).get();
} else if (request.getBody() instanceof StringBody) {
json.put("foo", ((StringBody) request.getBody()).get());
} else if (request.getBody() instanceof MultipartFormDataBody) {
MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
for (NameValuePair pair : body.get()) {
json.put(pair.getName(), pair.getValue());
}
}
response.send(json);
} catch (Exception e) {
}
}
});
}
use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class FutureTests method testContinuationArray.
public void testContinuationArray() throws Exception {
final ArrayList<Integer> results = new ArrayList<Integer>();
final Semaphore semaphore = new Semaphore(0);
final Continuation c = new Continuation(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
semaphore.release();
}
});
for (int i = 0; i < 10; i++) {
final int j = i;
c.add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
results.add(j);
next.onCompleted(null);
}
});
}
new Thread() {
public void run() {
c.start();
}
;
}.start();
assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
assertEquals(10, results.size());
for (int i = 0; i < 10; i++) {
assertEquals((int) results.get(i), i);
}
}
use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class FutureTests method testFutureChain.
public void testFutureChain() throws Exception {
final Semaphore semaphore = new Semaphore(0);
final Continuation c = new Continuation(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
semaphore.release();
}
});
IntegerFuture i1;
c.add(i1 = IntegerFuture.create(2, 200));
IntegerFuture i2;
c.add(i2 = IntegerFuture.create(3, 200));
new Thread() {
public void run() {
c.start();
}
;
}.start();
assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
assertEquals((int) i1.get(), 2);
assertEquals((int) i2.get(), 3);
}
use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.
the class FutureTests method testContinuationFail.
public void testContinuationFail() throws Exception {
final Semaphore semaphore = new Semaphore(0);
final Continuation c = new Continuation(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
assertNotNull(ex);
semaphore.release();
}
});
c.add(new ContinuationCallback() {
@Override
public void onContinue(Continuation continuation, CompletedCallback next) throws Exception {
throw new Exception("fail");
}
});
new Thread() {
public void run() {
c.start();
}
;
}.start();
assertTrue(semaphore.tryAcquire(3000, TimeUnit.MILLISECONDS));
}
use of com.koushikdutta.async.callback.CompletedCallback in project K6nele by Kaljurand.
the class WebSocketRecognitionService method startSocket.
/**
* Opens the socket and starts recording/sending.
*
* @param url Webservice URL
*/
void startSocket(String url) {
mIsEosSent = false;
Log.i(url);
AsyncHttpClient client = AsyncHttpClient.getDefaultInstance();
if (false) {
// http://stackoverflow.com/questions/37804816/androidasync-how-to-create-ssl-client-in-websocket-connection
AsyncSSLSocketMiddleware sslSocketMiddleware = new AsyncSSLSocketMiddleware(client);
SSLContext sslContext = null;
try {
sslContext = getSSLContext();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
sslSocketMiddleware.setSSLContext(sslContext);
client.insertMiddleware(sslSocketMiddleware);
}
client.websocket(url, PROTOCOL, new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, final WebSocket webSocket) {
mWebSocket = webSocket;
if (ex != null) {
handleException(ex);
return;
}
webSocket.setStringCallback(new WebSocket.StringCallback() {
public void onStringAvailable(String s) {
Log.i(s);
handleResult(s);
}
});
webSocket.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex == null) {
Log.e("ClosedCallback");
handleFinish(mIsEosSent);
} else {
Log.e("ClosedCallback: ", ex);
handleException(ex);
}
}
});
webSocket.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex == null) {
Log.e("EndCallback");
handleFinish(mIsEosSent);
} else {
Log.e("EndCallback: ", ex);
handleException(ex);
}
}
});
startSending(webSocket);
}
});
}
Aggregations