Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (9 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<string> LocalHash { get; protected set; }
75
        private Lazy<string> _topHash;
76
        public Lazy<string> TopHash
77
        {
78
            get { return _topHash; }
79
            set { _topHash = value; }
80
        }
81

    
82

    
83
        [ContractInvariantMethod]
84
        private void Invariants()
85
        {
86
            Contract.Invariant(AccountInfo!=null);
87
        }
88

    
89
        public bool IsShared
90
        {
91
            get { return  CloudFile!=null && AccountInfo.UserName != CloudFile.Account; }
92
        }
93

    
94
        protected CloudAction(AccountInfo accountInfo,CloudActionType action)
95
        {
96
            if (accountInfo==null)
97
                throw new ArgumentNullException("accountInfo");
98
            Contract.EndContractBlock();
99

    
100
            Action = action;
101
            AccountInfo = accountInfo;
102
        }
103

    
104
        public CloudAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm)
105
            : this(accountInfo,action)
106
        {
107
            LocalFile = localFile.WithProperCapitalization();
108
            CloudFile = cloudFile;
109
            FileState = state;
110
            if (LocalFile != null)
111
            {
112

    
113
                LocalHash = new Lazy<string>(() => LocalFile.CalculateHash(blockSize,algorithm),
114
                                             LazyThreadSafetyMode.ExecutionAndPublication);
115
            }
116
        }
117

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

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

    
132
        protected static ObjectInfo CreateObjectInfoFor(AccountInfo accountInfo, FileSystemInfo fileInfo)
133
        {
134
            Contract.Requires(accountInfo!=null);
135
            Contract.Requires(fileInfo!=null);
136
            Contract.Ensures(Contract.Result<ObjectInfo>()!=null);
137

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

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

    
169
            CloudFile = cloudFile;
170
        }
171

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

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

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

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

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

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

    
214
    }
215

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

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

    
229
    }
230

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

    
235
        public FileSystemInfo OldLocalFile { get; set; }
236

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

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

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

    
255
    }
256

    
257
}