Beispiel #1
0
        private Key[] QueryCommunication(Node node, string query)
        {
            List<Key> queryList = new List<Key>();

            NodeListViewItem nlvi = null;

            IFileShareService proxy = null;
            try
            {
                nlvi = new NodeListViewItem()
                {
                    CommunicationType = "Query",
                    Description = "キャッシュカテゴリリストとノードリストを受信しています",
                    Node = node,
                };
                NodeShowAdd(this, nlvi);

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

                    queryList.AddRange(proxy.GetCategoryKey(query));
                    DebugWrite(this, "QueryCommunication成功:クエリの受信に成功しました");

                    Settings.Default._routeTable.AddRange(proxy.GetRouteTable(HashFunction.HashCreate(query)));
                    DebugWrite(this, "QueryCommunication成功:ノードリストの受信に成功しました");
                }
            }
            catch (EndpointNotFoundException ex)
            {
                Debug.WriteLine("QueryCommunication" + ex.Message);
            }
            catch (TimeoutException ex)
            {
                Debug.WriteLine("QueryCommunication" + ex.Message);
            }
            catch (FaultException ex)
            {
                Debug.WriteLine("QueryCommunication" + ex.Message);
            }
            catch (CommunicationException ex)
            {
                Debug.WriteLine("QueryCommunication" + ex.Message);
            }
            catch (ArgumentNullException ex)
            {
                Debug.WriteLine("QueryCommunication" + ex.Message);
            }
            catch (NullReferenceException ex)
            {
                Debug.WriteLine("QueryCommunication" + ex.Message);
            }
            catch (ArgumentException ex)
            {
                Debug.WriteLine("QueryCommunication" + ex.Message);
            }
            finally
            {
                if (proxy != null)
                {
                    try
                    {
                        ((IClientChannel)proxy).Close();
                    }
                    catch
                    {
                        ((IClientChannel)proxy).Abort();
                    }
                }

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

            return queryList.ToArray();
        }
        private void _uploadDiffusionTimer_Clock(object sender)
        {
            if (Settings.Default.UploadDiffusionList.Count == 0) return;
            if (_uploadDiffusionTimer_Count > 0) return;
            _uploadDiffusionTimer_Count++;

            UploadDiffusion ud = Settings.Default.UploadDiffusionList[0];
            byte[] value = null;

            IFileShareService proxy = null;
            try
            {
                value = Settings.Default._cacheController[ud.Cache, ud.CacheBlockHash];

                int i = 3;

                while (i > 0 && (DateTime.Now - ud.CreateTime).Minutes < 30 && (DateTime.Now - ud.CreateTime).Minutes >= 0)
                {
                    NodeListViewItem nlvi = null;

                    try
                    {
                        Node node = Settings.Default._routeTable.Random();
                        nlvi = new NodeListViewItem()
                        {
                            CommunicationType = "uploadDiffusion",
                            Description = "キャッシュブロックを送信しています",
                            Node = node,
                        };
                        NodeShowAdd(this, nlvi);

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

                            proxy.SetCacheBlock(ud.Cache, value, ud.CreateTime);
                            DebugWrite(this, "_uploadDiffusionTimer_Clock:拡散アップロードに成功しました");

                            i--;
                        }
                    }
                    catch (EndpointNotFoundException ex)
                    {
                        Debug.WriteLine("_uploadDiffusionTimer_Clock" + ex.Message);
                    }
                    catch (TimeoutException ex)
                    {
                        Debug.WriteLine("_uploadDiffusionTimer_Clock" + ex.Message);
                    }
                    catch (FaultException ex)
                    {
                        Debug.WriteLine("_uploadDiffusionTimer_Clock" + ex.Message);
                    }
                    catch (CommunicationException ex)
                    {
                        Debug.WriteLine("_uploadDiffusionTimer_Clock" + ex.Message);
                    }
                    catch (NullReferenceException ex)
                    {
                        Debug.WriteLine("_uploadDiffusionTimer_Clock" + ex.Message);
                    }
                    catch (ArgumentNullException ex)
                    {
                        Debug.WriteLine("_uploadDiffusionTimer_Clock" + ex.Message);
                    }
                    catch (ArgumentException ex)
                    {
                        Debug.WriteLine("_uploadDiffusionTimer_Clock" + ex.Message);
                    }
                    catch (ApplicationException ex)
                    {
                        DebugWrite(this, "_uploadDiffusionTimer_Clock:" + ex.Message);

                        Debug.WriteLine("_uploadDiffusionTimer_Clock" + ex.Message);
                    }
                    finally
                    {
                        if (nlvi != null)
                            NodeShowRemove(this, nlvi);
                    }
                }
            }
            catch (ApplicationException ex)
            {
                DebugWrite(this, "_uploadDiffusionTimer_Clock:" + ex.Message);
            }
            finally
            {
                if (proxy != null)
                {
                    try
                    {
                        ((IClientChannel)proxy).Close();
                    }
                    catch
                    {
                        ((IClientChannel)proxy).Abort();
                    }
                }

                Settings.Default.UploadDiffusionList.RemoveAt(0);
                _uploadDiffusionTimer_Count--;
            }
        }
