use of io.vertx.core.net.impl.ServerID in project vert.x by eclipse.
the class HttpServerImpl method listen.
public synchronized HttpServer listen(int port, String host, Handler<AsyncResult<HttpServer>> listenHandler) {
if (requestStream.handler() == null && wsStream.handler() == null) {
throw new IllegalStateException("Set request or websocket handler first");
}
if (listening) {
throw new IllegalStateException("Already listening");
}
listenContext = vertx.getOrCreateContext();
listening = true;
serverOrigin = (options.isSsl() ? "https" : "http") + "://" + host + ":" + port;
List<HttpVersion> applicationProtocols = options.getAlpnVersions();
if (listenContext.isWorkerContext()) {
applicationProtocols = applicationProtocols.stream().filter(v -> v != HttpVersion.HTTP_2).collect(Collectors.toList());
}
sslHelper.setApplicationProtocols(applicationProtocols);
synchronized (vertx.sharedHttpServers()) {
// Will be updated on bind for a wildcard port
this.actualPort = port;
id = new ServerID(port, host);
HttpServerImpl shared = vertx.sharedHttpServers().get(id);
if (shared == null || port == 0) {
serverChannelGroup = new DefaultChannelGroup("vertx-acceptor-channels", GlobalEventExecutor.INSTANCE);
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(vertx.getAcceptorEventLoopGroup(), availableWorkers);
bootstrap.channel(NioServerSocketChannel.class);
applyConnectionOptions(bootstrap);
sslHelper.validate(vertx);
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
if (requestStream.isPaused() || wsStream.isPaused()) {
ch.close();
return;
}
ChannelPipeline pipeline = ch.pipeline();
if (sslHelper.isSSL()) {
pipeline.addLast("ssl", sslHelper.createSslHandler(vertx));
if (options.isUseAlpn()) {
pipeline.addLast("alpn", new ApplicationProtocolNegotiationHandler("http/1.1") {
@Override
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception {
if (protocol.equals("http/1.1")) {
configureHttp1(pipeline);
} else {
handleHttp2(ch);
}
}
});
} else {
configureHttp1(pipeline);
}
} else {
if (DISABLE_HC2) {
configureHttp1(pipeline);
} else {
pipeline.addLast(new Http1xOrHttp2Handler());
}
}
}
});
addHandlers(this, listenContext);
try {
bindFuture = AsyncResolveConnectHelper.doBind(vertx, port, host, bootstrap);
bindFuture.addListener(res -> {
if (res.failed()) {
vertx.sharedHttpServers().remove(id);
} else {
Channel serverChannel = res.result();
HttpServerImpl.this.actualPort = ((InetSocketAddress) serverChannel.localAddress()).getPort();
serverChannelGroup.add(serverChannel);
metrics = vertx.metricsSPI().createMetrics(this, new SocketAddressImpl(port, host), options);
}
});
} catch (final Throwable t) {
// Make sure we send the exception back through the handler (if any)
if (listenHandler != null) {
vertx.runOnContext(v -> listenHandler.handle(Future.failedFuture(t)));
} else {
// No handler - log so user can see failure
log.error(t);
}
listening = false;
return this;
}
vertx.sharedHttpServers().put(id, this);
actualServer = this;
} else {
// Server already exists with that host/port - we will use that
actualServer = shared;
this.actualPort = shared.actualPort;
addHandlers(actualServer, listenContext);
metrics = vertx.metricsSPI().createMetrics(this, new SocketAddressImpl(port, host), options);
}
actualServer.bindFuture.addListener(future -> {
if (listenHandler != null) {
final AsyncResult<HttpServer> res;
if (future.succeeded()) {
res = Future.succeededFuture(HttpServerImpl.this);
} else {
res = Future.failedFuture(future.cause());
listening = false;
}
listenContext.runOnContext((v) -> listenHandler.handle(res));
} else if (future.failed()) {
listening = false;
log.error(future.cause());
}
});
}
return this;
}
use of io.vertx.core.net.impl.ServerID in project vert.x by eclipse.
the class ClusteredEventBus method start.
@Override
public void start(Handler<AsyncResult<Void>> resultHandler) {
clusterManager.<String, ClusterNodeInfo>getAsyncMultiMap(SUBS_MAP_NAME, ar2 -> {
if (ar2.succeeded()) {
subs = ar2.result();
server = vertx.createNetServer(getServerOptions());
server.connectHandler(getServerHandler());
server.listen(asyncResult -> {
if (asyncResult.succeeded()) {
int serverPort = getClusterPublicPort(options, server.actualPort());
String serverHost = getClusterPublicHost(options);
serverID = new ServerID(serverPort, serverHost);
nodeInfo = new ClusterNodeInfo(clusterManager.getNodeID(), serverID);
haManager.addDataToAHAInfo(SERVER_ID_HA_KEY, new JsonObject().put("host", serverID.host).put("port", serverID.port));
if (resultHandler != null) {
started = true;
resultHandler.handle(Future.succeededFuture());
}
} else {
if (resultHandler != null) {
resultHandler.handle(Future.failedFuture(asyncResult.cause()));
} else {
log.error(asyncResult.cause());
}
}
});
} else {
if (resultHandler != null) {
resultHandler.handle(Future.failedFuture(ar2.cause()));
} else {
log.error(ar2.cause());
}
}
});
}
use of io.vertx.core.net.impl.ServerID in project vert.x by eclipse.
the class ConnectionHolder method schedulePing.
private void schedulePing() {
EventBusOptions options = eventBus.options();
pingTimeoutID = vertx.setTimer(options.getClusterPingInterval(), id1 -> {
timeoutID = vertx.setTimer(options.getClusterPingReplyInterval(), id2 -> {
log.warn("No pong from server " + serverID + " - will consider it dead");
close();
});
ClusteredMessage pingMessage = new ClusteredMessage<>(serverID, PING_ADDRESS, null, null, null, new PingMessageCodec(), true, eventBus);
Buffer data = pingMessage.encodeToWire();
socket.write(data);
});
}
use of io.vertx.core.net.impl.ServerID in project vert.x by eclipse.
the class AsyncMultiMapTest method testRemoveAllForValue.
@Test
public void testRemoveAllForValue() {
ServerID serverID1 = new ServerID(1234, "foo.com");
map.add("some-sub", serverID1, onSuccess(res -> {
assertNull(res);
ServerID serverID2 = new ServerID(4321, "blah.com");
map.add("some-sub", serverID2, onSuccess(res2 -> {
assertNull(res2);
map.add("some-sub2", serverID1, onSuccess(res3 -> {
assertNull(res3);
map.removeAllForValue(serverID1, onSuccess(res4 -> {
assertNull(res4);
map.get("some-sub", onSuccess(res5 -> {
Set<ServerID> set = new HashSet<>();
for (ServerID sid : res5) {
set.add(sid);
}
assertEquals(1, set.size());
assertTrue(set.contains(serverID2));
assertFalse(set.contains(serverID1));
map.get("some-sub2", onSuccess(res6 -> {
Set<ServerID> set2 = new HashSet<>();
for (ServerID sid : res6) {
set2.add(sid);
}
assertEquals(0, set2.size());
testComplete();
}));
}));
}));
}));
}));
}));
await();
}
use of io.vertx.core.net.impl.ServerID in project vert.x by eclipse.
the class AsyncMultiMapTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
startNodes(1);
clusterManager = ((VertxInternal) vertices[0]).getClusterManager();
CountDownLatch latch = new CountDownLatch(1);
clusterManager.<String, ServerID>getAsyncMultiMap("mymap", onSuccess(res -> {
map = res;
latch.countDown();
}));
awaitLatch(latch);
}
Aggregations