Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / CloudTransferAction.cs @ 4f6d51d4

History | View | Annotate | Download (5.7 kB)

1
using System;
2
using System.Diagnostics.Contracts;
3
using System.IO;
4
using System.Threading;
5
using Pithos.Interfaces;
6
using Pithos.Network;
7

    
8
namespace Pithos.Core.Agents
9
{
10
    public enum CloudActionType
11
    {
12
        MustSynch,
13
        UploadUnconditional,
14
        DownloadUnconditional,
15
        DeleteLocal,
16
        DeleteCloud,
17
        RenameCloud
18
    }
19

    
20
    public class CloudAction
21
    {
22
        public AccountInfo AccountInfo { get; set; }
23
        public CloudActionType Action { get; set; }
24
        public FileSystemInfo LocalFile { get; set; }
25
        public ObjectInfo CloudFile { get; set; }
26
        public FileState FileState { get; set; }
27
        public string Container { get; set; }
28

    
29

    
30
        public Lazy<string> LocalHash { get; protected set; }
31
        private Lazy<string> _topHash;
32
        public Lazy<string> TopHash
33
        {
34
            get { return _topHash; }
35
            set { _topHash = value; }
36
        }
37

    
38

    
39
        [ContractInvariantMethod]
40
        private void Invariants()
41
        {
42
            Contract.Invariant(AccountInfo!=null);
43
        }
44

    
45
        protected CloudAction(AccountInfo accountInfo,CloudActionType action)
46
        {
47
            if (accountInfo==null)
48
                throw new ArgumentNullException("accountInfo");
49
            Contract.EndContractBlock();
50

    
51
            Action = action;
52
            AccountInfo = accountInfo;
53
        }
54

    
55
        public CloudAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm)
56
            : this(accountInfo,action)
57
        {
58
            LocalFile = localFile;
59
            CloudFile = cloudFile;
60
            FileState = state;
61
            if (LocalFile != null)
62
            {
63

    
64
                LocalHash = new Lazy<string>(() => LocalFile.CalculateHash(blockSize,algorithm),
65
                                             LazyThreadSafetyMode.ExecutionAndPublication);
66
            }
67
        }
68

    
69
        //Calculate the download path for the cloud file
70
        public string GetDownloadPath()
71
        {
72
            if (CloudFile == null)
73
                return String.Empty;
74
            var filePath = CloudFile.RelativeUrlToFilePath(AccountInfo.UserName);
75
            return Path.Combine(AccountInfo.AccountPath, filePath);
76
        }
77

    
78
        public override string ToString()
79
        {
80
            return String.Format("{0}:{1}->{2}", this.Action, this.LocalFile.FullName, this.CloudFile.Name);
81
        }
82
    }    
83

    
84
    public class CloudDownloadAction:CloudAction
85
    {
86
        public CloudDownloadAction(AccountInfo accountInfo, ObjectInfo cloudFile)
87
            :base(accountInfo,CloudActionType.DownloadUnconditional)
88
        {            
89
            if (String.IsNullOrWhiteSpace(cloudFile.Container))
90
                throw new ArgumentException("CloudFile.Container","cloudFile");
91
            Contract.EndContractBlock();
92

    
93
            CloudFile = cloudFile;
94
        }
95

    
96
        [ContractInvariantMethod]
97
        private void Invariants()
98
        {
99
            Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
100
        }
101

    
102
        public override string ToString()
103
        {
104
            return String.Format("{0}: _ <- {1}", this.Action, this.CloudFile.Name);
105
        }
106
        
107
    }
108
    public class CloudDeleteAction:CloudAction
109
    {
110
        public CloudDeleteAction(AccountInfo accountInfo,FileSystemInfo fileInfo, FileState fileState)
111
            : this(accountInfo,new ObjectInfo(accountInfo.AccountPath,accountInfo.UserName,fileInfo),fileState)
112
        {
113
        }
114

    
115
        public CloudDeleteAction(AccountInfo accountInfo, ObjectInfo cloudFile, FileState fileState) 
116
            : base(accountInfo,CloudActionType.DeleteCloud)
117
        {
118
            CloudFile = cloudFile;
119
            FileState = fileState;
120
        }
121

    
122
        public CloudDeleteAction(CloudAction action)
123
            : this(action.AccountInfo,action.CloudFile,action.FileState)
124
        {}
125

    
126
        [ContractInvariantMethod]
127
        private void Invariants()
128
        {
129
            Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
130
        }
131

    
132
        public override string ToString()
133
        {
134
            return String.Format("{0}: _ ->{1}", this.Action, this.CloudFile.Name);
135
        }
136

    
137
    }
138

    
139
    public class CloudUploadAction:CloudAction
140
    {
141
        public CloudUploadAction(AccountInfo accountInfo, FileSystemInfo fileInfo, FileState state, int blockSize, string algorithm)
142
            : base(accountInfo, CloudActionType.UploadUnconditional, fileInfo, 
143
            new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName,fileInfo), state, blockSize, algorithm)
144
        {
145
        }
146

    
147
        [ContractInvariantMethod]
148
        private void Invariants()
149
        {
150
            Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
151
        }
152

    
153
    }
154

    
155
    public class CloudMoveAction:CloudAction
156
    {
157
        public ObjectInfo OldCloudFile { get; set; }
158

    
159
        public FileSystemInfo OldLocalFile { get; set; }
160

    
161
        public CloudMoveAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo oldFile, FileSystemInfo newFile)
162
            :base(accountInfo,action)
163
        {
164
            LocalFile = newFile;
165
            CloudFile = new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, newFile);
166
            
167
            OldLocalFile = oldFile;
168
            OldCloudFile = new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, oldFile);
169

    
170
            //This is a rename operation, a hash will not be used
171
            LocalHash = new Lazy<string>(() => String.Empty, LazyThreadSafetyMode.ExecutionAndPublication);
172
        }
173

    
174
        public override string ToString()
175
        {
176
            return String.Format("{0}:{1}->{2}", this.Action, OldCloudFile.Name, CloudFile.Name);
177
        }
178

    
179
    }
180

    
181
}