Example #1
0
        /// <summary>
        /// Retrieve the string value for a given ulong
        /// key.
        /// </summary>
        /// <param name="key">The key whose value should be returned.</param>
        /// <returns>The string value for the given key, or an empty string if not found.</returns>
        public string FindKey(ulong key)
        {
            // determine the owning node for the key
            ChordNode owningNode = ChordServer.CallFindSuccessor(key);

            if (owningNode != ChordServer.LocalNode)
            {
                // if this is not the owning node, call
                // FindKey on the remote owning node
                return(ChordServer.CallFindKey(owningNode, key));
            }
            else
            {
                // if this is the owning node, check
                // to see if the key exists in the data store
                if (this.m_DataStore.ContainsKey(key))
                {
                    // if the key exists, return the value
                    return(this.m_DataStore[key]);
                }
                else
                {
                    // if the key does not exist, return empty string
                    return(string.Empty);
                }
            }
        }
 /// <summary>
 /// Find the node that is the rightful owner of a given id.
 /// </summary>
 /// <param name="id">The id whose successor should be found.</param>
 /// <param name="hopCount">The number of network hops taken in finding the successor.</param>
 /// <returns>The ChordNode that is the Successor of a given ID value.</returns>
 public ChordNode FindSuccessor(UInt64 id, int hopCountIn, out int hopCountOut)
 {
     // is the local node's successor the rightful owner?
     if (ChordServer.IsIDInRange(id, this.ID, this.Successor.ID))
     {
         hopCountOut = hopCountIn;
         return(this.Successor);
     }
     else
     {
         // otherwise, find the nearest preceding finger, and ask that node.
         ChordNode predNode = FindClosestPrecedingFinger(id);
         return(ChordServer.CallFindSuccessor(predNode, id, 0, ++hopCountIn, out hopCountOut));
     }
 }
Example #3
0
        /// <summary>
        /// Add a key to the store.  Gets a hash of the key, determines
        /// the correct owning node, and stores the string value
        /// on that node.
        /// </summary>
        /// <param name="value">The value to add.</param>
        public void AddKey(string value)
        {
            // the key is the hash of the value to
            // add to the store, and determines the
            // owning NChord node
            ulong key = ChordServer.GetHash(value);

            // using the local node, determine the correct owning
            // node for the data to be stored given the key value
            ChordNode owningNode = ChordServer.CallFindSuccessor(key);

            if (owningNode != ChordServer.LocalNode)
            {
                // if this is not the owning node, then call AddKey
                // on the actual owning node
                ChordServer.CallAddKey(owningNode, value);
            }
            else
            {
                // if this is the owning node, then add the
                // key to the local data store
                this.m_DataStore.Add(key, value);
            }
        }