Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / CloudTransferAction.cs @ d78d765c

History | View | Annotate | Download (9.2 kB)

1
#region
2
/* -----------------------------------------------------------------------
3
 * <copyright file="CloudTransferAction.cs" company="GRNet">
4
 * 
5
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or
8
 * without modification, are permitted provided that the following
9
 * conditions are met:
10
 *
11
 *   1. Redistributions of source code must retain the above
12
 *      copyright notice, this list of conditions and the following
13
 *      disclaimer.
14
 *
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.
19
 *
20
 *
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.
33
 *
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.
38
 * </copyright>
39
 * -----------------------------------------------------------------------
40
 */
41
#endregion
42
using System;
43
using System.Diagnostics.Contracts;
44
using System.IO;
45
using System.Threading;
46
using Pithos.Interfaces;
47
using Pithos.Network;
48

    
49
namespace Pithos.Core.Agents
50
{
51
    public enum CloudActionType
52
    {
53
        MustSynch,
54
        UploadUnconditional,
55
        DownloadUnconditional,
56
        DeleteLocal,
57
        DeleteCloud,
58
        RenameCloud,
59
        RenameLocal
60
    }
61

    
62
    public class CloudAction
63
    {
64
        public AccountInfo AccountInfo { get; set; }
65
        public CloudActionType Action { get; set; }
66
        public FileSystemInfo LocalFile { get; set; }
67
        public ObjectInfo CloudFile { get; set; }
68
        public FileState FileState { get; set; }
69
        public string Container { get; set; }
70

    
71
        public readonly DateTime Created = DateTime.Now;
72

    
73

    
74
        public Lazy<TreeHash> TreeHash { get; protected set; }
75
        //public Lazy<string> TopHash { get; set; }
76

    
77

    
78
        [ContractInvariantMethod]
79
        private void Invariants()
80
        {
81
            Contract.Invariant(AccountInfo!=null);
82
        }
83

    
84
        public bool IsShared
85
        {
86
            get { return  CloudFile!=null && AccountInfo.UserName != CloudFile.Account; }
87
        }
88

    
89
        protected CloudAction(AccountInfo accountInfo,CloudActionType action)
90
        {
91
            if (accountInfo==null)
92
                throw new ArgumentNullException("accountInfo");
93
            Contract.EndContractBlock();
94

    
95
            Action = action;
96
            AccountInfo = accountInfo;
97
        }
98

    
99
        public CloudAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm)
100
            : this(accountInfo,action)
101
        {
102
            if(blockSize<=0)
103
                throw new ArgumentOutOfRangeException("blockSize");
104
            Contract.EndContractBlock();
105
            LocalFile = localFile.WithProperCapitalization();
106
            CloudFile = cloudFile;
107
            FileState = state;
108
            
109
            if (LocalFile == null) 
110
                return;
111

    
112
            TreeHash = new Lazy<TreeHash>(() => Signature.CalculateTreeHash(LocalFile, blockSize,algorithm),
113
                                            LazyThreadSafetyMode.ExecutionAndPublication);
114
        }
115

    
116
        //Calculate the download path for the cloud file
117
        public string GetDownloadPath()
118
        {
119
            if (CloudFile == null)
120
                return String.Empty;
121
            var filePath = CloudFile.RelativeUrlToFilePath(AccountInfo.UserName);
122
            return Path.Combine(AccountInfo.AccountPath, filePath);
123
        }
124

    
125
        public override string ToString()
126
        {
127
            return String.Format("{0}:{1}->{2}", Action, LocalFile.FullName, CloudFile.Name);
128
        }
129

    
130
        protected static ObjectInfo CreateObjectInfoFor(AccountInfo accountInfo, FileSystemInfo fileInfo)
131
        {
132
            if(accountInfo==null)
133
                throw new ArgumentNullException("accountInfo");
134
            if(fileInfo==null)
135
                throw new ArgumentNullException("fileInfo");
136
            Contract.Ensures(Contract.Result<ObjectInfo>()!=null);
137
            Contract.EndContractBlock();
138

    
139
            var capitalizedFileInfo = fileInfo.WithProperCapitalization();
140
            var fullLocalName = capitalizedFileInfo.FullName;
141
            var othersPath = Path.Combine(accountInfo.AccountPath, FolderConstants.OthersFolder);
142
            
143
            var isShared = fullLocalName.StartsWith(othersPath, StringComparison.InvariantCultureIgnoreCase);
144
            if (isShared)
145
            {                
146
                var pathRelativeToOther = fullLocalName.Substring(othersPath.Length + 1);
147
                var otherParts = pathRelativeToOther.Split('\\');
148
                var otherName = otherParts[0];
149
                var otherContainer = otherParts[1];
150
                return new ObjectInfo
151
                           {
152
                               Account = otherName, 
153
                               Container = otherContainer, 
154
                               Name = String.Join("/", otherParts.Splice(2))
155
                           };
156
            }
157
            return new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, fileInfo);
158
        }
