public CacheListViewItem(Cache cache) { this.Category = cache.Category; this.CreationTime = cache.CreationTime; this.Hash = cache.Hash; this.Name = cache.Name; this.Sign = cache.Sign; this.Signature = cache.Signature; this.Signature_SHA1 = cache.Signature_SHA1; this.SignatureHash = cache.SignatureHash; this.Size = cache.Size; this.CacheBlockLength = cache.CacheBlockHash.Length; this.PublicKey = cache.PublicKey; this.UploadBlockBitmap = new bool[this.CacheBlockLength]; this.Cache = cache; }
public byte[] GetCacheBlock(byte[] hash, int index) { if (hash == null) return null; DebugWrite(this, "FileShareService.GetCacheBlock:キャッシュブロック検索依頼を受けました"); try { Cache cache; lock (Settings.Default._cacheController.CacheList) { cache = Settings.Default._cacheController.CacheList.First(n => BinaryEditor.ArrayEquals(n.Hash, hash)); } return Settings.Default._cacheController[cache, index]; } catch (InvalidOperationException ex) { DebugWrite(this, "FileShareService.GetCacheBlock:" + ex.Message); Debug.WriteLine(ex.Message); } catch (ApplicationException ex) { DebugWrite(this, "FileShareService.GetCacheBlock:" + ex.Message); Debug.WriteLine(ex.Message); } DebugWrite(this, "FileShareService.GetCacheBlock:キャッシュブロックの中継依頼を受けました"); foreach (Key key in Settings.Default._keyController.Search(hash)) { if (BinaryEditor.ArrayEquals(key.FileLocation.NodeID, Settings.Default._routeTable.MyNode.NodeID)) continue; if (key.CacheBlockBitmap == null || key.CacheBlockBitmap[index] == false) continue; IFileShareService proxy = null; try { using (ChannelFactory<IFileShareService> channel = new ChannelFactory<IFileShareService>("Tcp_Client", key.FileLocation.Endpoint)) { proxy = channel.CreateChannel(); Cache cache; lock (Settings.Default._cacheController.CacheList) { cache = Settings.Default._cacheController.CacheList. FirstOrDefault(n => BinaryEditor.ArrayEquals(n.Hash, key.Hash)); } if (cache == null) { cache = new Cache(); cache.Key = Clone.DeepCopyClone<Key>(key); cache.Key.FileLocation = new Node(); cache.Key.Review = null; cache.Category = key.Cache_Category; cache.CreationTime = key.Cache_CreationTime; cache.Name = key.Cache_FileName; cache.Sign = key.Cache_Sign; cache.Size = key.Cache_Size; cache.Signature = key.Cache_Signature; cache.Signature_SHA1 = key.Cache_Signature_SHA1; cache.PublicKey = key.PublicKey; cache.CacheBlockHash = proxy.GetCacheBlockHashList(key.Cache_Hash); if (!cache.VerifyDigitalSignature()) throw new ApplicationException("電子署名が不正です"); lock (Settings.Default._cacheController.CacheList) { Settings.Default._cacheController.CacheList.Add(cache); } } byte[] block = proxy.GetCacheBlock(hash, index); if (block == null) continue; Settings.Default._cacheController[cache, index] = block; return block; } } catch (EndpointNotFoundException ex) { continue; } catch (TimeoutException ex) { continue; } catch (FaultException ex) { continue; } catch (CommunicationException ex) { continue; } catch (NullReferenceException ex) { continue; } catch (ArgumentNullException ex) { continue; } catch (ApplicationException ex) { continue; } catch (ArgumentException ex) { continue; } finally { if (proxy != null) { try { ((IClientChannel)proxy).Close(); } catch { ((IClientChannel)proxy).Abort(); } } } } return null; }
public void SetCacheBlock(Cache cache, byte[] value, DateTime createTime) { if (cache == null || value == null || createTime == null) return; DebugWrite(this, "FileShareService.SetCacheBlock:キャッシュブロックを受信しました"); byte[] valueHash = HashFunction.HashCreate(value); try { if (Settings.Default._cacheController.Contains(cache, valueHash)) return; Settings.Default._cacheController[cache, valueHash] = value; Settings.Default.UploadDiffusionList.Add(new UploadDiffusion() { Cache = cache, CacheBlockHash = valueHash, CreateTime = createTime }); } catch (ApplicationException ex) { DebugWrite(this, "FileShareService.SetCacheBlock:" + ex.Message); Debug.WriteLine(ex.Message); } }
public byte[][] GetCacheBlockHashList(byte[] hash) { Cache cache ; lock (Settings.Default._cacheController.CacheList) { cache = Settings.Default._cacheController.CacheList. FirstOrDefault(n => BinaryEditor.ArrayEquals(n.Hash, hash)); } if (cache != null && cache.CacheBlockHash != null) { DebugWrite(this, "FileShareService.GetCacheBlockHashList:キャッシュブロックのハッシュリストの検索依頼を受けました"); return cache.CacheBlockHash; } // キャッシュブロックハッシュリストが存在しない場合、中継する else { foreach (Key key in Settings.Default._keyController.Search(hash)) { if (BinaryEditor.ArrayEquals(Settings.Default._routeTable.MyNode.NodeID, key.FileLocation.NodeID)) continue; IFileShareService proxy = null; try { using (ChannelFactory<IFileShareService> channel = new ChannelFactory<IFileShareService>("Tcp_Client", key.FileLocation.Endpoint)) { proxy = channel.CreateChannel(); cache = new Cache(); cache.Key = Clone.DeepCopyClone<Key>(key); cache.Key.FileLocation = new Node(); cache.Key.Review = null; cache.Category = key.Cache_Category; cache.CreationTime = key.Cache_CreationTime; cache.Name = key.Cache_FileName; cache.Sign = key.Cache_Sign; cache.Size = key.Cache_Size; cache.Signature = key.Cache_Signature; cache.Signature_SHA1 = key.Cache_Signature_SHA1; cache.PublicKey = key.PublicKey; cache.CacheBlockHash = proxy.GetCacheBlockHashList(key.Cache_Hash); if (!cache.VerifyDigitalSignature()) throw new ApplicationException("電子署名が不正です"); lock (Settings.Default._cacheController.CacheList) { Settings.Default._cacheController.CacheList.Add(cache); } DebugWrite(this, "FileShareService.GetCacheBlockHashList:キャッシュブロックのハッシュリストの中継依頼を受けました"); return cache.CacheBlockHash; } } catch (EndpointNotFoundException ex) { continue; } catch (TimeoutException ex) { continue; } catch (FaultException ex) { continue; } catch (CommunicationException ex) { continue; } catch (NullReferenceException ex) { continue; } catch (ArgumentNullException ex) { continue; } catch (ApplicationException ex) { continue; } finally { if (proxy != null) { try { ((IClientChannel)proxy).Close(); } catch { ((IClientChannel)proxy).Abort(); } } } } } return null; }
/// <summary> /// Cacheを追加します /// </summary> public void Add(Cache item, Node node) { if (item == null) return; if (item.Key == null) return; Key key = item.Key; key.FileLocation = node; key.KeyCreateTime = DateTime.Now; key.CacheBlockBitmap = new bool[item.CacheBlockHash.Length]; string stringHash = BinaryEditor.BytesToHexString(item.Hash); for (int i = 0; i < item.CacheBlockHash.Length; i++) { if (Settings.Default._cacheController.ContainsKey(stringHash) && Settings.Default._cacheController[stringHash][i] != null) { key.CacheBlockBitmap[i] = true; } } lock (_myReviewDic) { string stringSignatureHash = BinaryEditor.BytesToHexString(item.SignatureHash); if (_myReviewDic.ContainsKey(stringSignatureHash)) key.Review = _myReviewDic[stringSignatureHash]; } this.Add(key); }