Beispiel #3
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));
                    }
                }
            }
Beispiel #4
0
        private bool StoreCommunication(Node node, Key[] keys)
        {
            NodeListViewItem nlvi = null;

            IFileShareService proxy = null;
            try
            {
                nlvi = new NodeListViewItem()
                {
                    CommunicationType = "Store",
                    Description = "キー情報を送信しています",
                    Node = node,
                };

                NodeShowAdd(this, nlvi);

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

                    proxy.Store(keys);

                    DebugWrite(this, "StoreCommunication成功:Keyリストの送信に成功しました");
                    return true;
                }
            }
            catch (EndpointNotFoundException ex)
            {
                Debug.WriteLine("StoreCommunication" + ex.Message);
                return false;
            }
            catch (TimeoutException ex)
            {
                Debug.WriteLine("StoreCommunication" + ex.Message);
                return false;
            }
            catch (FaultException ex)
            {
                Debug.WriteLine("StoreCommunication" + ex.Message);
                return false;
            }
            catch (CommunicationException ex)
            {
                if (Settings.Default._routeTable.Count > 30)
                {
                    Settings.Default._routeTable.AddDeadNode(node);
                    Settings.Default._routeTable.Remove(node);
                }

                Debug.WriteLine("StoreCommunication" + ex.Message);
                return false;
            }
            catch (NullReferenceException ex)
            {
                Debug.WriteLine("StoreCommunication" + ex.Message);
                return false;
            }
            catch (ArgumentNullException ex)
            {
                Debug.WriteLine("StoreCommunication" + ex.Message);
                return false;
            }
            catch (ArgumentException ex)
            {
                Debug.WriteLine("StoreCommunication" + ex.Message);
                return false;
            }
            finally
            {
                if (proxy != null)
                {
                    try
                    {
                        ((IClientChannel)proxy).Close();
                    }
                    catch
                    {
                        ((IClientChannel)proxy).Abort();
                    }
                }

                if (nlvi != null)
                    NodeShowRemove(this, nlvi);
            }
        }
Beispiel #5
0
 void NodeShowRemove(object sender, NodeListViewItem e)
 {
     this.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
     {
         if (Settings.Default._nodeShowList.Any(n => n == e))
             Settings.Default._nodeShowList.Remove(e);
     }));
 }
Beispiel #6
0
 void NodeShowAdd(object sender, NodeListViewItem e)
 {
     this.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
     {
         Settings.Default._nodeShowList.Add(e);
     }));
 }
Beispiel #7
0
        private bool UploadCommunication(Node node, Cache cache, byte[] value)
        {
            NodeListViewItem nlvi = null;

            IFileShareService proxy = null;
            try
            {
                nlvi = new NodeListViewItem()
                {
                    CommunicationType = "Upload",
                    Description = "キャッシュブロックを送信しています",
                    Node = node,
                };
                NodeShowAdd(this, nlvi);

                using (ChannelFactory<IFileShareService> channel = new ChannelFactory<IFileShareService>("Tcp_Client", node.Endpoint))
                {
                    proxy = channel.CreateChannel();
                    proxy.SetCacheBlock(cache, value, DateTime.Now);

                    DebugWrite(this, "UploadCommunication成功:キャッシュブロックのアップロードに成功しました");
                    return true;
                }
            }
            catch (EndpointNotFoundException ex)
            {
                Debug.WriteLine("UploadCommunication" + ex.Message);
                return false;
            }
            catch (TimeoutException ex)
            {
                Debug.WriteLine("UploadCommunication" + ex.Message);
                return false;
            }
            catch (FaultException ex)
            {
                Debug.WriteLine("UploadCommunication" + ex.Message);
                return false;
            }
            catch (CommunicationException ex)
            {
                Debug.WriteLine("UploadCommunication" + ex.Message);
                return false;
            }
            catch (NullReferenceException ex)
            {
                Debug.WriteLine("UploadCommunication" + ex.Message);
                return false;
            }
            catch (ArgumentNullException ex)
            {
                Debug.WriteLine("UploadCommunication" + ex.Message);
                return false;
            }
            catch (ApplicationException ex)
            {
                Debug.WriteLine("UploadCommunication" + ex.Message);
                return false;
            }
            catch (ArgumentException ex)
            {
                Debug.WriteLine("UploadCommunication" + ex.Message);
                return false;
            }
            finally
            {
                if (proxy != null)
                {
                    try
                    {
                        ((IClientChannel)proxy).Close();
                    }
                    catch
                    {
                        ((IClientChannel)proxy).Abort();
                    }
                }

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