Added log4net for client profile
[pithos-ms-client] / trunk / Pithos.Network / TreeHash.cs
index 2f036b1..6a6dfdd 100644 (file)
@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.Diagnostics.Contracts;
 using System.IO;
 using System.Text;
 using System.Threading.Tasks;
@@ -16,7 +17,27 @@ namespace Pithos.Network
         private const int DEFAULT_BLOCK_SIZE = 4*1024*1024;
         public string BlockHash { get; set; }
         public int BlockSize { get; set; }
-        public long Bytes { get; set; }
+        
+        private long _bytes;
+        public long Bytes
+        {
+            get
+            {
+                Contract.Ensures(Contract.Result<long>() >= 0);
+                return _bytes;
+            }
+            set
+            {
+                if (value<0)
+                    throw new ArgumentOutOfRangeException("Bytes");
+                Contract.Requires(value >= 0);
+                Contract.EndContractBlock();
+                
+                _bytes = value;
+            }
+        }
+        
+
 
         public Guid FileId { get; set; }
 
@@ -27,17 +48,25 @@ namespace Pithos.Network
         }
 
         private IList<byte[]> _hashes;
+
         public IList<byte[]> Hashes
         {
             get { return _hashes; }
             set
             {
                 _hashes = value;
-                _topHash.Force();                
+                _topHash.Force();
             }
         }
 
-        public TreeHash(string algorithm)
+        [ContractInvariantMethod]
+        private void Invariants()
+        {
+            Contract.Invariant(_bytes>=0);
+        }
+        
+
+       public TreeHash(string algorithm)
         {
             BlockHash = algorithm;            
             _topHash = new Lazy<byte[]>(() =>
@@ -104,6 +133,10 @@ namespace Pithos.Network
         //Saves the Json representation to a file
         public Task Save(string filePath)
         {
+            if (String.IsNullOrWhiteSpace(filePath))
+                throw new ArgumentNullException("filePath");
+            Contract.EndContractBlock();
+
             var fileName = FileId.ToString("N");
             var path = Path.Combine(filePath, fileName);
             if (!Directory.Exists(filePath))
@@ -139,9 +172,12 @@ namespace Pithos.Network
         public static TreeHash Parse(string json)
         {
             if (String.IsNullOrWhiteSpace(json))
-                return Empty;
+                return Empty;            
 
             var value = JsonConvert.DeserializeObject<JObject>(json);
+            if (value==null)
+                throw new ArgumentException("The json parameter doesn't contain any json data","json");
+            Contract.Assume(value!=null);
 
             var blockHash = (string) value["block_hash"];
             var size = value.Value<int>("block_size");