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; }
@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); }
@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); } } } } }
@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)); } }
/** * 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;
@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(); } }
@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(); } }
@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(); } }
/** * 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; }