Class used to represent a keyword inside Kademlia Network. A kademlia keyword has one or more tags associated.
Inheritance: IDocumentType, ILoadable
Exemplo n.º 1
0
        /// <summary>
        /// Stores a tag as resource into the kademlia repository. If the resource already exists the method tries to add the
        /// given Url (with the desired publication time) to the list of suppliers for that resource; if the url is already known
        /// the method does nothing. If the resource is new first the method adds it to the repository and then generates a set of
        /// keywords related to the new resource. For each generated keyword, if this is already in the repository the tag
        /// identifier (that is the resource identifier too) will be added to the related tags list, if the keyword doesn't exist
        /// it will be created and its related tags list will contains only the new resource.
        /// </summary>
        /// <param name="tag">Tag to store in the kademlia resource</param>
        /// <param name="peer">Url of the supplier</param>
        /// <param name="pubtime">Publication TIme</param>
        /// <returns>False if something went wrong, true otherwise</returns>
        public bool StoreResource(CompleteTag tag, Uri peer, DateTime pubtime)
        {
            KademliaResource rs = new KademliaResource();

            Console.WriteLine("Storing resource from peer " + peer);
            DhtElement         dhtElem = new DhtElement(peer, pubtime, this._elementValidity);
            RepositoryResponse resp    = _repository.GetByKey <KademliaResource>(tag.TagHash, rs);

            if (resp == RepositoryResponse.RepositoryLoad)
            {
                if (!rs.Urls.Contains(dhtElem))
                {
                    _repository.ArrayAddElement(rs.Id, "Urls", dhtElem);
                }
                else
                {
                    log.Debug("Urls " + peer.ToString() + " already known");
                }
            }
            else if (resp == RepositoryResponse.RepositoryMissingKey)
            {
                rs = new KademliaResource(tag, dhtElem);
                if (_repository.Save(rs) == RepositoryResponse.RepositorySuccess)
                {
                    List <string>          pks  = new List <string>(generatePrimaryKey(tag));
                    List <KademliaKeyword> keys = new List <KademliaKeyword>();
                    if (_repository.GetByKeySet(pks.ToArray(), keys) > 0)
                    {
                        foreach (KademliaKeyword k in keys)
                        {
                            if (!k.Tags.Contains(rs.Id))
                            {
                                _repository.ArrayAddElement(k.Id, "Tags", rs.Id);
                            }
                            pks.Remove(k.Id);
                        }
                        foreach (String pk in pks)
                        {
                            KademliaKeyword localKey = new KademliaKeyword(pk, rs.Id);
                            _repository.Save(localKey);
                        }
                    }
                    else
                    {
                        log.Error("Unexpected reposnde while getting keywords");
                        return(false);
                    }
                }
                else
                {
                    log.Error("Unexpected response while inserting Tag with key " + tag.TagHash);
                    return(false);
                }
            }
            else
            {
                log.Error("Unexpected response while testing presence of the key " + tag.TagHash);
                return(false);
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Stores a tag as resource into the kademlia repository. If the resource already exists the method tries to add the 
        /// given Url (with the desired publication time) to the list of suppliers for that resource; if the url is already known
        /// the method does nothing. If the resource is new first the method adds it to the repository and then generates a set of
        /// keywords related to the new resource. For each generated keyword, if this is already in the repository the tag
        /// identifier (that is the resource identifier too) will be added to the related tags list, if the keyword doesn't exist
        /// it will be created and its related tags list will contains only the new resource.
        /// </summary>
        /// <param name="tag">Tag to store in the kademlia resource</param>
        /// <param name="peer">Url of the supplier</param>
        /// <param name="pubtime">Publication TIme</param>
        /// <returns>False if something went wrong, true otherwise</returns>
        public bool StoreResource(CompleteTag tag, Uri peer,DateTime pubtime)
        {
            KademliaResource rs = new KademliaResource();
            Console.WriteLine("Storing resource from peer " + peer);
            DhtElement dhtElem = new DhtElement(peer, pubtime, this._elementValidity);
            RepositoryResponse resp = _repository.GetByKey<KademliaResource>(tag.TagHash, rs);
            if ( resp == RepositoryResponse.RepositoryLoad)
            {
                if (!rs.Urls.Contains(dhtElem)) {
                    _repository.ArrayAddElement(rs.Id, "Urls", dhtElem);
                } else {
                    log.Debug("Urls "+peer.ToString()+" already known");
                }
            }
            else if (resp == RepositoryResponse.RepositoryMissingKey)
            {
                rs = new KademliaResource(tag, dhtElem);
                if (_repository.Save(rs) == RepositoryResponse.RepositorySuccess)
                {
                    List<string> pks = new List<string>(generatePrimaryKey(tag));
                    List<KademliaKeyword> keys = new List<KademliaKeyword>();
                    if (_repository.GetByKeySet(pks.ToArray(), keys) > 0)
                    {
                        foreach (KademliaKeyword k in keys)
                        {
                            if (!k.Tags.Contains(rs.Id))
                            {
                                _repository.ArrayAddElement(k.Id, "Tags", rs.Id);
                            }
                            pks.Remove(k.Id);
                        }
                        foreach (String pk in pks)
                        {
                            KademliaKeyword localKey = new KademliaKeyword(pk,rs.Id);
                            _repository.Save(localKey);
                        }
                    }
                    else
                    {
                        log.Error("Unexpected reposnde while getting keywords");
                        return false;
                    }

                }
                else
                {
                    log.Error("Unexpected response while inserting Tag with key " + tag.TagHash);
                    return false;
                }
            }
            else
            {
                log.Error("Unexpected response while testing presence of the key " + tag.TagHash);
                return false;
            }
            return true;
        }