예제 #1
0
 /// <summary>
 ///   Called every time that the Kademlia node notice a new node in the network. Can be
 ///   either because the new node make a RPC request or was found in a node lookUp procedure.
 /// </summary>
 /// <param name = "nodeIdentifier">New node identifier.</param>
 public virtual void RegisterNewNode(NodeIdentifier <TKey> nodeIdentifier)
 {
     if (!RoutingTable.Contains(nodeIdentifier))
     {
         RoutingTable.Add(nodeIdentifier);
     }
 }
예제 #2
0
        /// <summary>
        ///   Store the pair (key, value)
        /// </summary>
        /// <param name = "key"></param>
        /// <param name = "value"></param>
        /// <param name = "callerIdentifier"></param>
        public virtual void Store(TKey key, TValue value, NodeIdentifier <TKey> callerIdentifier = null)
        {
            Debug.Assert(KadCore.NodeIdentifier != null, "kadCore.NodeIdentifier != null");

            // notify about the existence of the caller node
            if (callerIdentifier != null)
            {
                OnNewNodeNotice(callerIdentifier);
            }

            if (DataStore.Contains(key))
            {
                DataStore[key] = value;
            }
            else
            {
                DataStore.Add(key, value);
            }

            KadLogger.Info(string.Format("Store method called with params ({0},{1}) from {2} ", key, value, callerIdentifier));
            if (callerIdentifier == null)
            {
                KadLogger.Warn("Caller identifier is null.");
            }
        }
예제 #3
0
 public KadCore(RoutingTable <TKey> routingTable, KademeliaSettings settings,
                Func <NodeIdentifier <TKey>, IKadNode <TKey, TValue> > createClientFromNodeIdDelegate,
                NodeIdentifier <TKey> nodeIdentifier = null)
 {
     _createClientFromNodeIdDelegate = createClientFromNodeIdDelegate;
     RoutingTable   = routingTable;
     Settings       = settings;
     NodeIdentifier = nodeIdentifier;
 }
예제 #4
0
        /// <summary>
        /// Returns true if the given node identifier is online
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private bool IsOnline(NodeIdentifier <TKey> node)
        {
//            Regex b = new Regex(@"http://\w+(\.\w+)*:\d+");
//            string baseAddress = b.Match(node.ServiceUrl).Value;
//            var mexAddress =new Uri (baseAddress + "/?WSDL");
//            return MetadataResolver.Resolve(GetContractType, mexAddress, MetadataExchangeClientMode.HttpGet).Count>0;

            // todo checking if a service is online is really expensive, find a better way to do this
            return(true);
        }
예제 #5
0
        public virtual bool RemoveInto(TKey key, TValue value, NodeIdentifier <TKey> callerIdentifier = null)
        {
            // notify about the existence of the caller node
            if (callerIdentifier != null)
            {
                OnNewNodeNotice(callerIdentifier);
            }

            return(!DataStore.Contains(key) || ((IList <TValue>)DataStore[key]).Remove(value));
        }
예제 #6
0
 public bool Equals(NodeIdentifier <TKey> other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(Equals(other.NodeId, NodeId));
 }
예제 #7
0
        public FindValueResult <TKey, IEnumerable <TValue> > FindPagedValue(TKey key, int pageIndex, int pageCount,
                                                                            NodeIdentifier <TKey> callerIdentifier = null)
        {
            // notify about the existence of the caller node
            if (callerIdentifier != null)
            {
                OnNewNodeNotice(callerIdentifier);
            }

            return(DataStore.Contains(key)
                       ? new FindValueResult <TKey, IEnumerable <TValue> >(
                       DataStore[key].Skip((pageIndex - 1) * pageCount).Take(pageCount))
                       : new FindValueResult <TKey, IEnumerable <TValue> >(GetKClosestContacts(key)));
        }
