[gnote] Use InterruptableTimeout in synchronization



commit d96d572a203a8cb56134a597fe41ded5901afd05
Author: Aurimas Äernius <aurisc4 gmail com>
Date:   Sun Aug 5 21:07:56 2012 +0300

    Use InterruptableTimeout in synchronization
    
    Replace Glib::TimeoutSource with utils::InterruptableTimeout.
    Use it to cancel the timeouts.

 src/synchronization/filesystemsyncserver.cpp |   17 +++++++++++++-
 src/synchronization/filesystemsyncserver.hpp |    4 ++-
 src/synchronization/fusesyncserviceaddin.cpp |   28 +++++++------------------
 src/synchronization/fusesyncserviceaddin.hpp |    7 ++---
 4 files changed, 29 insertions(+), 27 deletions(-)
---
diff --git a/src/synchronization/filesystemsyncserver.cpp b/src/synchronization/filesystemsyncserver.cpp
index 4b61779..7ab9c84 100644
--- a/src/synchronization/filesystemsyncserver.cpp
+++ b/src/synchronization/filesystemsyncserver.cpp
@@ -72,6 +72,9 @@ FileSystemSyncServer::FileSystemSyncServer(const std::string & localSyncPath)
 
   m_new_revision = latest_revision() + 1;
   m_new_revision_path = get_revision_dir_path(m_new_revision);
+
+  m_lock_timeout.signal_timeout
+    .connect(sigc::mem_fun(*this, &FileSystemSyncServer::lock_timeout));
 }
 
 
@@ -240,7 +243,7 @@ bool FileSystemSyncServer::begin_sync_transaction()
   // TODO: Verify that the lockTimeout is actually working or figure
   // out some other way to automatically update the lock file.
   // Reset the timer to 20 seconds sooner than the sync lock duration
-  m_lock_timeout = Glib::TimeoutSource::create(m_sync_lock.duration.total_milliseconds() - 20000);
+  m_lock_timeout.reset(m_sync_lock.duration.total_milliseconds() - 20000);
 
   m_updated_notes.clear();
   m_deleted_notes.clear();
@@ -369,6 +372,7 @@ there may be some excess files floating around.  Here's the error:%s\n", e.what(
     // * * * End Cleanup Code * * *
   }
 
+  m_lock_timeout.cancel();
   sharp::file_delete(m_lock_path);// TODO: Errors?
   commitSucceeded = true;// TODO: When return false?
   return commitSucceeded;
@@ -377,7 +381,7 @@ there may be some excess files floating around.  Here's the error:%s\n", e.what(
 
 bool FileSystemSyncServer::cancel_sync_transaction()
 {
-  //m_lock_timeout.cancel(); TODO: what to do with this?
+  m_lock_timeout.cancel();
   sharp::file_delete(m_lock_path);
   return true;
 }
@@ -627,5 +631,14 @@ bool FileSystemSyncServer::is_valid_xml_file(const std::string & xmlFilePath)
 }
 
 
+void FileSystemSyncServer::lock_timeout()
+{
+  m_sync_lock.renew_count++;
+  update_lock_file(m_sync_lock);
+  // Reset the timer to 20 seconds sooner than the sync lock duration
+  m_lock_timeout.reset(m_sync_lock.duration.total_milliseconds() - 20000);
+}
+
+
 }
 }
diff --git a/src/synchronization/filesystemsyncserver.hpp b/src/synchronization/filesystemsyncserver.hpp
index 63dcb84..7949c2a 100644
--- a/src/synchronization/filesystemsyncserver.hpp
+++ b/src/synchronization/filesystemsyncserver.hpp
@@ -22,6 +22,7 @@
 #define _SYNCHRONIZATION_FILESYSTEMSYNCSERVER_HPP_
 
 #include "syncmanager.hpp"
+#include "utils.hpp"
 #include "sharp/datetime.hpp"
 
 
@@ -52,6 +53,7 @@ private:
   void cleanup_old_sync(const SyncLockInfo & syncLockInfo);
   void update_lock_file(const SyncLockInfo & syncLockInfo);
   bool is_valid_xml_file(const std::string & xmlFilePath);
+  void lock_timeout();
 
   std::list<std::string> m_updated_notes;
   std::list<std::string> m_deleted_notes;
@@ -68,7 +70,7 @@ private:
 
   sharp::DateTime m_initial_sync_attempt;
   std::string m_last_sync_lock_hash;
