/// <summary> /// This method is used when the sync lock file is determined to be out /// of date. It will check to see if the manifest.xml file exists and /// check whether it is valid (must be a valid XML file). /// </summary> private void CleanupOldSync(SyncLockInfo syncLockInfo) { Logger.Debug("Sync: Cleaning up a previous failed sync transaction"); int rev = LatestRevision; if (rev >= 0 && !IsValidXmlFile(manifestPath)) { // Time to discover the latest valid revision // If no manifest.xml file exists, that means we've got to // figure out if there are any previous revisions with valid // manifest.xml files around. for (; rev >= 0; rev--) { string revParentPath = GetRevisionDirPath(rev); string manPath = Path.Combine(revParentPath, "manifest.xml"); if (IsValidXmlFile(manPath) == false) { continue; } // Restore a valid manifest path File.Copy(manPath, manifestPath, true); break; } } // Delete the old lock file Logger.Debug("Sync: Deleting expired lockfile"); RemoveLockFile(lockPath); }
public EncryptedFileSystemSyncServer(string localSyncPath, byte[] _password, object _initParam) { myKey = _password; serverPath = localSyncPath; cachePath = Services.NativeApplication.CacheDirectory; cachePath = Path.Combine(cachePath, "sync_temp"); lockPath = Path.Combine(serverPath, "lock"); manifestPath = Path.Combine(serverPath, "manifest.xml"); // setup sync-temp-folder if (!Directory.Exists(cachePath)) { Directory.CreateDirectory(cachePath); } else { // Empty the temp dir Logger.Debug("purging directory " + cachePath); foreach (string oldFile in Directory.GetFiles(cachePath)) { try { File.Delete(oldFile); } catch { } } Logger.Debug("purging done"); } Logger.Debug("setupWorkDir"); SetupWorkDirectory(_initParam); Logger.Debug("setupWorkDir done"); Logger.Debug("getRevision"); newRevision = LatestRevision + 1; newRevisionPath = GetRevisionDirPath(newRevision); Logger.Debug("getRevision done"); Logger.Debug("final stuff"); lockTimeout = new InterruptableTimeout(); lockTimeout.Timeout += LockTimeout; syncLock = new SyncLockInfo(); Logger.Debug("final stuff done"); }
public EncryptedFileSystemSyncServer(string localSyncPath, byte[] _password, object _initParam) { myKey = _password; serverPath = localSyncPath; cachePath = Services.NativeApplication.CacheDirectory; cachePath = Path.Combine(cachePath, "sync_temp"); lockPath = Path.Combine(serverPath, "lock"); manifestPath = Path.Combine(serverPath, "manifest.xml"); // setup sync-temp-folder if (!Directory.Exists(cachePath)) { Directory.CreateDirectory(cachePath); } else { // Empty the temp dir Logger.Debug("purging directory " + cachePath); foreach (string oldFile in Directory.GetFiles(cachePath)) { try { File.Delete(oldFile); } catch { } } Logger.Debug("purging done"); } Logger.Debug("setupWorkDir"); SetupWorkDirectory(_initParam); Logger.Debug("setupWorkDir done"); Logger.Debug("getRevision"); newRevision = LatestRevision + 1; newRevisionPath = GetRevisionDirPath(newRevision); Logger.Debug("getRevision done"); Logger.Debug("final stuff"); lockTimeout = new InterruptableTimeout(); lockTimeout.Timeout += LockTimeout; syncLock = new SyncLockInfo(); Logger.Debug("final stuff done"); }
public FileSystemSyncServer (string localSyncPath) { serverPath = localSyncPath; if (!Directory.Exists (serverPath)) throw new DirectoryNotFoundException (serverPath); cachePath = Services.NativeApplication.CacheDirectory; lockPath = Path.Combine (serverPath, "lock"); manifestPath = Path.Combine (serverPath, "manifest.xml"); newRevision = LatestRevision + 1; newRevisionPath = GetRevisionDirPath (newRevision); lockTimeout = new InterruptableTimeout (); lockTimeout.Timeout += LockTimeout; syncLock = new SyncLockInfo (); }
public FileSystemSyncServer(string localSyncPath) { serverPath = localSyncPath; if (!Directory.Exists(serverPath)) { throw new DirectoryNotFoundException(serverPath); } cachePath = Services.NativeApplication.CacheDirectory; lockPath = Path.Combine(serverPath, "lock"); manifestPath = Path.Combine(serverPath, "manifest.xml"); newRevision = LatestRevision + 1; newRevisionPath = GetRevisionDirPath(newRevision); lockTimeout = new InterruptableTimeout(); lockTimeout.Timeout += LockTimeout; syncLock = new SyncLockInfo(); }
private void UpdateLockFile(SyncLockInfo syncLockInfo) { XmlWriter xml = XmlWriter.Create(lockPath, XmlEncoder.DocumentSettings); try { xml.WriteStartDocument(); xml.WriteStartElement(null, "lock", null); xml.WriteStartElement(null, "transaction-id", null); xml.WriteString(syncLockInfo.TransactionId); xml.WriteEndElement(); xml.WriteStartElement(null, "client-id", null); xml.WriteString(syncLockInfo.ClientId); xml.WriteEndElement(); xml.WriteStartElement(null, "renew-count", null); xml.WriteString(string.Format("{0}", syncLockInfo.RenewCount)); xml.WriteEndElement(); xml.WriteStartElement(null, "lock-expiration-duration", null); xml.WriteString(syncLockInfo.Duration.ToString()); xml.WriteEndElement(); xml.WriteStartElement(null, "revision", null); xml.WriteString(syncLockInfo.Revision.ToString()); xml.WriteEndElement(); xml.WriteEndElement(); xml.WriteEndDocument(); } finally { xml.Close(); } AdjustPermissions(lockPath); // TODO UPLOAD TO WEBDAV HERE }
public virtual bool BeginSyncTransaction() { // Lock expiration: If a lock file exists on the server, a client // will never be able to synchronize on its first attempt. The // client should record the time elapsed if (File.Exists(lockPath)) { SyncLockInfo currentSyncLock = CurrentSyncLock; if (initialSyncAttempt == DateTime.MinValue) { Logger.Debug("Sync: Discovered a sync lock file, wait at least {0} before trying again.", currentSyncLock.Duration); // This is our initial attempt to sync and we've detected // a sync file, so we're gonna have to wait. initialSyncAttempt = DateTime.Now; lastSyncLockHash = currentSyncLock.HashString; return(false); } else if (lastSyncLockHash != currentSyncLock.HashString) { Logger.Debug("Sync: Updated sync lock file discovered, wait at least {0} before trying again.", currentSyncLock.Duration); // The sync lock has been updated and is still a valid lock initialSyncAttempt = DateTime.Now; lastSyncLockHash = currentSyncLock.HashString; return(false); } else { if (lastSyncLockHash == currentSyncLock.HashString) { // The sync lock has is the same so check to see if the // duration of the lock has expired. If it hasn't, wait // even longer. if (DateTime.Now - currentSyncLock.Duration < initialSyncAttempt) { Logger.Debug("Sync: You haven't waited long enough for the sync file to expire."); return(false); } } // Cleanup Old Sync Lock! CleanupOldSync(currentSyncLock); } } // Reset the initialSyncAttempt initialSyncAttempt = DateTime.MinValue; lastSyncLockHash = string.Empty; // Create a new lock file so other clients know another client is // actively synchronizing right now. syncLock.RenewCount = 0; syncLock.Revision = newRevision; UpdateLockFile(syncLock); // 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 lockTimeout.Reset((uint)syncLock.Duration.TotalMilliseconds - 20000); updatedNotes = new List <string>(); deletedNotes = new List <string>(); return(true); }
private void UpdateLockFile(SyncLockInfo syncLockInfo) { XmlWriter xml = XmlWriter.Create(lockPath, XmlEncoder.DocumentSettings); try { xml.WriteStartDocument(); xml.WriteStartElement(null, "lock", null); xml.WriteStartElement(null, "transaction-id", null); xml.WriteString(syncLockInfo.TransactionId); xml.WriteEndElement(); xml.WriteStartElement(null, "client-id", null); xml.WriteString(syncLockInfo.ClientId); xml.WriteEndElement(); xml.WriteStartElement(null, "renew-count", null); xml.WriteString(string.Format("{0}", syncLockInfo.RenewCount)); xml.WriteEndElement(); xml.WriteStartElement(null, "lock-expiration-duration", null); xml.WriteString(syncLockInfo.Duration.ToString()); xml.WriteEndElement(); xml.WriteStartElement(null, "revision", null); xml.WriteString(syncLockInfo.Revision.ToString()); xml.WriteEndElement(); xml.WriteEndElement(); xml.WriteEndDocument(); } finally { xml.Close(); } AdjustPermissions(lockPath); // TODO UPLOAD TO WEBDAV HERE }
/// <summary> /// This method is used when the sync lock file is determined to be out /// of date. It will check to see if the manifest.xml file exists and /// check whether it is valid (must be a valid XML file). /// </summary> private void CleanupOldSync(SyncLockInfo syncLockInfo) { Logger.Debug("Sync: Cleaning up a previous failed sync transaction"); int rev = LatestRevision; if (rev >= 0 && !IsValidXmlFile(manifestPath)) { // Time to discover the latest valid revision // If no manifest.xml file exists, that means we've got to // figure out if there are any previous revisions with valid // manifest.xml files around. for (; rev >= 0; rev--) { string revParentPath = GetRevisionDirPath(rev); string manPath = Path.Combine(revParentPath, "manifest.xml"); if (IsValidXmlFile(manPath) == false) continue; // Restore a valid manifest path File.Copy(manPath, manifestPath, true); break; } } // Delete the old lock file Logger.Debug("Sync: Deleting expired lockfile"); RemoveLockFile(lockPath); }