Пример #1
0
            public void Communication()
            {
                lock (_downloadNodeHash)
                {
                    _downloadNodeHash.Add(key.FileLocation.NodeID);
                }

                NodeListViewItem nlvi = null;
                string stringHash = "";
                int i = -1;

                IFileShareService proxy = null;
                try
                {
                    stringHash = BinaryEditor.BytesToHexString(item.Hash);
                    Cache cache;

                    lock (Settings.Default._cacheController.CacheList)
                    {
                        cache = Settings.Default._cacheController.CacheList.
                            FirstOrDefault(n => BinaryEditor.ArrayEquals(n.SignatureHash, item.SignatureHash));
                    }

                    lock (_downloadBlockDic)
                    {
                        if (!_downloadBlockDic.ContainsKey(stringHash))
                            _downloadBlockDic.Add(stringHash, new List<int>());
                    }

                    nlvi = new NodeListViewItem()
                    {
                        CommunicationType = "download",
                        Description = "キャッシュブロックを受信しています",
                        Node = key.FileLocation,
                    };
                    NodeShowAdd(this, nlvi);

                    using (ChannelFactory<IFileShareService> channel = new ChannelFactory<IFileShareService>("Tcp_Client", key.FileLocation.Endpoint))
                    {
                        proxy = channel.CreateChannel();

                        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);
                            }
                        }

                        for (; ; )
                        {
                            lock (_downloadBlockDic)
                            {
                                List<int> bmp = new List<int>();

                                for (int j = 0; j < item.CacheBlockLength; j++)
                                {
                                    if ((!Settings.Default._cacheController.ContainsKey(stringHash) ||
                                            Settings.Default._cacheController[stringHash][j] == null) && key.CacheBlockBitmap[j] == true &&
                                            !_downloadBlockDic[stringHash].Any(n => n == j))
                                    {
                                        bmp.Add(j);
                                    }
                                }

                                if (bmp.Count == 0) return;

                                i = bmp[new Random().Next(bmp.Count)];
                                _downloadBlockDic[stringHash].Add(i);
                            }

                            byte[] block = proxy.GetCacheBlock(item.Hash, i);
                            DebugWrite(this, "download成功:キャッシュブロックのダウンロードに成功しました");

                            if (block != null)
                                Settings.Default._cacheController[cache, i] = block;

                            lock (_downloadBlockDic)
                            {
                                _downloadBlockDic[stringHash].Remove(i);
                            }
                        }
                    }
                }
                catch (EndpointNotFoundException ex)
                {
                    Debug.WriteLine("DownloadCommunication" + ex.Message);
                }
                catch (TimeoutException ex)
                {
                    Debug.WriteLine("DownloadCommunication" + ex.Message);
                }
                catch (FaultException ex)
                {
                    Debug.WriteLine("DownloadCommunication" + ex.Message);
                }
                catch (CommunicationException ex)
                {
                    Debug.WriteLine("DownloadCommunication" + ex.Message);
                }
                catch (NullReferenceException ex)
                {
                    Debug.WriteLine("DownloadCommunication" + ex.Message);
                }
                catch (ArgumentNullException ex)
                {
                    Debug.WriteLine("DownloadCommunication" + ex.Message);
                }
                catch (ArgumentException ex)
                {
                    Debug.WriteLine("DownloadCommunication" + ex.Message);
                }
                catch (ApplicationException ex)
                {
                    DebugWrite(this, "DownloadCommunication:" + ex.Message);

                    Debug.WriteLine("DownloadCommunication" + ex.Message);
                }
                finally
                {
                    if (proxy != null)
                    {
                        try
                        {
                            ((IClientChannel)proxy).Close();
                        }
                        catch
                        {
                            ((IClientChannel)proxy).Abort();
                        }
                    }

                    lock (_downloadBlockDic)
                    {
                        if (i != -1)
                            _downloadBlockDic[stringHash].Remove(i);
                    }

                    if (nlvi != null)
                        NodeShowRemove(this, nlvi);

                    lock (_downloadNodeHash)
                    {
                        _downloadNodeHash.RemoveAll(n => BinaryEditor.ArrayEquals(n, key.FileLocation.NodeID));
                    }
                }
            }