amichair commented on code in PR #93: URL: https://github.com/apache/aries-rsa/pull/93#discussion_r3151637717
########## discovery/tcp/src/main/java/org/apache/aries/rsa/discovery/tcp/TcpConnectionManager.java: ########## @@ -0,0 +1,228 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.aries.rsa.discovery.tcp; + +import org.apache.aries.rsa.discovery.tcp.TcpMessage.*; +import org.osgi.service.remoteserviceadmin.EndpointDescription; +import org.osgi.service.remoteserviceadmin.EndpointEvent; +import org.osgi.service.remoteserviceadmin.EndpointEventListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.*; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static org.apache.aries.rsa.discovery.tcp.TcpDiscovery.toURI; + +/** + * Manages all TCP connections for this provider, as well as + * the higher-level TCP discovery protocol logic. + * <p> + * This includes accepting incoming connections on a server socket, + * initiating outgoing connections to configured (or discovered) peers, + * connection retry logic after unexpected disconnection, handling + * incoming messages from connections and handling gossip-discovered peers. + * <p> + * In addition to the protocol-level functionality, it keeps track of + * all known local endpoints and notifies all remote peers about them. + */ +public class TcpConnectionManager implements EndpointEventListener { + private static final Logger LOG = LoggerFactory.getLogger(TcpConnectionManager.class); + + private final InterestManager interestManager; + private final String localAddress; + private final String localUuid; + private final long reconnectDelay; + private final boolean gossip; + + private final ExecutorService executor = Executors.newCachedThreadPool(); // for connect/accept threads + private final Set<TcpConnection> connections = ConcurrentHashMap.newKeySet(); // all connections, including before handshake + private final Map<String, TcpConnection> connectionsByUuid = new ConcurrentHashMap<>(); // connections after handshake (known uuid) + private final Map<String, EndpointDescription> localEndpoints = new ConcurrentHashMap<>(); + private final Set<String> peers = ConcurrentHashMap.newKeySet(); // all configured and discovered (gossip) peer addresses + + private ServerSocket serverSocket; + private volatile boolean closing; + + public TcpConnectionManager(InterestManager interestManager, String localAddress, + String localUuid, long reconnectDelay, boolean gossip) { + this.interestManager = interestManager; + this.localAddress = localAddress; + this.localUuid = localUuid; + this.reconnectDelay = reconnectDelay; + this.gossip = gossip; + } + + public void open(String bindAddress, int port, Collection<String> peers) throws IOException { + serverSocket = new ServerSocket(); + serverSocket.setReuseAddress(true); + serverSocket.bind(new InetSocketAddress(bindAddress, port)); + executor.submit(this::acceptLoop); + addPeers(peers); + } + + public void close() throws IOException { + closing = true; + serverSocket.close(); // acceptLoop will get SocketException + connections.forEach(TcpConnection::close); + executor.shutdownNow(); Review Comment: There is nothing to wait for, it's the server accept loop (blocking), reconnect loop (should stop trying immediately on shutdown), onConnect (no need to perform handshake if shutting down). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