예제 #8
0
        public virtual bool Remove(TKey key, NodeIdentifier <TKey> callerIdentifier = null)
        {
            Debug.Assert(KadCore.NodeIdentifier != null, "kadCore.NodeIdentifier != null");

            // notify about the existence of the caller node
            if (callerIdentifier != null)
            {
                OnNewNodeNotice(callerIdentifier);
            }

            KadLogger.Info(string.Format("Remove method called with key {0} from {1} ", key, callerIdentifier));
            if (callerIdentifier == null)
            {
                KadLogger.Warn("Caller identifier is null.");
            }

            return(DataStore.Remove(key));
        }
예제 #9
0
        /// <summary>
        ///   return the K closest nodes to the key that this node knows about.
        /// </summary>
        /// <param name = "key"></param>
        /// <param name = "callerIdentifier"></param>
        /// <returns></returns>
        public virtual FindNodeResult <TKey> FindNode(TKey key, NodeIdentifier <TKey> callerIdentifier = null)
        {
            Debug.Assert(KadCore.NodeIdentifier != null, "kadCore.NodeIdentifier != null");

            // notify about the existence of the caller node
            if (callerIdentifier != null)
            {
                OnNewNodeNotice(callerIdentifier);
            }

            KadLogger.Info(string.Format("FindNode method called with key {0} from {1} ", key, callerIdentifier));
            if (callerIdentifier == null)
            {
                KadLogger.Warn("Caller identifier is null.");
            }

            // returns the K nearest elements that this node knows about around the key);
            return(new FindNodeResult <TKey>(GetKClosestContacts(key)));
        }
예제 #10
0
        public override void Store(TKey key, IEnumerable <TValue> value, NodeIdentifier <TKey> callerIdentifier = null)
        {
            // notify about the existence of the caller node
            if (callerIdentifier != null)
            {
                OnNewNodeNotice(callerIdentifier);
            }

            //** the store methods in this class need to perform a FindValue(key) call first to
            // synchronize the lists, as we may be in the case where a node exits the network
            // and this node only knows about the passed value when it may be others in the network.

            if (DataStore.Contains(key))
            {
                ((IList <TValue>)DataStore[key]).AddRange(value);
            }
            else
            {
                var elements = new List <TValue>();
                try
                {
                    // if the current node doesn't contain the key, then other elements may exist in other nodes
                    // 1. Find other values that may exist in other nodes
                    //                    IEnumerable<TValue> otherNodesValues = KadCore.ValueLookUp(key);

                    // 2. combine the new elements with the existing ones
                    //                    elements.AddRange(otherNodesValues);
                }
                catch (KeyNotFoundException e)
                {
                }
                finally
                {
                    // 2. combine the new elements with the existing ones,
                    // remember that this node searched for the specified id on the network, so the current values
                    // may exist on the list already.
                    elements.AddRange(value);

                    // 3. add the element to the DataStore
                    DataStore.Add(key, elements);
                }
            }
        }
예제 #11
0
        /// <summary>
        ///   If the node has received a Store with that key, then return the value,
        ///   if not, return the K closest nodes to the key that this node knows about.
        /// </summary>
        /// <param name = "key"></param>
        /// <param name = "callerIdentifier"></param>
        /// <returns></returns>
        public virtual FindValueResult <TKey, TValue> FindValue(TKey key, NodeIdentifier <TKey> callerIdentifier = null)
        {
            Debug.Assert(KadCore.NodeIdentifier != null, "kadCore.NodeIdentifier != null");

            // notify about the existence of the caller node
            if (callerIdentifier != null)
            {
                OnNewNodeNotice(callerIdentifier);
            }

            KadLogger.Info(string.Format("FindValue method called with key {0} from {1} ", key, callerIdentifier));
            if (callerIdentifier == null)
            {
                KadLogger.Warn("Caller identifier is null.");
            }

            return(DataStore.Contains(key)
                       ? new FindValueResult <TKey, TValue>(DataStore[key])
                       : new FindValueResult <TKey, TValue>(GetKClosestContacts(key)));
        }
