Java Code Examples for io.vertx.core.Vertx#createNetServer()

The following examples show how to use io.vertx.core.Vertx#createNetServer() . 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: TcpEventBusBridgeImpl.java    From vertx-tcp-eventbus-bridge with Apache License 2.0 5 votes vote down vote up
public TcpEventBusBridgeImpl(Vertx vertx, BridgeOptions options, NetServerOptions netServerOptions, Handler<BridgeEvent> eventHandler) {
  this.eb = vertx.eventBus();
  this.options = options != null ? options : new BridgeOptions();
  this.bridgeEventHandler = eventHandler;

  server = vertx.createNetServer(netServerOptions == null ? new NetServerOptions() : netServerOptions);
  server.connectHandler(this::handler);
}
 
Example 4
Source File: StompServerImpl.java    From vertx-stomp with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of {@link StompServerImpl}.
 * @param vertx the vert.x instance
 * @param net the net server, may be {@code null}
 * @param options the options
 */
public StompServerImpl(Vertx vertx, NetServer net, StompServerOptions options) {
  Objects.requireNonNull(vertx);
  Objects.requireNonNull(options);
  this.options = options;
  this.vertx = vertx;
  if (net == null) {
    server = vertx.createNetServer(options);
  } else {
    server = net;
  }
}
 
Example 5
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()));
  });
}
 
Example 6
Source File: TestTcpServer.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void testConnectionLimit(@Mocked Vertx vertx, @Mocked AsyncResultCallback<InetSocketAddress> callback,
    @Mocked NetServer netServer, @Mocked NetSocketImpl netSocket) {
  DefaultServerEndpointMetric endpointMetric = new DefaultServerEndpointMetric(null);
  DefaultTcpServerMetrics tcpServerMetrics = new DefaultTcpServerMetrics(endpointMetric);

  new MockUp<NetServer>(netServer) {
    @Mock
    NetServer connectHandler(Handler<NetSocket> handler) {
      connectHandler = handler;
      return netServer;
    }
  };
  new MockUp<NetSocketImpl>(netSocket) {
    @Mock
    void close() {
      netSocketClosed = true;
    }
  };
  new Expectations() {
    {
      vertx.createNetServer((NetServerOptions) any);
      result = netServer;
      netServer.listen(anyInt, anyString, (Handler) any);
      netSocket.metrics();
      result = tcpServerMetrics;
    }
  };
  URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663?sslEnabled=true");
  TcpServer server = new TcpServerForTest(endpointObject) {
    @Override
    protected int getConnectionLimit() {
      return 2;
    }
  };
  // assert done in Expectations
  server.init(vertx, "", callback);

  // no problem
  endpointMetric.onConnect();
  endpointMetric.onConnect();
  connectHandler.handle(netSocket);

  // reject
  endpointMetric.onConnect();
  connectHandler.handle(netSocket);
  Assert.assertTrue(netSocketClosed);
  Assert.assertEquals(1, endpointMetric.getRejectByConnectionLimitCount());
}
 
Example 7
Source File: MqttServerImpl.java    From vertx-mqtt with Apache License 2.0 4 votes vote down vote up
public MqttServerImpl(Vertx vertx, MqttServerOptions options) {
  this.server = vertx.createNetServer(options);
  this.options = options;
}
 
Example 8
Source File: VertxNetUtils.java    From Lealone-Plugins with Apache License 2.0 4 votes vote down vote up
public static NetServer createNetServer(Vertx vertx, EncryptionOptions eo) {
    NetServerOptions netServerOptions = VertxNetUtils.getNetServerOptions(eo);
    NetServer server = vertx.createNetServer(netServerOptions);
    return server;
}