Replaced Merkle hash with MD5 for change checking
[pithos-ms-client] / trunk / Pithos.Core / Agents / NetworkAgent.cs
index e1b9b16..47e0ddc 100644 (file)
@@ -63,10 +63,10 @@ namespace Pithos.Core.Agents
     {
         private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 
-        private Agent<CloudAction> _agent;
+        //private Agent<CloudAction> _agent;
 
         [System.ComponentModel.Composition.Import]
-        private DeleteAgent DeleteAgent { get; set; }
+        public DeleteAgent DeleteAgent { get; set; }
 
         [System.ComponentModel.Composition.Import]
         public IStatusKeeper StatusKeeper { get; set; }
@@ -121,7 +121,6 @@ namespace Pithos.Core.Agents
         //Essentially it stops the poll agent to give priority to the network agent
         //Initially the event is signalled because we don't need to pause
         private readonly AsyncManualResetEvent _proceedEvent = new AsyncManualResetEvent(true);
-        private Agents.Selectives _selectives;
         private bool _pause;
 
         public AsyncManualResetEvent ProceedEvent
@@ -154,7 +153,7 @@ namespace Pithos.Core.Agents
             
         }
 
-        public void Start()
+        /*public void Start()
         {
             if (_agent != null)
                 return;
@@ -176,8 +175,9 @@ namespace Pithos.Core.Agents
                 loop();
             });
 
-        }
+        }*/
 
+/*
         private async Task Process(CloudAction action)
         {
             if (action == null)
@@ -219,7 +219,9 @@ namespace Pithos.Core.Agents
                         {
                             case CloudActionType.UploadUnconditional:
                                 //Abort if the file was deleted before we reached this point
-                                await Uploader.UploadCloudFile(action,CurrentOperationCancelToken);
+                                var uploadAction = (CloudUploadAction) action;
+                                ProcessChildUploads(uploadAction);
+                                await Uploader.UploadCloudFile(uploadAction ,CurrentOperationCancelToken);
                                 break;
                             case CloudActionType.DownloadUnconditional:
                                 await Downloader.DownloadCloudFile(accountInfo, cloudFile, downloadPath, CurrentOperationCancelToken);
@@ -260,7 +262,7 @@ namespace Pithos.Core.Agents
                         Log.WarnFormat("[REQUEUE] {0} : {1} -> {2}", action.Action, action.LocalFile, action.CloudFile);
                     }
                 }
-*/
+#1#
                 catch (OperationCanceledException ex)
                 {                    
                     Log.WarnFormat("Cancelling [{0}]",ex);
@@ -294,8 +296,47 @@ namespace Pithos.Core.Agents
                 }
             }
         }
+*/
 
-        private CancellationToken CurrentOperationCancelToken
+    /*    private void ProcessChildUploads(CloudUploadAction uploadAction)
+        {
+            if (!uploadAction.IsCreation || !(uploadAction.LocalFile is DirectoryInfo)) 
+                return;
+
+            var dirInfo = uploadAction.LocalFile as DirectoryInfo;
+
+            var account = uploadAction.AccountInfo;
+            var folderActions = from info in dirInfo.EnumerateDirectories("*", SearchOption.AllDirectories)                          
+                          select
+                              new CloudUploadAction(account, info, null, account.BlockSize, account.BlockHash,
+                                                    uploadAction, true);
+            var fileActions = from info in dirInfo.EnumerateFiles("*", SearchOption.AllDirectories)                          
+                          select
+                              new CloudUploadAction(account, info, null, account.BlockSize, account.BlockHash,
+                                                    uploadAction, true);            
+            //Post folder actions first, to ensure the selective folders are updated
+            folderActions.ApplyAction(PostUploadAction);
+            fileActions.ApplyAction(PostUploadAction);            
+        }
+*/
+/*
+        private void PostUploadAction(CloudUploadAction action)
+        {
+            var state = StatusKeeper.GetStateByFilePath(action.LocalFile.FullName);
+            if (state != null)
+                state.Delete();
+            //StatusKeeper.SetFileState(action.LocalFile.FullName,FileStatus.Created,FileOverlayStatus.Normal,String.Empty);
+            state = FileState.CreateFor(action.LocalFile);
+            //StatusKeeper.SetFileStatus();
+            state.FileStatus = FileStatus.Created;
+            state.OverlayStatus = FileOverlayStatus.Normal;
+            state.Create();
+            action.FileState = state;
+            Post(action);
+        }
+*/
+
+        public CancellationToken CurrentOperationCancelToken
         {
             get { return _currentOperationCancellation.Token; }
         }
