[geary/wip/789924-network-transition: 2/15] Ensure only one ConnectivityManager instance is used per endpoint.



commit 54194add442c9112452b9d827fb4b777a5d99ba5
Author: Michael James Gratton <mike vee net>
Date:   Sun Nov 5 16:58:41 2017 +1100

    Ensure only one ConnectivityManager instance is used per endpoint.
    
    Reduce the number of
    Add a ConnectivityManager to each endpoint
    
    * src/engine/api/geary-endpoint.vala (Endpoint): Add an endpoint manager
      property, initialise it with the endpoint.
    
    * src/engine/util/util-connectivity-manager.vala (ConnectivityManager):
      Make the Endpoint property a weak ref to avoid a circular dep.
    
    * src/engine/imap/transport/imap-client-session-manager.vala
      (ClientSessionManager): Use the endpoint's connectivity manager rather
      than our own.

 src/engine/api/geary-endpoint.vala                 |    7 ++++-
 .../transport/imap-client-session-manager.vala     |   23 ++++++++-----------
 src/engine/util/util-connectivity-manager.vala     |    5 +++-
 3 files changed, 19 insertions(+), 16 deletions(-)
---
diff --git a/src/engine/api/geary-endpoint.vala b/src/engine/api/geary-endpoint.vala
index cded061..596754e 100644
--- a/src/engine/api/geary-endpoint.vala
+++ b/src/engine/api/geary-endpoint.vala
@@ -40,6 +40,7 @@ public class Geary.Endpoint : BaseObject {
     }
     
     public NetworkAddress remote_address { get; private set; }
+    public ConnectivityManager connectivity { get; private set; }
     public Flags flags { get; private set; }
     public uint timeout_sec { get; private set; }
     public TlsCertificateFlags tls_validation_flags { get; set; default = TlsCertificateFlags.VALIDATE_ALL; }
@@ -120,13 +121,15 @@ public class Geary.Endpoint : BaseObject {
      * @see tls_validation_warnings
      */
     public signal void untrusted_host(SecurityType security, TlsConnection cx);
-    
+
+
     public Endpoint(string host_specifier, uint16 default_port, Flags flags, uint timeout_sec) {
         this.remote_address = new NetworkAddress(host_specifier, default_port);
         this.flags = flags;
         this.timeout_sec = timeout_sec;
+        this.connectivity = new ConnectivityManager(this);
     }
-    
+
     private SocketClient get_socket_client() {
         if (socket_client != null)
             return socket_client;
diff --git a/src/engine/imap/transport/imap-client-session-manager.vala 
b/src/engine/imap/transport/imap-client-session-manager.vala
index 3400dd0..3dac334 100644
--- a/src/engine/imap/transport/imap-client-session-manager.vala
+++ b/src/engine/imap/transport/imap-client-session-manager.vala
@@ -56,7 +56,6 @@ public class Geary.Imap.ClientSessionManager : BaseObject {
 
     private AccountInformation account_information;
     private Endpoint endpoint;
-       private ConnectivityManager connectivity;
     private Gee.HashSet<ClientSession> sessions = new Gee.HashSet<ClientSession>();
     private int pending_sessions = 0;
     private Nonblocking.Mutex sessions_mutex = new Nonblocking.Mutex();
@@ -79,20 +78,21 @@ public class Geary.Imap.ClientSessionManager : BaseObject {
         this.endpoint.notify[Endpoint.PROP_TRUST_UNTRUSTED_HOST].connect(on_imap_trust_untrusted_host);
         this.endpoint.untrusted_host.connect(on_imap_untrusted_host);
 
-               this.connectivity = new ConnectivityManager(this.endpoint);
-               this.connectivity.notify["is-reachable"].connect(on_connectivity_change);
-               this.connectivity.check_reachable.begin();
+               this.endpoint.connectivity.notify["is-reachable"].connect(on_connectivity_change);
+        if (!this.endpoint.connectivity.is_reachable) {
+            this.endpoint.connectivity.check_reachable.begin();
+        }
     }
 
     ~ClientSessionManager() {
         if (is_open)
             warning("Destroying opened ClientSessionManager");
 
-        account_information.notify["imap-credentials"].disconnect(on_imap_credentials_notified);
-        endpoint.untrusted_host.disconnect(on_imap_untrusted_host);
-        endpoint.notify[Endpoint.PROP_TRUST_UNTRUSTED_HOST].disconnect(on_imap_trust_untrusted_host);
-               this.connectivity.cancel_check();
-               this.connectivity = null;
+        this.account_information.notify["imap-credentials"].disconnect(on_imap_credentials_notified);
+        this.endpoint.untrusted_host.disconnect(on_imap_untrusted_host);
+        this.endpoint.notify[Endpoint.PROP_TRUST_UNTRUSTED_HOST].disconnect(on_imap_trust_untrusted_host);
+               this.endpoint.connectivity.cancel_check();
+        
     }
 
     public async void open_async(Cancellable? cancellable) throws Error {
@@ -499,10 +499,7 @@ public class Geary.Imap.ClientSessionManager : BaseObject {
     }
 
        private void on_connectivity_change() {
-               this.is_endpoint_reachable = this.connectivity.is_reachable;
-               debug("Host %s became %s",
-              this.endpoint.to_string(),
-              this.is_endpoint_reachable ? "reachable" : "unreachable");
+               this.is_endpoint_reachable = this.endpoint.connectivity.is_reachable;
                if (this.is_endpoint_reachable) {
             this.adjust_session_pool.begin();
                }
diff --git a/src/engine/util/util-connectivity-manager.vala b/src/engine/util/util-connectivity-manager.vala
index 464d581..92f6767 100644
--- a/src/engine/util/util-connectivity-manager.vala
+++ b/src/engine/util/util-connectivity-manager.vala
@@ -23,7 +23,8 @@ public class Geary.ConnectivityManager : BaseObject {
        /** Determines if the managed endpoint is currently reachable. */
        public bool is_reachable { get; private set; default = false; }
 
-    private Endpoint endpoint;
+    // Weak to avoid a circular ref with the endpoint
+    private weak Endpoint endpoint;
 
     private NetworkMonitor monitor;
 
@@ -125,6 +126,8 @@ public class Geary.ConnectivityManager : BaseObject {
                // change. 0.36 fixes that, so pull this out when we can
                // depend on that as a minimum.
                if (this.is_reachable != reachable) {
+            debug("Host %s became %s",
+                  this.endpoint.to_string(), reachable ? "reachable" : "unreachable");
                        this.is_reachable = reachable;
                }
        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]