use of io.vertx.core.Context in project vert.x by eclipse.
the class AsynchronousCounter method decrementAndGet.
@Override
public void decrementAndGet(Handler<AsyncResult<Long>> resultHandler) {
Objects.requireNonNull(resultHandler, "resultHandler");
Context context = vertx.getOrCreateContext();
context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.decrementAndGet())));
}
use of io.vertx.core.Context in project vert.x by eclipse.
the class VertxHttp2NetSocket method sendFile.
@Override
public NetSocket sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
synchronized (conn) {
Context resultCtx = resultHandler != null ? vertx.getOrCreateContext() : null;
File file = vertx.resolveFile(filename);
if (!file.exists()) {
if (resultHandler != null) {
resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(new FileNotFoundException())));
} else {
// log.error("File not found: " + filename);
}
return this;
}
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (IOException e) {
if (resultHandler != null) {
resultCtx.runOnContext((v) -> resultHandler.handle(Future.failedFuture(e)));
} else {
//log.error("Failed to send file", e);
}
return this;
}
long contentLength = Math.min(length, file.length() - offset);
FileStreamChannel fileChannel = new FileStreamChannel(ar -> {
if (resultHandler != null) {
resultCtx.runOnContext(v -> {
resultHandler.handle(Future.succeededFuture());
});
}
}, this, offset, contentLength);
drainHandler(fileChannel.drainHandler);
handlerContext.channel().eventLoop().register(fileChannel);
fileChannel.pipeline().fireUserEventTriggered(raf);
}
return this;
}
use of io.vertx.core.Context in project vertx-proton by vert-x3.
the class ProtonServerImplTest method testAuthenticatorCreatedPerConnection.
@Test(timeout = 20000)
public void testAuthenticatorCreatedPerConnection(TestContext context) {
Async connectedAsync = context.async();
Async connectedAsync2 = context.async();
AtomicInteger port = new AtomicInteger(-1);
final TestPlainAuthenticatorFactory authenticatorFactory = new TestPlainAuthenticatorFactory();
ProtonServer.create(vertx).saslAuthenticatorFactory(authenticatorFactory).connectHandler(protonConnection -> {
// Verify the expected auth detail was recorded in the connection attachments, just using a String here.
String authValue = protonConnection.attachments().get(AUTH_KEY, String.class);
context.assertEquals(AUTH_VALUE, authValue);
}).listen(server -> {
port.set(server.result().actualPort());
ProtonClient.create(vertx).connect("localhost", port.intValue(), GOOD_USER, PASSWD, protonConnectionAsyncResult -> {
context.assertTrue(protonConnectionAsyncResult.succeeded());
protonConnectionAsyncResult.result().disconnect();
connectedAsync.complete();
});
});
connectedAsync.awaitSuccess();
context.assertEquals(1, authenticatorFactory.getCreateCount(), "unexpected authenticator count");
ProtonClient.create(vertx).connect("localhost", port.intValue(), GOOD_USER, PASSWD, protonConnectionAsyncResult -> {
context.assertTrue(protonConnectionAsyncResult.succeeded());
protonConnectionAsyncResult.result().disconnect();
connectedAsync2.complete();
});
connectedAsync2.awaitSuccess();
context.assertEquals(2, authenticatorFactory.getCreateCount(), "unexpected authenticator count");
}
use of io.vertx.core.Context in project vertx-proton by vert-x3.
the class ProtonServerImplTest method testCustomAuthenticatorFailsAuthentication.
@Test(timeout = 20000)
public void testCustomAuthenticatorFailsAuthentication(TestContext context) {
Async connectedAsync = context.async();
ProtonServer.create(vertx).saslAuthenticatorFactory(new TestPlainAuthenticatorFactory()).connectHandler(protonConnection -> {
context.fail("Handler should not be called for connection that failed authentication");
}).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), BAD_USER, PASSWD, protonConnectionAsyncResult -> {
context.assertFalse(protonConnectionAsyncResult.succeeded());
connectedAsync.complete();
}));
connectedAsync.awaitSuccess();
}
use of io.vertx.core.Context in project vertx-proton by vert-x3.
the class ProtonServerImplTest method doTestAsyncServerAuthenticatorTestImpl.
private void doTestAsyncServerAuthenticatorTestImpl(TestContext context, boolean passAuthentication) {
Async connectAsync = context.async();
AtomicBoolean connectedServer = new AtomicBoolean();
final long delay = 750;
TestAsyncAuthenticator testAsyncAuthenticator = new TestAsyncAuthenticator(delay, passAuthentication);
TestAsyncAuthenticatorFactory authenticatorFactory = new TestAsyncAuthenticatorFactory(testAsyncAuthenticator);
ProtonServer.create(vertx).saslAuthenticatorFactory(authenticatorFactory).connectHandler(protonConnection -> {
connectedServer.set(true);
}).listen(server -> {
final long startTime = System.currentTimeMillis();
ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), GOOD_USER, PASSWD, conResult -> {
// Verify the process took expected time from auth delay.
long actual = System.currentTimeMillis() - startTime;
context.assertTrue(actual >= delay, "Connect completed before expected time delay elapsed! " + actual);
if (passAuthentication) {
context.assertTrue(conResult.succeeded(), "Expected connect to succeed");
conResult.result().disconnect();
} else {
context.assertFalse(conResult.succeeded(), "Expected connect to fail");
}
connectAsync.complete();
});
});
connectAsync.awaitSuccess();
if (passAuthentication) {
context.assertTrue(connectedServer.get(), "Server handler should have been called");
} else {
context.assertFalse(connectedServer.get(), "Server handler should not have been called");
}
context.assertEquals(1, authenticatorFactory.getCreateCount(), "unexpected authenticator creation count");
}
Aggregations