/// <summary> /// Checks if the repository contains a given tag /// </summary> /// <param name="tagid">Resource Identifier to search</param> /// <returns>True if the repository contains the resource, false if not.</returns> public bool ContainsTag(string tagid) { KademliaResource rs = Get(tagid); if (rs != null) { return(true); } return(false); }
/// <summary> /// Returns the Resource identified by the given taghash. /// </summary> /// <param name="tagid">Identifier of the resource to get</param> /// <returns>The requested resource if it exists, null if not.</returns> public KademliaResource Get(string tagid) { KademliaResource rs = new KademliaResource(); if (_repository.GetByKey <KademliaResource>(tagid, rs) == RepositoryResponse.RepositoryLoad) { return(rs); } else { return(null); } }
/// <summary> /// Method that merges the Dht Elements of the current resource with the ones of an another resource. /// </summary> /// <param name="other">The other resource</param> /// <returns></returns> public bool MergeTo(KademliaResource other) { if (this.Tag.FileHash == other.Tag.FileHash) { Console.WriteLine("Merged!"); foreach (DhtElement e in other.Urls) { this.Urls.Add(e); } return(true); } else { return(false); } }
/// <summary> /// Returns the publication time of a Dht Element related to a Resource. /// </summary> /// <param name="tagid">Resource Identifier</param> /// <param name="url">Url of the Dht Element</param> /// <returns>The publication time of the Dht Element if it exists, the DateTime min value if the element doesn't exist</returns> public DateTime GetPublicationTime(string tagid, Uri url) { KademliaResource rs = Get(tagid); if (rs != null) { DhtElement elem = rs.Urls.ToList().Find(de => { return(de.Url.Equals(url)); } ); if (elem != null) { return(elem.Publication); } } return(DateTime.MinValue); }
/// <summary> /// Sets the publication time of a Dht Element in order to avoid expiration /// </summary> /// <param name="tagid">Resource Identifier</param> /// <param name="url">URL of the Dht Element to refresh</param> /// <param name="pubtime">Publication time to set</param> /// <returns>True if the Dht Element exists and has been refreshed, false otherwise</returns> public bool RefreshResource(string tagid, Uri url, DateTime pubtime) { KademliaResource rs = new KademliaResource(); RepositoryResponse resp = _repository.GetByKey <KademliaResource>(tagid, rs); if (resp == RepositoryResponse.RepositoryLoad) { int eindex = rs.Urls.ToList().FindIndex(elem => { return(elem.Url.Equals(url)); }); if (eindex != -1) { _repository.ArraySetElement(tagid, "Urls", eindex, "Publication", pubtime); return(true); } } return(false); }
/// <summary> /// Method that implements the registration of a peer as supplier for a given resource. /// This method just load the resource and adds the Dht Element if it's not already in the suppliers list. /// </summary> /// <param name="tagid">Resource Identifier</param> /// <param name="url">Url of the supplier</param> /// <param name="pubtime">Publication Time for the supplier</param> /// <returns></returns> public bool Put(string tagid, Uri url, DateTime pubtime) { KademliaResource rs = new KademliaResource(); DhtElement dhtElem = new DhtElement(url, pubtime, this._elementValidity); RepositoryResponse resp = _repository.GetByKey <KademliaResource>(tagid, rs); if (resp == RepositoryResponse.RepositoryLoad) { if (!rs.Urls.Contains(dhtElem)) { _repository.ArrayAddElement(rs.Id, "Urls", dhtElem); return(true); } else { log.Debug("Urls " + url.ToString() + " already known"); } } return(false); }
/// <summary> /// Checks if the given resource contains the Url in its supplier list /// </summary> /// <param name="tagid">Resource identifier</param> /// <param name="url">Url of the supplier</param> /// <returns>True if the Peer with the given Url is a supplier for the resource.</returns> public bool ContainsUrl(string tagid, Uri url) { KademliaResource rs = Get(tagid); DhtElement fakeElem = new DhtElement() { Url = url }; if (rs != null) { if (rs.Urls.Contains(fakeElem)) { return(true); } else { return(false); } } return(false); }
/// <summary> /// Sets all configuration to enable the playback and the downloading of the track from the network. /// After setting all configuration, the player will wait for the buffer to be full enough to start playing. /// </summary> /// <param name="rsc">Resource to Handle</param> /// <param name="spos">Starting position</param> private void setupLocalStream(KademliaResource rsc,long spos) { if (localstream != null) { localstream.Close(); } EnableFlowRestart = true; Dictionary<string, float> tD = new Dictionary<string, float>(); foreach (DhtElement de in rsc.Urls) { Console.WriteLine(de.Url); tD[de.Url.ToString()] = 0; } this.streambuff = new byte[rsc.Tag.FileSize]; localstream = new ObservableStream(streambuff); startPosition = spos; localstream.Seek(spos, SeekOrigin.Begin); pos = spos; NotifyPropertyChanged("Position"); NotifyPropertyChanged("BufferPortion"); localstream.WaitedPositionReached += resumePlay; localstream.PositionChanged += (sender, args) => { NotifyPropertyChanged("BufferPortion"); }; peer.GetFlow(rsc.Tag.FileHash,(int)startPosition, (int)rsc.Tag.FileSize, tD, localstream); BufferingState = true; NotifyPropertyChanged("PlayingState"); startPhaseBuffering = true; localstream.WaitForMore(60000); NotifyPropertyChanged("ResourceTag"); NotifyPropertyChanged("Length"); NotifyPropertyChanged("BigBufferSize"); NotifyPropertyChanged("HasResource"); }
/// <summary> /// Sets the current handling resource. If the player is handling the same resource then the methods call <see cref="restartStream"/> /// otherwise it calls <see cref="setupLocalStream"/> /// </summary> /// <param name="rsc">Resource that has to be handled</param> private void setResource(KademliaResource rsc) { this.stop(); if (rsc == null) return; if ((this.rsc != null) && (rsc.Id.Equals(this.rsc.Id))) { restartStream(); } else { this.rsc = rsc; setupLocalStream(this.rsc, 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); }
/// <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; }
/// <summary> /// Sets the publication time of a Dht Element in order to avoid expiration /// </summary> /// <param name="tagid">Resource Identifier</param> /// <param name="url">URL of the Dht Element to refresh</param> /// <param name="pubtime">Publication time to set</param> /// <returns>True if the Dht Element exists and has been refreshed, false otherwise</returns> public bool RefreshResource(string tagid, Uri url,DateTime pubtime) { KademliaResource rs = new KademliaResource(); RepositoryResponse resp = _repository.GetByKey<KademliaResource>(tagid, rs); if (resp == RepositoryResponse.RepositoryLoad) { int eindex = rs.Urls.ToList().FindIndex(elem => { return elem.Url.Equals(url); }); if (eindex != -1) { _repository.ArraySetElement(tagid, "Urls", eindex, "Publication", pubtime); return true; } } return false; }
/// <summary> /// Method that implements the registration of a peer as supplier for a given resource. /// This method just load the resource and adds the Dht Element if it's not already in the suppliers list. /// </summary> /// <param name="tagid">Resource Identifier</param> /// <param name="url">Url of the supplier</param> /// <param name="pubtime">Publication Time for the supplier</param> /// <returns></returns> public bool Put(string tagid, Uri url, DateTime pubtime) { KademliaResource rs = new KademliaResource(); DhtElement dhtElem = new DhtElement(url, pubtime, this._elementValidity); RepositoryResponse resp = _repository.GetByKey<KademliaResource>(tagid, rs); if (resp == RepositoryResponse.RepositoryLoad) { if (!rs.Urls.Contains(dhtElem)) { _repository.ArrayAddElement(rs.Id, "Urls", dhtElem); return true; } else { log.Debug("Urls " + url.ToString() + " already known"); } } return false; }
/// <summary> /// Returns the Resource identified by the given taghash. /// </summary> /// <param name="tagid">Identifier of the resource to get</param> /// <returns>The requested resource if it exists, null if not.</returns> public KademliaResource Get(string tagid) { KademliaResource rs = new KademliaResource(); if (_repository.GetByKey<KademliaResource>(tagid, rs) == RepositoryResponse.RepositoryLoad) { return rs; } else { return null; } }
/// <summary> /// Method that merges the Dht Elements of the current resource with the ones of an another resource. /// </summary> /// <param name="other">The other resource</param> /// <returns></returns> public bool MergeTo(KademliaResource other) { if (this.Tag.FileHash == other.Tag.FileHash) { Console.WriteLine("Merged!"); foreach (DhtElement e in other.Urls) { this.Urls.Add(e); } return true; } else { return false; } }
/// <summary> /// Default constructor /// </summary> /// <param name="rs"></param> public StreamRequestedArgs(KademliaResource rs = null) { res = rs; }