-  Glib::RefPtr<Glib::TimeoutSource> m_lock_timeout;
+  utils::InterruptableTimeout m_lock_timeout;
   SyncLockInfo m_sync_lock;
 };
 
diff --git a/src/synchronization/fusesyncserviceaddin.cpp b/src/synchronization/fusesyncserviceaddin.cpp
index 2fbc8e1..488a378 100644
--- a/src/synchronization/fusesyncserviceaddin.cpp
+++ b/src/synchronization/fusesyncserviceaddin.cpp
@@ -60,6 +60,9 @@ void FuseSyncServiceAddin::initialize()
   if(is_supported()) {
     // Determine mount path, etc
     set_up_mount_path();
+
+    m_unmount_timeout.signal_timeout
+      .connect(sigc::mem_fun(*this, &FuseSyncServiceAddin::unmount_timeout));
   }
   m_initialized = true;
 }
@@ -69,7 +72,7 @@ SyncServer::Ptr FuseSyncServiceAddin::create_sync_server()
   SyncServer::Ptr server;
 
   // Cancel timer
-  m_unmount_timeout.clear();
+  m_unmount_timeout.cancel();
 
   // Mount if necessary
   if(is_configured()) {
@@ -88,7 +91,7 @@ SyncServer::Ptr FuseSyncServiceAddin::create_sync_server()
 void FuseSyncServiceAddin::post_sync_cleanup()
 {
   // Set unmount timeout to 5 minutes or something
-  unmount_timeout_reset(1000 * 60 * 5);
+  m_unmount_timeout.reset(1000 * 60 * 5);
 }
 
 bool FuseSyncServiceAddin::is_supported()
@@ -299,7 +302,7 @@ void FuseSyncServiceAddin::gnote_exit_handler()
   unmount_timeout();
 }
 
-bool FuseSyncServiceAddin::unmount_timeout()
+void FuseSyncServiceAddin::unmount_timeout()
 {
   if(is_mounted()) {
     sharp::Process p;
@@ -315,14 +318,13 @@ bool FuseSyncServiceAddin::unmount_timeout()
     // TODO: What does this return if it was not mounted?
     if(p.exit_code() != 0) {
       DBG_OUT("Error unmounting %s", id().c_str());
-      unmount_timeout_reset(1000 * 60 * 5); // Try again in five minutes
+      m_unmount_timeout.reset(1000 * 60 * 5); // Try again in five minutes
     }
     else {
       DBG_OUT("Successfully unmounted %s", id().c_str());
-      unmount_timeout_cancel();
+      m_unmount_timeout.cancel();
     }
   }
-  return true;
 }
 
 // Checks to see if the mount is actually mounted and alive
@@ -355,19 +357,5 @@ bool FuseSyncServiceAddin::is_mounted()
   return false;
 }
 
-void FuseSyncServiceAddin::unmount_timeout_reset(int timeout)
-{
-  m_unmount_timeout = Glib::TimeoutSource::create(timeout);
-  m_unmount_timeout->connect(sigc::mem_fun(*this, &FuseSyncServiceAddin::unmount_timeout));
-  Gnote::obj().signal_quit.connect(sigc::mem_fun(*this, &FuseSyncServiceAddin::gnote_exit_handler));
-  m_unmount_timeout->attach();
-}
-
-void FuseSyncServiceAddin::unmount_timeout_cancel()
-{
-  m_unmount_timeout->destroy();
-  m_unmount_timeout.reset();
-}
-
 }
 }
diff --git a/src/synchronization/fusesyncserviceaddin.hpp b/src/synchronization/fusesyncserviceaddin.hpp
index de19e2d..9286a0e 100644
--- a/src/synchronization/fusesyncserviceaddin.hpp
+++ b/src/synchronization/fusesyncserviceaddin.hpp
@@ -23,6 +23,7 @@
 #include <glibmm.h>
 
 #include "syncserviceaddin.hpp"
+#include "utils.hpp"
 
 
 namespace gnote {
@@ -62,13 +63,11 @@ private:
   void set_up_mount_path();
   void prepare_mount_path();
   void gnote_exit_handler();
-  bool unmount_timeout();
+  void unmount_timeout();
   bool is_mounted();
-  void unmount_timeout_reset(int timeout);
-  void unmount_timeout_cancel();
 
   std::string m_mount_path;
-  Glib::RefPtr<Glib::TimeoutSource> m_unmount_timeout;
+  utils::InterruptableTimeout m_unmount_timeout;
 
   std::string m_fuse_mount_exe_path;
   std::string m_fuse_unmount_exe_path;



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