@Override public void serverStarted(Server server) throws Exception { if (endpoint == null) { assert server.activePort().isPresent(); endpoint = Endpoint.of(server.defaultHostname(), server.activePort().get() .localAddress().getPort()); } client.start(); final String key = endpoint.host() + '_' + endpoint.port(); final byte[] value = nodeValueCodec.encode(endpoint); client.create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL) .forPath(zNodePath + '/' + key, value); }
/** * Adds an HTTP port that listens to the specified {@code localAddress}. * * @param localAddress the local address to bind * * @see #http(int) * @see <a href="#no_port_specified">What happens if no HTTP(S) port is specified?</a> */ public ServerBuilder http(InetSocketAddress localAddress) { return port(new ServerPort(requireNonNull(localAddress, "localAddress"), HTTP)); }
private static String bossThreadName(ServerPort port) { final InetSocketAddress localAddr = port.localAddress(); final String localHostName = localAddr.getAddress().isAnyLocalAddress() ? "*" : localAddr.getHostString(); // e.g. 'armeria-boss-http-*:8080' // 'armeria-boss-http-127.0.0.1:8443' // 'armeria-boss-proxy+http+https-127.0.0.1:8443' final String protocolNames = port.protocols().stream() .map(SessionProtocol::uriText) .collect(Collectors.joining("+")); return "armeria-boss-" + protocolNames + '-' + localHostName + ':' + localAddr.getPort(); } }
@Override public String toString() { String strVal = this.strVal; if (strVal == null) { this.strVal = strVal = toString(getClass(), localAddress(), protocols()); } return strVal; }
if (p.localAddress().getPort() > 0) { for (int i = 0; i < distinctPorts.size(); i++) { final ServerPort port = distinctPorts.get(i); if (port.localAddress().equals(p.localAddress())) { final ServerPort merged = new ServerPort(port.localAddress(), Sets.union(port.protocols(), p.protocols())); distinctPorts.set(i, merged); found = true;
/** * Sets a {@link SessionProtocol} and its port number only if it has not been specified before. */ SamlPortConfigBuilder setSchemeAndPortIfAbsent(ServerPort serverPort) { requireNonNull(serverPort, "serverPort"); if (serverPort.hasHttps()) { setSchemeIfAbsent(SessionProtocol.HTTPS); } else if (serverPort.hasHttp()) { setSchemeIfAbsent(SessionProtocol.HTTP); } else { throw new IllegalArgumentException("unexpected session protocol: " + serverPort.protocols()); } // Do not set a port if the port number is 0 which means that the port will be automatically chosen. final int port = serverPort.localAddress().getPort(); if (isValidPort(port)) { setPortIfAbsent(port); } return this; }
/** * Returns the port number of the {@link Server} for the specified {@link SessionProtocol}. * * @throws IllegalStateException if the {@link Server} is not started or it did not open a port of the * specified protocol. */ public int port(SessionProtocol protocol) { return server().activePorts().values().stream() .filter(p1 -> p1.hasProtocol(protocol)).findAny() .flatMap(p -> Optional.of(p.localAddress().getPort())) .orElseThrow(() -> new IllegalStateException(protocol + " port not open")); }
@Override public void operationComplete(ChannelFuture f) { final Channel ch = f.channel(); assert ch.eventLoop().inEventLoop(); if (f.isSuccess()) { serverChannels.add(ch); ch.closeFuture() .addListener((ChannelFutureListener) future -> serverChannels.remove(future.channel())); final InetSocketAddress localAddress = (InetSocketAddress) ch.localAddress(); final ServerPort actualPort = new ServerPort(localAddress, port.protocols()); // Update the boss thread so its name contains the actual port. Thread.currentThread().setName(bossThreadName(actualPort)); synchronized (activePorts) { // Update the map of active ports. activePorts.put(localAddress, actualPort); } if (logger.isInfoEnabled()) { if (localAddress.getAddress().isAnyLocalAddress() || localAddress.getAddress().isLoopbackAddress()) { port.protocols().forEach(p -> logger.info( "Serving {} at {} - {}://127.0.0.1:{}/", p.name(), localAddress, p.uriText(), localAddress.getPort())); } else { logger.info("Serving {} at {}", Joiner.on('+').join(port.protocols()), localAddress); } } } } }
port -> checkState(port.protocols().stream().anyMatch(p -> p != PROXY), "protocols: %s (expected: at least one %s or %s)", port.protocols(), HTTP, HTTPS)); ports = resolveDistinctPorts(this.ports); for (final ServerPort p : ports) { if (p.hasTls()) { throw new IllegalArgumentException("TLS not configured; cannot serve HTTPS"); ports = ImmutableList.of(new ServerPort(0, HTTP)); ports = resolveDistinctPorts(this.ports); } else { ports = ImmutableList.of(new ServerPort(0, HTTPS));
@Override protected void initChannel(Channel ch) throws Exception { ChannelUtil.disableWriterBufferWatermark(ch); final ChannelPipeline p = ch.pipeline(); p.addLast(new FlushConsolidationHandler()); p.addLast(ReadSuppressingHandler.INSTANCE); configurePipeline(p, port.protocols(), null); }
/** * Sets a {@link SessionProtocol} and its port number only if it has not been specified before. */ SamlPortConfigBuilder setSchemeAndPortIfAbsent(ServerPort serverPort) { requireNonNull(serverPort, "serverPort"); if (serverPort.hasHttps()) { setSchemeIfAbsent(SessionProtocol.HTTPS); } else if (serverPort.hasHttp()) { setSchemeIfAbsent(SessionProtocol.HTTP); } else { throw new IllegalArgumentException("unexpected session protocol: " + serverPort.protocols()); } // Do not set a port if the port number is 0 which means that the port will be automatically chosen. final int port = serverPort.localAddress().getPort(); if (isValidPort(port)) { setPortIfAbsent(port); } return this; }
buf.append(ServerPort.toString(null, p.localAddress(), p.protocols())); buf.append(", "); hasPorts = true;
@Test public void testPortConfiguration() throws Exception { final Collection<ServerPort> ports = server.activePorts().values(); assertThat(ports.stream().filter(ServerPort::hasHttp)).hasSize(3); assertThat(ports.stream().filter(p -> p.localAddress().getAddress().isAnyLocalAddress())).hasSize(2); assertThat(ports.stream().filter(p -> p.localAddress().getAddress().isLoopbackAddress())).hasSize(1); }
@Override public synchronized void start() { try { if (!isRunning) { server.start().get(); if (port == 0) { // Replace the specified port number with the primary port number. // Server#activePort doesn't return the first added port, so we need to find that. final Optional<ServerPort> port = server.activePorts().values().stream() .filter(p -> p.protocols().contains(protocol)) .filter(p -> address == null || Arrays.equals(address.getAddress(), p.localAddress().getAddress().getAddress())) .findFirst(); assert port.isPresent() : "the primary port doest not exist."; this.port = port.get().localAddress().getPort(); } isRunning = true; } } catch (Exception cause) { throw new WebServerException("Failed to start " + ArmeriaWebServer.class.getSimpleName(), Exceptions.peel(cause)); } }
/** * Adds an HTTPS port that listens on all available network interfaces. * * @param port the HTTPS port number. * * @see #https(InetSocketAddress) * @see <a href="#no_port_specified">What happens if no HTTP(S) port is specified?</a> */ public ServerBuilder https(int port) { return port(new ServerPort(port, HTTPS)); }
private String newUrl(String scheme) { final int port = server.activePort().get().localAddress().getPort(); return scheme + "://127.0.0.1:" + port; }
@Override public void serialize(ServerPort value, JsonGenerator gen, SerializerProvider serializers) throws IOException { final InetSocketAddress localAddr = value.localAddress(); final int port = localAddr.getPort(); final String host; if (localAddr.getAddress().isAnyLocalAddress()) { host = "*"; } else { final String hs = localAddr.getHostString(); if (NetUtil.isValidIpV6Address(hs)) { // Try to get the platform-independent consistent IPv6 address string. host = NetUtil.toAddressString(localAddr.getAddress()); } else { host = hs; } } gen.writeStartObject(); gen.writeObjectFieldStart("localAddress"); gen.writeStringField("host", host); gen.writeNumberField("port", port); gen.writeEndObject(); gen.writeArrayFieldStart("protocols"); for (final SessionProtocol protocol : value.protocols()) { gen.writeString(protocol.uriText()); } gen.writeEndArray(); gen.writeEndObject(); } }
/** * Adds an HTTPS port that listens to the specified {@code localAddress}. * * @param localAddress the local address to bind * * @see #http(int) * @see <a href="#no_port_specified">What happens if no HTTP(S) port is specified?</a> */ public ServerBuilder https(InetSocketAddress localAddress) { return port(new ServerPort(requireNonNull(localAddress, "localAddress"), HTTPS)); }
private String newUrl(String scheme) { final int port = server.activePort().get().localAddress().getPort(); return scheme + "://127.0.0.1:" + port; }
@Override public void serialize(ServerPort value, JsonGenerator gen, SerializerProvider serializers) throws IOException { final InetSocketAddress localAddr = value.localAddress(); final int port = localAddr.getPort(); final String host; if (localAddr.getAddress().isAnyLocalAddress()) { host = "*"; } else { final String hs = localAddr.getHostString(); if (NetUtil.isValidIpV6Address(hs)) { // Try to get the platform-independent consistent IPv6 address string. host = NetUtil.toAddressString(localAddr.getAddress()); } else { host = hs; } } gen.writeStartObject(); gen.writeObjectFieldStart("localAddress"); gen.writeStringField("host", host); gen.writeNumberField("port", port); gen.writeEndObject(); gen.writeArrayFieldStart("protocols"); for (final SessionProtocol protocol : value.protocols()) { gen.writeString(protocol.uriText()); } gen.writeEndArray(); gen.writeEndObject(); } }