use of io.vertx.core.net.impl.SocketAddressImpl 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.SocketAddressImpl in project vert.x by eclipse.
the class NetTest method testSocketAddress.
@Test
public void testSocketAddress() throws Exception {
assertNullPointerException(() -> new SocketAddressImpl(0, null));
assertIllegalArgumentException(() -> new SocketAddressImpl(0, ""));
assertIllegalArgumentException(() -> new SocketAddressImpl(-1, "someHost"));
assertIllegalArgumentException(() -> new SocketAddressImpl(65536, "someHost"));
}
use of io.vertx.core.net.impl.SocketAddressImpl in project java-chassis by ServiceComb.
the class TestTcp method testTcpClient.
@Test
public void testTcpClient() throws Exception {
NetClient oNetClient = new NetClient() {
@Override
public boolean isMetricsEnabled() {
// TODO Auto-generated method stub
return true;
}
@Override
public NetClient connect(int port, String host, Handler<AsyncResult<NetSocket>> connectHandler) {
return Mockito.mock(NetClient.class);
}
@Override
public void close() {
// TODO Auto-generated method stub
}
};
TcpClient oTcpClient = new TcpClient(Mockito.mock(Context.class), oNetClient, "highway://127.2.0.1:8080", new TcpClientConfig());
oTcpClient.checkTimeout();
oTcpClient.send(new TcpOutputStream(), 123, Mockito.mock(TcpResonseCallback.class));
oTcpClient.send(new TcpOutputStream(), 123, Mockito.mock(TcpResonseCallback.class));
Assert.assertNotEquals(null, oTcpClient.getContext());
new MockUp<TcpClientPool>() {
@Mock
protected void startCheckTimeout(TcpClientConfig clientConfig, Context context) {
}
};
TcpClientConfig config = new TcpClientConfig();
TcpClientPool oClientPool = new TcpClientPool(config, Vertx.vertx().getOrCreateContext(), oNetClient);
oClientPool.send("highway://152.2.2.3:8080", new TcpOutputStream(), Mockito.mock(TcpResonseCallback.class));
oClientPool.send("highway://152.2.2.3:8080", new TcpOutputStream(), Mockito.mock(TcpResonseCallback.class));
Assert.assertNotNull(oClientPool);
TcpRequest oTcpRequest = new TcpRequest(1234, Mockito.mock(TcpResonseCallback.class));
oTcpRequest.isTimeout();
oTcpRequest.onReply(Buffer.buffer(), Buffer.buffer(("test").getBytes()));
oTcpRequest.onSendError(new Throwable("test Errorsss"));
Assert.assertNotNull(oTcpRequest);
TcpClientVerticle oTcpClientVerticle = new TcpClientVerticle();
oTcpClientVerticle.init(Vertx.vertx(), Vertx.vertx().getOrCreateContext());
oTcpClientVerticle.createClientPool();
oTcpClientVerticle.createClientPool();
Assert.assertNotNull(oTcpClientVerticle.getVertx());
NetSocket socket = Mockito.mock(NetSocket.class);
Throwable e = Mockito.mock(Throwable.class);
Buffer hBuffer = Mockito.mock(Buffer.class);
Buffer bBuffer = Mockito.mock(Buffer.class);
Deencapsulation.invoke(oTcpClient, "connect");
Deencapsulation.invoke(oTcpClient, "onConnectSuccess", socket);
Mockito.when(socket.localAddress()).thenReturn(new SocketAddressImpl(0, "127.0.0.1"));
Deencapsulation.setField(oTcpClient, "netSocket", socket);
Deencapsulation.invoke(oTcpClient, "onDisconnected", e);
Deencapsulation.invoke(oTcpClient, "tryLogin");
Deencapsulation.invoke(oTcpClient, "onLoginSuccess");
Deencapsulation.invoke(oTcpClient, "onConnectFailed", e);
long l = 10;
Deencapsulation.invoke(oTcpClient, "onReply", l, hBuffer, bBuffer);
oTcpClient.checkTimeout();
Assert.assertNotNull(oTcpClient);
}
use of io.vertx.core.net.impl.SocketAddressImpl in project vert.x by eclipse.
the class DatagramSocketImpl method send.
@Override
@SuppressWarnings("unchecked")
public DatagramSocket send(Buffer packet, int port, String host, Handler<AsyncResult<DatagramSocket>> handler) {
Objects.requireNonNull(packet, "no null packet accepted");
Objects.requireNonNull(host, "no null host accepted");
InetSocketAddress addr = InetSocketAddress.createUnresolved(host, port);
if (addr.isUnresolved()) {
vertx.resolveAddress(host, res -> {
if (res.succeeded()) {
doSend(packet, new InetSocketAddress(res.result(), port), handler);
} else {
handler.handle(Future.failedFuture(res.cause()));
}
});
} else {
// If it's immediately resolved it means it was just an IP address so no need to async resolve
doSend(packet, addr, handler);
}
if (metrics.isEnabled()) {
metrics.bytesWritten(null, new SocketAddressImpl(port, host), packet.length());
}
return this;
}
use of io.vertx.core.net.impl.SocketAddressImpl in project java-chassis by ServiceComb.
the class TestHighwayServerConnection method init.
@Before
public void init() {
new Expectations() {
{
netSocket.remoteAddress();
result = new SocketAddressImpl(new InetSocketAddress("127.0.0.1", 80));
}
};
connection = new HighwayServerConnection();
connection.init(netSocket);
header = new RequestHeader();
}
Aggregations