Revision c53aa229 trunk/Pithos.Core/PithosMonitor.cs

b/trunk/Pithos.Core/PithosMonitor.cs
25 25
    [Export(typeof(PithosMonitor))]
26 26
    public class PithosMonitor:IDisposable
27 27
    {
28
        private const string PithosContainer = "pithos";
29
        private const string TrashContainer = "trash";
30

  
31
        private const string FragmentsFolder = "fragments";
32

  
33 28
        private int _blockSize;
34 29
        private string _blockHash;
35 30

  
......
42 37
        [Import]
43 38
        public IPithosWorkflow Workflow { get; set; }
44 39

  
45
        [Import]
46 40
        public ICloudClient CloudClient { get; set; }
47 41

  
48 42
        public IStatusNotification StatusNotification { get; set; }
......
60 54
        public string UserName { get; set; }
61 55
        public string ApiKey { get; set; }
62 56

  
63
        private ServiceHost _statusService { get; set; }
57
        private Network.AccountInfo _accountInfo;
58

  
64 59

  
65 60
        private static readonly ILog Log = LogManager.GetLogger(typeof(PithosMonitor));
66 61

  
......
93 88
        private bool _isInitialized;
94 89

  
95 90
        public void Start()
96
        {
91
        {            
92
            if (String.IsNullOrWhiteSpace(ApiKey))
93
                throw new InvalidOperationException("The ApiKey is empty");
94
            if (String.IsNullOrWhiteSpace(UserName))
95
                throw new InvalidOperationException("The UserName is empty");
96
            if (String.IsNullOrWhiteSpace(AuthenticationUrl))
97
                throw new InvalidOperationException("The Authentication url is empty");
98
            Contract.EndContractBlock();
99

  
97 100
            StatusNotification.NotifyChange("Starting");
98 101
            if (_isInitialized)
99 102
            {
......
102 105
            }
103 106
            _cancellationSource = new CancellationTokenSource();
104 107

  
108
            CloudClient=new CloudFilesClient(UserName,ApiKey);
105 109
            var proxyUri = ProxyFromSettings();            
106 110
            CloudClient.Proxy = proxyUri;
107 111
            CloudClient.UsePithos = this.UsePithos;
108
            
112
            CloudClient.AuthenticationUrl = this.AuthenticationUrl;            
113

  
114
            _accountInfo = CloudClient.Authenticate();
115
            _accountInfo.AccountPath = RootPath;
116

  
117

  
109 118
            EnsurePithosContainers();
110 119
            
111 120
            StatusKeeper.BlockHash = _blockHash;
......
114 123
            StatusKeeper.StartProcessing(_cancellationSource.Token);
115 124
            IndexLocalFiles(RootPath);
116 125
            StartWatcherAgent(RootPath);
117
            StartStatusService();
126

  
127
            StartNetworkAgent();
128

  
118 129
            StartWorkflowAgent();
119
            WorkflowAgent.RestartInterruptedFiles();
130
            WorkflowAgent.RestartInterruptedFiles(_accountInfo);
120 131
            _isInitialized = true;
121 132
        }
122 133

  
123 134
        private void EnsurePithosContainers()
124 135
        {
125
            CloudClient.UsePithos = this.UsePithos;
126
            CloudClient.AuthenticationUrl = this.AuthenticationUrl;
127
            CloudClient.Authenticate(UserName, ApiKey);
128 136

  
129 137
            //Create the two default containers if they are missing
130
            var pithosContainers = new List<string>{ TrashContainer,PithosContainer};
138
            var pithosContainers = new List<string>{ FolderConstants.TrashContainer,FolderConstants.PithosContainer};
131 139
            foreach (var container in pithosContainers)
132 140
            {                
133 141
                var info=CloudClient.GetContainerInfo(this.UserName, container);
......
138 146
                }
139 147
                _blockSize = info.BlockSize;
140 148
                _blockHash = info.BlockHash;
149
                _accountInfo.BlockSize = _blockSize;
150
                _accountInfo.BlockHash = _blockHash;
141 151
            }
142

  
143
/*
144
            //Create folders for any other containers
145
            var allContainers = CloudClient.ListContainers();            
146
            pithosContainers.AddRange(new[]{"shared","others"});
147

  
148
            var extraContainers = from container in allContainers
149
                                  where !pithosContainers.Contains(container.Name.ToLower())
150
                                  select container;
151
            
152
            foreach (var container in extraContainers)
153
            {
154
                var containerPath = Path.Combine(this.RootPath, container.Name);
155
                if (!Directory.Exists(containerPath))
156
                    Directory.CreateDirectory(containerPath);
157
            }
158
*/
159

  
160

  
161

  
162 152
        }
163 153

  
164 154
        public string AuthenticationUrl { get; set; }
......
190 180
                Log.Info("START");
191 181
                try
192 182
                {
193
                    var fragmentsPath = Path.Combine(RootPath, FragmentsFolder);
183
                    var fragmentsPath = Path.Combine(RootPath, FolderConstants.FragmentsFolder);
194 184
                    var directory = new DirectoryInfo(path);
195 185
                    var files =
196 186
                        from file in directory.EnumerateFiles("*", SearchOption.AllDirectories)
......
212 202
        }
213 203

  
214 204
        
215
        private void StartStatusService()
216
        {
217
            // Create a ServiceHost for the CalculatorService type and provide the base address.
218
            var baseAddress = new Uri("net.pipe://localhost/pithos");
219
            _statusService = new ServiceHost(typeof(StatusService), baseAddress);
220
            
221
            var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
222
            
223
            _statusService.AddServiceEndpoint(typeof(IStatusService), binding, "net.pipe://localhost/pithos/statuscache");
224
            _statusService.AddServiceEndpoint(typeof (ISettingsService), binding, "net.pipe://localhost/pithos/settings");
225

  
226

  
227
            //// Add a mex endpoint
228
            var smb = new ServiceMetadataBehavior
229
                          {
230
                              HttpGetEnabled = true, 
231
                              HttpGetUrl = new Uri("http://localhost:30000/pithos/mex")
232
                          };
233
            _statusService.Description.Behaviors.Add(smb);
234

  
235

  
236
            _statusService.Open();
237
        }
238

  
239
        private void StopStatusService()
240
        {
241
            if (_statusService == null)
242
                return;
243

  
244
            if (_statusService.State == CommunicationState.Faulted)
245
                _statusService.Abort();
246
            else if (_statusService.State != CommunicationState.Closed)
247
                _statusService.Close();
248
            _statusService = null;
249

  
250
        }
205
  
251 206

  
252 207

  
253 208
        private void StartWorkflowAgent()
......
263 218

  
264 219
            try
265 220
            {
266
                CloudClient.UsePithos = this.UsePithos;
267
                CloudClient.AuthenticationUrl = this.AuthenticationUrl;
268
                CloudClient.Authenticate(UserName, ApiKey);
269

  
270
                StartNetworkAgent(RootPath);
271

  
272 221
                WorkflowAgent.StatusNotification = StatusNotification;
273
                WorkflowAgent.FragmentsPath = Path.Combine(RootPath, FragmentsFolder);
274 222
                WorkflowAgent.Start();                
275 223
            }
276 224
            catch (Exception)
......
321 269

  
322 270
        private Timer timer;
323 271

  
324
        private void StartNetworkAgent(string accountPath)
272
        private void StartNetworkAgent()
325 273
        {
274

  
275
            NetworkAgent.AddAccount(_accountInfo);
276

  
326 277
            NetworkAgent.StatusNotification = StatusNotification;
278
            
327 279

  
328
            NetworkAgent.Start(PithosContainer, TrashContainer,_blockSize,_blockHash);
280
            NetworkAgent.Start();
329 281

  
330
            NetworkAgent.ProcessRemoteFiles(accountPath);
282
            NetworkAgent.ProcessRemoteFiles();
331 283
        }
332 284

  
333 285
        //Make sure a hidden fragments folder exists to store partial downloads
......
359 311
        {
360 312
            FileAgent.StatusKeeper = StatusKeeper;
361 313
            FileAgent.Workflow = Workflow;
362
            FileAgent.FragmentsPath = Path.Combine(RootPath, FragmentsFolder);
363
            FileAgent.Start(path);
314
            FileAgent.FragmentsPath = Path.Combine(RootPath, FolderConstants.FragmentsFolder);
315
            FileAgent.Start(_accountInfo,path);
364 316
        }
365 317

  
366 318
        public void Stop()
367
        {            
368
            FileAgent.Stop();
319
        {          
320
            if (FileAgent!=null)
321
                FileAgent.Stop();
322
            FileAgent = null;
369 323
            if (timer != null)
370 324
                timer.Dispose();
371
            timer = null;
372
            StopStatusService();
325
            timer = null;            
373 326
        }
374 327

  
375 328

  

Also available in: Unified diff