protected RouteTable(SerializationInfo info, StreamingContext context) { _nodeList = (Node[][])info.GetValue("_nodeList", typeof(Node[][])); _myNode = (Node)info.GetValue("MyNode", typeof(Node)); TimerEvent(); }
/// <summary> /// Cacheリストを追加します /// </summary> public void AddRange(IEnumerable<Cache> collection, Node node) { foreach (Cache item in collection) { this.Add(item, node); } }
/// <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); }
private bool DeadNodeCheck(Node node) { string nodeIdString = BinaryEditor.BytesToHexString(node.NodeID); if (_deadNodeDic.ContainsKey(nodeIdString)) { var t = DateTime.Now - _deadNodeDic[nodeIdString]; if (t.Hours < 1) { return true; } } return false; }
/// <summary> /// RouteTableクラスの新しいインスタンスを初期化します /// </summary> /// <param name="myNode">自分自身のノード情報を指定する</param> public RouteTable(Node myNode) { this.MyNode = myNode; TimerEvent(); }
/// <summary> /// ノードを削除する /// </summary> /// <param name="node"></param> public void Remove(Node node) { lock (this) { int i = Xor(this.MyNode.NodeID, node.NodeID); if (_nodeList[i] != null) { int index = 0; while (_nodeList[i].Count(n => n != null) > index && BinaryEditor.ArrayEquals(_nodeList[i][index].NodeID, node.NodeID)) index++; if (_nodeList[i].Count(n => n != null) > index) { _nodeList[i][index] = null; _nodeList[i] = _nodeList[i].OrderBy(n => n == null).ToArray(); } } } }
/// <summary> /// ノードを検索する /// </summary> /// <param name="key">検索するノードID</param> /// <returns>距離の近いノードを返す</returns> public Node[] Search(byte[] key) { Node[][] tempNodeList = new Node[256 + 1][]; List<Node> tempOneList = new List<Node>(); lock (this) { for (int i = 0; i < _nodeList.Length; i++) { for (int j = 0; _nodeList[i] != null && j < _nodeList[i].Length && _nodeList[i][j] != null; j++) { int index = Xor(key, _nodeList[i][j].NodeID); if (tempNodeList[index] == null) { tempNodeList[index] = new Node[MAX_NODELIST_LENGTH]; } if (tempNodeList[index].Count(n => n != null) < MAX_NODELIST_LENGTH) { tempNodeList[index][tempNodeList[index].Count(n => n != null)] = _nodeList[i][j]; } } } } for (int i = 0; i < tempNodeList.Length; i++) { for (int j = 0; tempNodeList[i] != null && j < tempNodeList[i].Length && tempNodeList[i][j] != null; j++) { if (tempOneList.Count <= 100) tempOneList.Add(tempNodeList[i][j]); } } return tempOneList.ToArray(); }
/// <summary> /// ノードリストを追加する /// </summary> /// <param name="Nodes">追加するノードリスト</param> public void AddRange(Node[] Nodes) { foreach (Node node in Nodes) { this.Add(node); } }
/// <summary> /// 死亡ノードを追加する /// </summary> /// <param name="node"></param> public void AddDeadNode(Node node) { _deadNodeDic[BinaryEditor.BytesToHexString(node.NodeID)] = DateTime.Now; }
/// <summary> /// ノードを追加する /// </summary> /// <param name="node">追加するノード</param> public void Add(Node node) { if (node == null) return; if (!Verification.VerificationIPAddress(node.Endpoint)) return; if (DeadNodeCheck(node)) return; if (_pingNodeList.Any(n => BinaryEditor.ArrayEquals(n.NodeID, node.NodeID))) return; int i = Xor(MyNode.NodeID, node.NodeID); if (i == 0) return; lock (this) { // 追加するnodeがNodeListに入っている場合、そのノードをNodeListの末尾に移す if (_nodeList[i] != null && true == _nodeList[i].Any(n => n != null && BinaryEditor.ArrayEquals(n.NodeID, node.NodeID))) { _nodeList[i] = _nodeList[i].OrderBy(n => n == null || BinaryEditor.ArrayEquals(n.NodeID, node.NodeID)).ToArray(); } // 追加するnodeがNodeListの中に存在しない場合 else if (_nodeList[i] != null) { if (_nodeList[i].Count(n => n == null) == 0) { Node checkNode = _nodeList[i].FirstOrDefault(n => n != null && !_pingNodeList.Any(m => BinaryEditor.ArrayEquals(n.NodeID, m.NodeID))); if (checkNode != null) _pingNodeList.Add(checkNode); } if (_nodeList[i].Count(n => n != null) < MAX_NODELIST_LENGTH) { _nodeList[i][_nodeList[i].Count(n => n != null)] = node; _nodeList[i] = _nodeList[i].OrderBy(n => n == null || BinaryEditor.ArrayEquals(n.NodeID, node.NodeID)).ToArray(); } } else { _nodeList[i] = new Node[MAX_NODELIST_LENGTH]; _nodeList[i][0] = node; } } }
bool _routeTable_Ping(object sender, Node e) { try { using (ChannelFactory<IFileShareService> channel = new ChannelFactory<IFileShareService>("Tcp_Client", e.Endpoint)) { IFileShareService proxy = channel.CreateChannel(); Settings.Default._routeTable.AddRange(proxy.GetRouteTable(new byte[] { 0 })); LogWrite("_routeTable_Ping成功:ノードの生存を確認しました"); return true; } } catch (EndpointNotFoundException ex) { LogWrite("_routeTable_Ping成功:ノードの死亡を確認しました"); Debug.WriteLine("_routeTable_Ping" + ex.Message); return false; } catch (TimeoutException ex) { LogWrite("_routeTable_Ping成功:ノードの死亡を確認しました"); Debug.WriteLine("_routeTable_Ping" + ex.Message); return false; } catch (FaultException ex) { LogWrite("_routeTable_Ping成功:ノードの死亡を確認しました"); Debug.WriteLine("_routeTable_Ping" + ex.Message); return false; } catch (CommunicationException ex) { LogWrite("_routeTable_Ping成功:ノードの死亡を確認しました"); Debug.WriteLine("_routeTable_Ping" + ex.Message); return false; } catch (NullReferenceException ex) { LogWrite("_routeTable_Ping成功:ノードの死亡を確認しました"); Debug.WriteLine("_routeTable_Ping" + ex.Message); return false; } }
/// <summary> /// �T�[�r�X��J�n���� /// </summary> private void StartService() { LogWrite("�T�[�r�X�J�n"); Node node = new Node(); switch (Settings.Default.ConnectionType) { case ConnectionType.Direct: if (Settings.Default.DirectConnectionInformation.IPAddress.Contains(":")) { node.Endpoint = new EndpointAddress(string.Format("net.tcp://[{0}]:{1}/FileShareService", Settings.Default.DirectConnectionInformation.IPAddress, Settings.Default.DirectConnectionInformation.Port)); } else { node.Endpoint = new EndpointAddress(string.Format("net.tcp://{0}:{1}/FileShareService", Settings.Default.DirectConnectionInformation.IPAddress, Settings.Default.DirectConnectionInformation.Port)); } Settings.Default._routeTable.MyNode = node; break; case ConnectionType.UPnP: node.Endpoint = new EndpointAddress(string.Format("net.tcp://{0}:{1}/FileShareService", Settings.Default.UpnpConnectionInformation.GlobalIPAddress, Settings.Default.UpnpConnectionInformation.ExternalPort)); if (!UPnPClient.OpenFirewallPort(Settings.Default.UpnpConnectionInformation.MachineIPAddress, Settings.Default.UpnpConnectionInformation.GatewayIPAddress, Settings.Default.UpnpConnectionInformation.ExternalPort, Settings.Default.UpnpConnectionInformation.InternalPort)) { MessageBox.Show("�|�[�g���J���ł��܂���ł����B"); this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { MessageBoxStatusBarItem.Content = "UPnP�ɂ��|�[�g�J�����s"; })); return; } Settings.Default._routeTable.MyNode = node; break; case ConnectionType.Other: node.Endpoint = new EndpointAddress(string.Format("net.tcp://{0}:{1}/FileShareService", Settings.Default.OtherConnectionInformation.IPAddress, Settings.Default.OtherConnectionInformation.Port)); Settings.Default._routeTable.MyNode = node; break; } LogWrite(string.Format("MyNode Uri: \"{0}\" ID: \"{1}\"", node.Endpoint.ToString(), BinaryEditor.BytesToHexString(node.NodeID))); _serviceHostThread = new Thread(new ThreadStart(delegate() { try { Node serviceNode = new Node(); switch (Settings.Default.ConnectionType) { case ConnectionType.Direct: serviceNode = Settings.Default._routeTable.MyNode; break; case ConnectionType.UPnP: serviceNode.Endpoint = new EndpointAddress(string.Format("net.tcp://{0}:{1}/FileShareService", Settings.Default.UpnpConnectionInformation.MachineIPAddress, Settings.Default.UpnpConnectionInformation.InternalPort)); break; case ConnectionType.Other: serviceNode.Endpoint = new EndpointAddress(string.Format("net.tcp://0.0.0.0:{0}/FileShareService", Settings.Default.OtherConnectionInformation.Port)); break; } Settings.Default._serviceHost = new ServiceHost(typeof(FileShareService), serviceNode.Endpoint.Uri); Settings.Default._serviceHost.Open(); LogWrite(string.Format("ServiceHost Uri: \"{0}\" ID: \"{1}\"", serviceNode.Endpoint.ToString(), BinaryEditor.BytesToHexString(serviceNode.NodeID))); StartEvent(); } catch (Exception e) { MessageBox.Show(e.Message, "Profes, Profes.P2P.FileShare.dll", MessageBoxButton.OK, MessageBoxImage.Error); try { Settings.Default._serviceHost.Abort(); } catch{ } this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { MessageBoxStatusBarItem.Content = "�g�p�ł��Ȃ�IP�A�h���X"; })); } })); _serviceHostThread.Start(); Settings.Default.IsServiceStarted = true; this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { MessageBoxStatusBarItem.Content = "�T�[�r�X�J�n"; })); }