amichair commented on code in PR #93:
URL: https://github.com/apache/aries-rsa/pull/93#discussion_r3151650298


##########
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();
+    }
+
+    private void addPeers(Collection<String> peers) {
+        peers.stream()
+            .filter(peer -> !this.peers.contains(peer)) // only new ones
+            .filter(peer -> !localAddress.equals(peer)) // exclude ourself
+            .forEach(peer -> {
+                LOG.info("Adding peer {}", peer);
+                this.peers.add(peer);
+                executor.submit(() -> connectLoop(peer));
+            });
+    }
+
+    private void acceptLoop() {
+        while (true) {
+            try {
+                Socket socket = serverSocket.accept();
+                executor.submit(() -> onConnected(socket, null));
+            } catch (IOException ioe) {
+                return; // socket closed
+            } catch (Throwable t) {
+                LOG.error("Unexpected error in accept loop - shutting down", 
t);
+                return;
+            }
+        }
+    }
+
+    private void connectLoop(String address) {
+        URI uri = toURI(address);
+        while (!closing) {
+            try {
+                Socket socket = new Socket(uri.getHost(), uri.getPort());
+                onConnected(socket, address);
+                return; // connection established; onConnectionClosed will 
restart this loop if needed
+            } catch (IOException ioe) {
+                try {
+                    Thread.sleep(reconnectDelay);
+                } catch (InterruptedException ie) {
+                    // end loop (shutting down)
+                    return;
+                }
+            } catch (Throwable t) {
+                LOG.error("Unexpected error in connect loop - shutting down", 
t);
+                return;

Review Comment:
   I'm not sure there is something that could be done. We won't stop the whole 
component if one connection with a peer goes horribly wrong. It's not an 
expected IOError or such (handled separately). I don't know what might happen 
here, likely a bug in the code or some critical OOME in the system or such. But 
it runs in a separate thread so better catch and log than propagate to 
nowhere... if you can think of some specific other handling here we can change 
it.



-- 
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]

Reply via email to