Java Code Examples for io.vertx.core.net.NetServer#connectHandler()

The following examples show how to use io.vertx.core.net.NetServer#connectHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestTcpServer.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void testTcpServerNonSSL(@Mocked Vertx vertx, @Mocked AsyncResultCallback<InetSocketAddress> callback,
    @Mocked NetServer netServer) {
  new Expectations() {
    {
      vertx.createNetServer();
      result = netServer;
      netServer.connectHandler((Handler) any);
      netServer.listen(anyInt, anyString, (Handler) any);
    }
  };
  URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663");
  TcpServer server = new TcpServerForTest(endpointObject);
  // assert done in Expectations
  server.init(vertx, "", callback);
}
 
Example 2
Source File: TestTcpServer.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void testTcpServerSSL(@Mocked Vertx vertx, @Mocked AsyncResultCallback<InetSocketAddress> callback,
    @Mocked NetServer netServer) {
  new Expectations() {
    {
      vertx.createNetServer((NetServerOptions) any);
      result = netServer;
      netServer.connectHandler((Handler) any);
      netServer.listen(anyInt, anyString, (Handler) any);
    }
  };
  URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663?sslEnabled=true");
  TcpServer server = new TcpServerForTest(endpointObject);
  // assert done in Expectations
  server.init(vertx, "", callback);
}
 
Example 3
Source File: ProtonClientTest.java    From vertx-proton with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000)
public void testConnectionDisconnectedDuringCreation(TestContext context) {
  server.close();

  Async connectFailsAsync = context.async();

  NetServer netServer = this.vertx.createNetServer();
  netServer.connectHandler(netSocket -> {
    netSocket.pause();
    vertx.setTimer(50, x -> {
      netSocket.close();
    });
  });

  netServer.listen(listenResult -> {
    context.assertTrue(listenResult.succeeded());

    ProtonClient.create(vertx).connect("localhost", netServer.actualPort(), connResult -> {
      context.assertFalse(connResult.succeeded());
      connectFailsAsync.complete();
    });

  });

  connectFailsAsync.awaitSuccess();
}
 
Example 4
Source File: WebClientTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Repeat(times = 100)
@Test
public void testTimeoutRequestBeforeSending() throws Exception {
  NetServer server = vertx.createNetServer();
  server.connectHandler(so -> {
  });
  CountDownLatch latch = new CountDownLatch(1);
  server.listen(8080, "localhost", onSuccess(v -> {
    latch.countDown();
  }));
  awaitLatch(latch);
  webClient
    .get(8080, "localhost", "/")
    .timeout(1)
    .send(onFailure(err -> {
      testComplete();
    }));
  await();
}
 
Example 5
Source File: VertxTcpServer.java    From jetlinks-community with Apache License 2.0 5 votes vote down vote up
public void setServer(Collection<NetServer> mqttServer) {
    if (this.tcpServers != null && !this.tcpServers.isEmpty()) {
        shutdown();
    }
    this.tcpServers = mqttServer;

    for (NetServer tcpServer : this.tcpServers) {
        tcpServer.connectHandler(this::acceptTcpConnection);
    }

}
 
Example 6
Source File: TcpServer.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public void init(Vertx vertx, String sslKey, AsyncResultCallback<InetSocketAddress> callback) {
  NetServer netServer;
  if (endpointObject.isSslEnabled()) {
    SSLOptionFactory factory =
        SSLOptionFactory.createSSLOptionFactory(sslKey, null);
    SSLOption sslOption;
    if (factory == null) {
      sslOption = SSLOption.buildFromYaml(sslKey);
    } else {
      sslOption = factory.createSSLOption();
    }
    SSLCustom sslCustom = SSLCustom.createSSLCustom(sslOption.getSslCustomClass());
    NetServerOptions serverOptions = new NetServerOptions();
    VertxTLSBuilder.buildNetServerOptions(sslOption, sslCustom, serverOptions);
    netServer = vertx.createNetServer(serverOptions);
  } else {
    netServer = vertx.createNetServer();
  }

  netServer.connectHandler(netSocket -> {
    DefaultTcpServerMetrics serverMetrics = (DefaultTcpServerMetrics) ((NetSocketImpl) netSocket).metrics();
    DefaultServerEndpointMetric endpointMetric = serverMetrics.getEndpointMetric();
    long connectedCount = endpointMetric.getCurrentConnectionCount();
    int connectionLimit = getConnectionLimit();
    if (connectedCount > connectionLimit) {
      netSocket.close();
      endpointMetric.onRejectByConnectionLimit();
      return;
    }

    TcpServerConnection connection = createTcpServerConnection();
    connection.init(netSocket);
  });
  netServer.exceptionHandler(e -> {
    LOGGER.error("Unexpected error in server.{}", ExceptionUtils.getExceptionMessageWithoutTrace(e));
  });
  InetSocketAddress socketAddress = endpointObject.getSocketAddress();
  netServer.listen(socketAddress.getPort(), socketAddress.getHostString(), ar -> {
    if (ar.succeeded()) {
      callback.success(socketAddress);
      return;
    }

    // 监听失败
    String msg = String.format("listen failed, address=%s", socketAddress.toString());
    callback.fail(new Exception(msg, ar.cause()));
  });
}