159
    }    
160

    
161
    public class CloudDownloadAction:CloudAction
162
    {
163
        public CloudDownloadAction(AccountInfo accountInfo, ObjectInfo cloudFile)
164
            :base(accountInfo,CloudActionType.DownloadUnconditional)
165
        {            
166
            if (String.IsNullOrWhiteSpace(cloudFile.Container))
167
                throw new ArgumentException("CloudFile.Container","cloudFile");
168
            Contract.EndContractBlock();
169

    
170
            CloudFile = cloudFile;
171
        }
172

    
173
        [ContractInvariantMethod]
174
        private void Invariants()
175
        {
176
            Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
177
        }
178

    
179
        public override string ToString()
180
        {
181
            return String.Format("{0}: _ <- {1}", this.Action, this.CloudFile.Name);
182
        }
183
        
184
    }
185
    public class CloudDeleteAction:CloudAction
186
    {
187
        public CloudDeleteAction(AccountInfo accountInfo,FileSystemInfo fileInfo, FileState fileState)
188
            : this(accountInfo,fileInfo,CreateObjectInfoFor(accountInfo, fileInfo),fileState)
189
        {            
190
        }
191

    
192
        public CloudDeleteAction(AccountInfo accountInfo, FileSystemInfo fileInfo,ObjectInfo cloudFile, FileState fileState) 
193
            : base(accountInfo,CloudActionType.DeleteCloud)
194
        {
195
            CloudFile = cloudFile;
196
            LocalFile = fileInfo;
197
            FileState = fileState;
198
        }
199

    
200
        public CloudDeleteAction(CloudAction action)
201
            : this(action.AccountInfo,action.LocalFile,action.CloudFile,action.FileState)
202
        {}
203

    
204
        [ContractInvariantMethod]
205
        private void Invariants()
206
        {
207
            Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
208
        }
209

    
210
        public override string ToString()
211
        {
212
            return String.Format("{0}: _ ->{1}", Action, CloudFile.Name);
213
        }
214

    
215
    }
216

    
217
    public class CloudUploadAction:CloudAction
218
    {
219
        public CloudUploadAction(AccountInfo accountInfo, FileSystemInfo fileInfo, FileState state, int blockSize, string algorithm)
220
            : base(accountInfo, CloudActionType.UploadUnconditional,fileInfo,CreateObjectInfoFor(accountInfo,fileInfo),state,blockSize,algorithm)             
221
        {
222
        }
223

    
224
        [ContractInvariantMethod]
225
        private void Invariants()
226
        {
227
            Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
228
        }
229

    
230
    }
231

    
232
    public class CloudMoveAction:CloudAction
233
    {
234
        public ObjectInfo OldCloudFile { get; set; }
235

    
236
        public FileSystemInfo OldLocalFile { get; set; }
237

    
238
        public CloudMoveAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo oldFile, FileSystemInfo newFile)
239
            :base(accountInfo,action)
240
        {
241
            LocalFile = newFile;
242
            CloudFile = CreateObjectInfoFor(accountInfo, newFile);
243
            
244
            OldLocalFile = oldFile;
245
            OldCloudFile = CreateObjectInfoFor(accountInfo, oldFile);
246

    
247
            //This is a rename operation, a hash will not be used
248
            TreeHash = new Lazy<TreeHash>(() => Network.TreeHash.Empty, LazyThreadSafetyMode.ExecutionAndPublication);
249
        }
250

    
251
        public override string ToString()
252
        {
253
            return String.Format("{0}:{1}->{2}", Action, OldCloudFile.Name, CloudFile.Name);
254
        }
255

    
256
    }
257

    
258
}