@@ -366,7 +407,7 @@ namespace Pithos.Core.Agents
             }            
         }
 
-        private async Task SyncFiles(AccountInfo accountInfo,CloudAction action)
+/*        private async Task SyncFiles(AccountInfo accountInfo,CloudAction action)
         {
             if (accountInfo == null)
                 throw new ArgumentNullException("accountInfo");
@@ -386,7 +427,7 @@ namespace Pithos.Core.Agents
                 var cloudFile = action.CloudFile;
                 var downloadPath = action.LocalFile.GetProperCapitalization();
 
-                var cloudHash = cloudFile.Hash.ToLower();
+                var cloudHash = cloudFile.X_Object_Hash.ToLower();
                 var previousCloudHash = cloudFile.PreviousHash == null?null: cloudFile.PreviousHash.ToLower();
                 var localHash = action.TreeHash.Value.TopHash.ToHashString();// LocalHash.Value.ToLower();
                 //var topHash = action.TopHash.Value.ToLower();
@@ -421,7 +462,7 @@ namespace Pithos.Core.Agents
                 // If the previous tophash matches the local tophash, the file was only changed on the server. 
                 if (localHash == previousCloudHash)
                 {
-                    await Downloader.DownloadCloudFile(accountInfo, cloudFile, downloadPath, CurrentOperationCancelToken);
+                    await Downloader.DownloadCloudFile(accountInfo, cloudFile, downloadPath CurrentOperationCancelToken);
                 }
                 else
                 {
@@ -430,7 +471,7 @@ namespace Pithos.Core.Agents
                     ReportConflictForMismatch(downloadPath);
                 }
             }
-        }
+        }*/
 
         private void ReportConflictForMismatch(string downloadPath)
         {
@@ -445,6 +486,7 @@ namespace Pithos.Core.Agents
             StatusNotification.NotifyChange(message, TraceLevel.Warning);
         }
 
+/*
         public void Post(CloudAction cloudAction)
         {
             if (cloudAction == null)
@@ -460,21 +502,26 @@ namespace Pithos.Core.Agents
             else
                 _agent.Post(cloudAction);
         }
+*/
        
 
+/*
         public IEnumerable<CloudAction> GetEnumerable()
         {
             return _agent.GetEnumerable();
         }
+*/
 
         public Task GetDeleteAwaiter()
         {
             return DeleteAgent.ProceedEvent.WaitAsync();
         }
+/*
         public CancellationToken CancellationToken
         {
             get { return _agent.CancellationToken; }
         }
+*/
 
         public bool Pause
         {
@@ -527,7 +574,7 @@ namespace Pithos.Core.Agents
 
 
                 //The local file is already renamed
-                StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Modified);
+                StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Modified).Wait();
 
 
                 var account = action.CloudFile.Account ?? accountInfo.UserName;
@@ -538,7 +585,7 @@ namespace Pithos.Core.Agents
                 client.MoveObject(account, container, action.OldCloudFile.Name, container, action.CloudFile.Name);
 
                 StatusKeeper.SetFileStatus(newFilePath, FileStatus.Unchanged);
-                StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Normal);
+                StatusKeeper.SetFileOverlayStatus(newFilePath, FileOverlayStatus.Normal).Wait();
                 NativeMethods.RaiseChangeNotification(newFilePath);
             }
         }