예제 #12
0
        public virtual HeartBeat <TKey> Ping(NodeIdentifier <TKey> callerIdentifier = null)
        {
            Debug.Assert(KadCore.NodeIdentifier != null, "kadCore.NodeIdentifier != null");

            // notify about the existence of the caller node
            if (callerIdentifier != null)
            {
                OnNewNodeNotice(callerIdentifier);
            }

            KadLogger.Info("Ping method called from " + callerIdentifier);
            if (callerIdentifier == null)
            {
                KadLogger.Warn("Caller identifier is null.");
            }
            KadLogger.Info("Returning NodeIdentifier: " + KadCore.NodeIdentifier);

            // This method could return valuable information about current/max performance, payload, etc.
            // also some data about the host: machine name, user, total files shared, etc.
            return(new HeartBeat <TKey>(KadCore.NodeIdentifier));
        }
예제 #13
0
 /// <summary>
 ///   this function can be used as a comparer with the "closest to key" criteria
 /// </summary>
 /// <param name = "node1"></param>
 /// <param name = "node2"></param>
 /// <param name = "key"></param>
 /// <param name = "metric">metric used to calculate the distance</param>
 /// <returns></returns>
 public static int DistanceComparer(NodeIdentifier <TKey> node1, NodeIdentifier <TKey> node2, TKey key,
                                    Metric <TKey> metric)
 {
     return(DistanceComparer(node1.NodeId, node2.NodeId, key, metric));
 }
예제 #14
0
 protected abstract ISetKadNode <TKey, TValue> CreateClientFromNodeId(NodeIdentifier <TKey> nodeIdentifier);
예제 #15
0
 public void AddToRoutingTable(NodeIdentifier <TKey> nodeId)
 {
     KadCore.RoutingTable.Add(nodeId);
 }
예제 #16
0
 public HeartBeat(NodeIdentifier <TKey> nodeIdentifier)
 {
     NodeIdentifier = nodeIdentifier;
 }
예제 #17
0
 protected IKadNode <TKey, TValue> CreateClientFromNodeId(NodeIdentifier <TKey> node)
 {
     return(_createClientFromNodeIdDelegate(node));
 }
예제 #18
0
 /// <summary>
 ///   Called every time that the Kademlia node notice a new node in the network. Can be
 ///   either because the new node make a RPC request or was found in a node lookUp procedure.
 /// </summary>
 /// <param name = "nodeIdentifier">New node identifier.</param>
 protected virtual void OnNewNodeNotice(NodeIdentifier <TKey> nodeIdentifier)
 {
     KadCore.RegisterNewNode(nodeIdentifier);
     KadLogger.Info("New node noticed, adding it to the routing table. Node: " + nodeIdentifier);
 }
예제 #19
0
 public bool Remove(NodeIdentifier <TKey> item)
 {
     return(_nodeIdentifiers.Remove(item));
 }
예제 #20
0
 public int DistanceComparer(NodeIdentifier <TKey> node1, NodeIdentifier <TKey> node2, TKey key)
 {
     return(DistanceComparer(node1.NodeId, node2.NodeId, key));
 }
예제 #21
0
 public void Add(NodeIdentifier <TKey> item)
 {
     Debug.Assert(item != null, "item != null");
     _nodeIdentifiers.Add(item);
 }
예제 #22
0
 public bool Contains(NodeIdentifier <TKey> item)
 {
     return(_nodeIdentifiers.Contains(item));
 }
예제 #23
0
 public virtual void StoreInto(TKey key, TValue value, NodeIdentifier <TKey> callerIdentifier = null)
 {
     Store(key, new[] { value }, callerIdentifier);
 }
예제 #24
0
 private ISetKadNode <TKey, TValue> CreateSetClientFromNodeId(NodeIdentifier <TKey> node)
 {
     return((ISetKadNode <TKey, TValue>)CreateClientFromNodeId(node));
 }
예제 #25
0
 public SetKadCore(RoutingTable <TKey> routingTable, KademeliaSettings settings,
                   Func <NodeIdentifier <TKey>, ISetKadNode <TKey, TValue> > createClientFromNodeIdDelegate,
                   NodeIdentifier <TKey> nodeIdentifier)
     : base(routingTable, settings, createClientFromNodeIdDelegate, nodeIdentifier)
 {
 }