From 1255ac936bca7d6a5f8ff00113e4fea207701481 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 1 Jun 2019 13:04:22 +0100 Subject: [PATCH] close connections on shutdown --- .../main/groovy/com/muwire/core/Core.groovy | 4 +++ .../muwire/core/connection/Connection.groovy | 1 - .../core/connection/ConnectionManager.groovy | 2 ++ .../connection/LeafConnectionManager.groovy | 4 +++ .../UltrapeerConnectionManager.groovy | 10 +++++++- gui/griffon-app/lifecycle/Shutdown.groovy | 25 +++++++++++++++++++ 6 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 gui/griffon-app/lifecycle/Shutdown.groovy diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 3118f945..541668e2 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -196,6 +196,10 @@ public class Core { connectionEstablisher.start() hostCache.waitForLoad() } + + public void shutdown() { + connectionManager.shutdown() + } static main(args) { def home = System.getProperty("user.home") + File.separator + ".MuWire" diff --git a/core/src/main/groovy/com/muwire/core/connection/Connection.groovy b/core/src/main/groovy/com/muwire/core/connection/Connection.groovy index 97546775..6996c58e 100644 --- a/core/src/main/groovy/com/muwire/core/connection/Connection.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/Connection.groovy @@ -82,7 +82,6 @@ abstract class Connection implements Closeable { read() } } catch (SocketTimeoutException e) { - close() } catch (Exception e) { log.log(Level.WARNING,"unhandled exception in reader",e) } finally { diff --git a/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy index 157b8025..ae9816cf 100644 --- a/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionManager.groovy @@ -58,6 +58,8 @@ abstract class ConnectionManager { abstract void onConnectionEvent(ConnectionEvent e) abstract void onDisconnectionEvent(DisconnectionEvent e) + + abstract void shutdown() protected void sendPings() { final long now = System.currentTimeMillis() diff --git a/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy index 73d682b4..4395e255 100644 --- a/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/LeafConnectionManager.groovy @@ -71,4 +71,8 @@ class LeafConnectionManager extends ConnectionManager { log.severe("removed destination not present in connection manager ${e.destination.toBase32()}") } + @Override + void shutdown() { + + } } diff --git a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy index 367c61e8..d6306560 100644 --- a/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/UltrapeerConnectionManager.groovy @@ -20,7 +20,7 @@ class UltrapeerConnectionManager extends ConnectionManager { final Map peerConnections = new ConcurrentHashMap() final Map leafConnections = new ConcurrentHashMap() - + UltrapeerConnectionManager() {} public UltrapeerConnectionManager(EventBus eventBus, Persona me, int maxPeers, int maxLeafs, @@ -100,6 +100,14 @@ class UltrapeerConnectionManager extends ConnectionManager { if (removed == null) log.severe("Removed connection not present in either leaf or peer map ${e.destination.toBase32()}") } + + @Override + void shutdown() { + peerConnections.each {k,v -> v.close() } + leafConnections.each {k,v -> v.close() } + peerConnections.clear() + leafConnections.clear() + } void forwardQueryToLeafs(QueryEvent e) { diff --git a/gui/griffon-app/lifecycle/Shutdown.groovy b/gui/griffon-app/lifecycle/Shutdown.groovy new file mode 100644 index 00000000..963f3cf0 --- /dev/null +++ b/gui/griffon-app/lifecycle/Shutdown.groovy @@ -0,0 +1,25 @@ + +import javax.annotation.Nonnull +import javax.inject.Inject + +import org.codehaus.griffon.runtime.core.AbstractLifecycleHandler + +import com.muwire.core.Core + +import griffon.core.GriffonApplication +import groovy.util.logging.Log + +@Log +class Shutdown extends AbstractLifecycleHandler { + @Inject + Shutdown(@Nonnull GriffonApplication application) { + super(application) + } + + @Override + void execute() { + log.info("shutting down") + Core core = application.context.get("core") + core.shutdown() + } +}