2 /* -----------------------------------------------------------------------
3 * <copyright file="PithosMonitor.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * The views and conclusions contained in the software and
35 * documentation are those of the authors and should not be
36 * interpreted as representing official policies, either expressed
37 * or implied, of GRNET S.A.
39 * -----------------------------------------------------------------------
43 using System.Collections.Concurrent;
44 using System.Collections.Generic;
45 using System.ComponentModel.Composition;
46 using System.Diagnostics;
47 using System.Diagnostics.Contracts;
51 using System.Net.NetworkInformation;
52 using System.Security.Cryptography;
53 using System.ServiceModel.Description;
55 using System.Threading;
56 using System.Threading.Tasks;
57 using Castle.ActiveRecord.Queries;
58 using Microsoft.WindowsAPICodePack.Net;
59 using Pithos.Core.Agents;
60 using Pithos.Interfaces;
61 using System.ServiceModel;
67 [Export(typeof(PithosMonitor))]
68 public class PithosMonitor:IDisposable
70 private int _blockSize;
71 private string _blockHash;
74 public IPithosSettings Settings{get;set;}
76 private IStatusKeeper _statusKeeper;
79 public IStatusKeeper StatusKeeper
81 get { return _statusKeeper; }
84 _statusKeeper = value;
85 FileAgent.StatusKeeper = value;
90 private IPithosWorkflow _workflow;
93 public IPithosWorkflow Workflow
95 get { return _workflow; }
99 FileAgent.Workflow = value;
103 public ICloudClient CloudClient { get; set; }
105 public IStatusNotification StatusNotification { get; set; }
108 public FileAgent FileAgent { get; private set; }
110 private WorkflowAgent _workflowAgent;
113 public WorkflowAgent WorkflowAgent
115 get { return _workflowAgent; }
118 _workflowAgent = value;
119 FileAgent.WorkflowAgent = value;
124 public NetworkAgent NetworkAgent { get; set; }
126 public PollAgent PollAgent { get; set; }
128 public string UserName { get; set; }
129 private string _apiKey;
132 get { return _apiKey; }
136 if (_accountInfo != null)
137 _accountInfo.Token = value;
141 private AccountInfo _accountInfo;
146 private static readonly ILog Log = LogManager.GetLogger(typeof(PithosMonitor));
151 get { return FileAgent.Pause; }
154 FileAgent.Pause = value;
157 StatusKeeper.SetPithosStatus(PithosStatus.SyncPaused);
158 StatusNotification.NotifyChange("Paused");
162 StatusKeeper.SetPithosStatus(PithosStatus.InSynch);
163 StatusNotification.NotifyChange("Synchronizing");
168 private string _rootPath;
169 public string RootPath
171 get { return _rootPath; }
174 _rootPath = String.IsNullOrWhiteSpace(value)
181 CancellationTokenSource _cancellationSource;
183 public PithosMonitor()
185 FileAgent = new FileAgent();
188 private bool _started;
192 if (String.IsNullOrWhiteSpace(ApiKey))
193 throw new InvalidOperationException("The ApiKey is empty");
194 if (String.IsNullOrWhiteSpace(UserName))
195 throw new InvalidOperationException("The UserName is empty");
196 if (String.IsNullOrWhiteSpace(AuthenticationUrl))
197 throw new InvalidOperationException("The Authentication url is empty");
198 Contract.EndContractBlock();
200 //If the account doesn't have a valid path, don't start monitoring but don't throw either
201 if (String.IsNullOrWhiteSpace(RootPath))
205 WorkflowAgent.StatusNotification = StatusNotification;
207 StatusNotification.NotifyChange("Starting");
210 if (!_cancellationSource.IsCancellationRequested)
213 _cancellationSource = new CancellationTokenSource();
216 CloudClient=new CloudFilesClient(UserName,ApiKey);
217 CloudClient.UsePithos = true;
218 CloudClient.AuthenticationUrl = this.AuthenticationUrl;
220 _accountInfo = CloudClient.Authenticate();
221 _accountInfo.SiteUri = AuthenticationUrl;
222 _accountInfo.AccountPath = RootPath;
225 var pithosFolder = Path.Combine(RootPath, FolderConstants.PithosContainer);
226 if (!Directory.Exists(pithosFolder))
227 Directory.CreateDirectory(pithosFolder);
228 //Create the cache folder and ensure it is hidden
229 CreateHiddenFolder(RootPath, FolderConstants.CacheFolder);
231 var policy=CloudClient.GetAccountPolicies(_accountInfo);
233 StatusNotification.NotifyAccount(policy);
234 EnsurePithosContainers();
236 StatusKeeper.BlockHash = _blockHash;
237 StatusKeeper.BlockSize = _blockSize;
239 StatusKeeper.StartProcessing(_cancellationSource.Token);
245 WorkflowAgent.RestartInterruptedFiles(_accountInfo);
249 private void EnsurePithosContainers()
252 //Create the two default containers if they are missing
253 var pithosContainers = new List<string>{ FolderConstants.TrashContainer,FolderConstants.PithosContainer};
254 foreach (var container in pithosContainers)
256 var info=CloudClient.GetContainerInfo(this.UserName, container);
257 if (info == ContainerInfo.Empty)
259 CloudClient.CreateContainer(this.UserName, container);
260 info = CloudClient.GetContainerInfo(this.UserName, container);
262 _blockSize = info.BlockSize;
263 _blockHash = info.BlockHash;
264 _accountInfo.BlockSize = _blockSize;
265 _accountInfo.BlockHash = _blockHash;
269 public string AuthenticationUrl { get; set; }
271 private void IndexLocalFiles()
273 StatusNotification.NotifyChange("Indexing Local Files",TraceLevel.Info);
274 using (log4net.ThreadContext.Stacks["Monitor"].Push("Indexing local files"))
279 var cachePath = Path.Combine(RootPath, FolderConstants.CacheFolder);
280 var directory = new DirectoryInfo(RootPath);
282 from file in directory.EnumerateFiles("*", SearchOption.AllDirectories)
283 where !file.FullName.StartsWith(cachePath, StringComparison.InvariantCultureIgnoreCase) &&
284 !file.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase)
286 StatusKeeper.ProcessExistingFiles(files);
289 catch (Exception exc)
291 Log.Error("[ERROR]", exc);
304 /* private void StartWorkflowAgent()
306 WorkflowAgent.StatusNotification = StatusNotification;
308 /* //On Vista and up we can check for a network connection
309 bool connected=Environment.OSVersion.Version.Major < 6 || NetworkListManager.IsConnectedToInternet;
310 //If we are not connected retry later
313 Task.Factory.StartNewDelayed(10000, StartWorkflowAgent);
319 WorkflowAgent.Start();
323 //Faild to authenticate due to network or account error
324 //Retry after a while
325 Task.Factory.StartNewDelayed(10000, StartWorkflowAgent);
329 internal class LocalFileComparer:EqualityComparer<CloudAction>
331 public override bool Equals(CloudAction x, CloudAction y)
333 if (x.Action != y.Action)
335 if (x.LocalFile != null && y.LocalFile != null && !x.LocalFile.FullName.Equals(y.LocalFile.FullName))
337 if (x.CloudFile != null && y.CloudFile != null )
339 if (x.CloudFile.Hash == null & y.CloudFile.Hash != null)
341 if (x.CloudFile.Hash != null & y.CloudFile.Hash == null)
343 if (x.CloudFile.Hash == null & y.CloudFile.Hash == null)
344 return (x.CloudFile.Name == y.CloudFile.Name);
345 if (!x.CloudFile.Hash.Equals(y.CloudFile.Hash))
348 if (x.CloudFile == null ^ y.CloudFile == null ||
349 x.LocalFile == null ^ y.LocalFile == null)
354 public override int GetHashCode(CloudAction obj)
356 var hash1 = (obj.LocalFile == null) ? int.MaxValue : obj.LocalFile.FullName.GetHashCode();
357 var hash2 = (obj.CloudFile == null) ? int.MaxValue : (obj.CloudFile.Hash ?? obj.CloudFile.Name??"").GetHashCode();
358 var hash3 = obj.Action.GetHashCode();
359 return hash1 ^ hash2 & hash3;
365 private void StartNetworkAgent()
368 NetworkAgent.AddAccount(_accountInfo);
370 NetworkAgent.StatusNotification = StatusNotification;
372 NetworkAgent.Start();
374 PollAgent.StatusNotification = StatusNotification;
376 PollAgent.PollRemoteFiles();
379 //Make sure a hidden cache folder exists to store partial downloads
380 private static string CreateHiddenFolder(string rootPath, string folderName)
382 if (String.IsNullOrWhiteSpace(rootPath))
383 throw new ArgumentNullException("rootPath");
384 if (!Path.IsPathRooted(rootPath))
385 throw new ArgumentException("rootPath");
386 if (String.IsNullOrWhiteSpace(folderName))
387 throw new ArgumentNullException("folderName");
388 Contract.EndContractBlock();
390 var folder = Path.Combine(rootPath, folderName);
391 if (!Directory.Exists(folder))
393 var info = Directory.CreateDirectory(folder);
394 info.Attributes |= FileAttributes.Hidden;
396 Log.InfoFormat("Created cache Folder: {0}", folder);
400 var info = new DirectoryInfo(folder);
401 if ((info.Attributes & FileAttributes.Hidden) == 0)
403 info.Attributes |= FileAttributes.Hidden;
404 Log.InfoFormat("Reset cache folder to hidden: {0}", folder);
413 private void StartWatcherAgent()
415 AgentLocator<FileAgent>.Register(FileAgent,RootPath);
417 FileAgent.StatusKeeper = StatusKeeper;
418 FileAgent.Workflow = Workflow;
419 FileAgent.CachePath = Path.Combine(RootPath, FolderConstants.CacheFolder);
420 FileAgent.Start(_accountInfo, RootPath);
425 AgentLocator<FileAgent>.Remove(RootPath);
441 public void Dispose()
444 GC.SuppressFinalize(this);
447 protected virtual void Dispose(bool disposing)
456 public void MoveFileStates(string oldPath, string newPath)
458 if (String.IsNullOrWhiteSpace(oldPath))
459 throw new ArgumentNullException("oldPath");
460 if (!Path.IsPathRooted(oldPath))
461 throw new ArgumentException("oldPath must be an absolute path","oldPath");
462 if (string.IsNullOrWhiteSpace(newPath))
463 throw new ArgumentNullException("newPath");
464 if (!Path.IsPathRooted(newPath))
465 throw new ArgumentException("newPath must be an absolute path","newPath");
466 Contract.EndContractBlock();
468 StatusKeeper.ChangeRoots(oldPath, newPath);
471 public void AddSelectivePaths(string[] added)
473 /* FileAgent.SelectivePaths.AddRange(added);
474 NetworkAgent.SyncPaths(added);*/
477 public void RemoveSelectivePaths(string[] removed)
479 FileAgent.SelectivePaths.RemoveAll(removed.Contains);
480 foreach (var removedPath in removed.Where(Directory.Exists))
482 Directory.Delete(removedPath,true);
486 public IEnumerable<ObjectInfo> GetRootFolders()
488 var dirs = from container in CloudClient.ListContainers(UserName)
489 from dir in CloudClient.ListObjects(UserName, container.Name, "")
490 where dir.Content_Type=="application/directory"
495 public ObjectInfo GetObjectInfo(string filePath)
497 if (String.IsNullOrWhiteSpace(filePath))
498 throw new ArgumentNullException("filePath");
499 Contract.EndContractBlock();
501 var file=new FileInfo(filePath);
502 string relativeUrl;//=file.AsRelativeUrlTo(this.RootPath);
503 var relativePath = file.AsRelativeTo(RootPath);
505 string accountName,container;
507 var parts=relativePath.Split('\\');
509 var accountInfo = _accountInfo;
510 if (relativePath.StartsWith(FolderConstants.OthersFolder))
512 accountName = parts[1];
513 container = parts[2];
514 relativeUrl = String.Join("/", parts.Splice(3));
515 //Create the root URL for the target account
516 var oldName = UserName;
517 var absoluteUri = _accountInfo.StorageUri.AbsoluteUri;
518 var nameIndex=absoluteUri.IndexOf(oldName);
519 var root=absoluteUri.Substring(0, nameIndex);
521 accountInfo = new AccountInfo
523 UserName = accountName,
524 AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
525 StorageUri = new Uri(root + accountName),
526 BlockHash=accountInfo.BlockHash,
527 BlockSize=accountInfo.BlockSize,
528 Token=accountInfo.Token
533 accountName = this.UserName;
534 container = parts[0];
535 relativeUrl = String.Join("/", parts.Splice(1));
538 var client = new CloudFilesClient(accountInfo);
539 var objectInfo=client.GetObjectInfo(accountName, container, relativeUrl);
543 public Task<ContainerInfo> GetContainerInfo(string filePath)
545 if (String.IsNullOrWhiteSpace(filePath))
546 throw new ArgumentNullException("filePath");
547 Contract.EndContractBlock();
549 var file=new FileInfo(filePath);
550 var relativePath = file.AsRelativeTo(RootPath);
552 string accountName,container;
554 var parts=relativePath.Split('\\');
556 var accountInfo = _accountInfo;
557 if (relativePath.StartsWith(FolderConstants.OthersFolder))
559 accountName = parts[1];
560 container = parts[2];
561 //Create the root URL for the target account
562 var oldName = UserName;
563 var absoluteUri = _accountInfo.StorageUri.AbsoluteUri;
564 var nameIndex=absoluteUri.IndexOf(oldName);
565 var root=absoluteUri.Substring(0, nameIndex);
567 accountInfo = new AccountInfo
569 UserName = accountName,
570 AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
571 StorageUri = new Uri(root + accountName),
572 BlockHash=accountInfo.BlockHash,
573 BlockSize=accountInfo.BlockSize,
574 Token=accountInfo.Token
579 accountName = UserName;
580 container = parts[0];
583 return Task.Factory.StartNew(() =>
585 var client = new CloudFilesClient(accountInfo);
586 var containerInfo = client.GetContainerInfo(accountName, container);
587 return containerInfo;