Classe rappresentante il Modello per una traccia.
Inheritance: ILoadable
 /// <summary>
 /// This method shows the use and the functionality of some repository methods.
 /// <c>
 /// Insert a new Track in the Database, Count all elements and the Load it all! Then delete a item and Load it all again
 /// </c>
 /// </summary>
 public static void InsertCountLoadAllDeleteAndLoadAgain()
 {
     ExampleHelper.ExampleMethodPrint("Insert a new Track in the Database, Count all elements and the Load it all!\n"+
                                         "Then delete a item and Load it all again", MethodInfo.GetCurrentMethod());
     TrackModel track = new TrackModel(@"..\..\Resource\Garden.mp3");
     ExampleHelper.DumpObjectProperties(track.GetAsDatabaseType());
     Console.WriteLine("Save Response : " + _trackRep.Save(track));
     Console.WriteLine("Count : " + _trackRep.Count<TrackModel.Track>());
     List<TrackModel.Track> list = new List<TrackModel.Track>();
     Console.WriteLine("GetAll Response : " + _trackRep.GetAll(list));
     foreach (TrackModel.Track t in list)
     {
         ExampleHelper.DumpObjectProperties(t);
     }
     TrackModel anotherTrack = new TrackModel();
     Console.WriteLine("Delete Response: " + _trackRep.Delete<TrackModel.Track>(list.First().Id));
     list.Clear();
     Console.WriteLine("GetAll Response : " + _trackRep.GetAll(list));
     foreach (TrackModel.Track t in list)
     {
         ExampleHelper.DumpObjectProperties(t);
     }
 }
Example #2
0
 /// <summary>
 /// Method used to request the download of a chunk from the network. When it is invoked from
 /// a remote peer it gets the chunk from the file and calls back the ReturnChunk method
 /// of the requestor.
 /// </summary>
 /// <param name="chkrq">Message used to pass information about the Chunk requested</param>
 public void GetChunk(ChunkRequest chkrq)
 {
     log.Info("Received request to send chunk!");
     servingBuffer++;
     TrackModel track = new TrackModel();
     RepositoryResponse resp = trackRepository.GetByKey<TrackModel.Track>(chkrq.RID, track);
     log.Debug("Searching track " + track + " in repository");
     if (resp >= 0)
     {
         log.Debug("Track found! Extracting chunk.");
         byte[] data;
         using (FileStream fs = new FileStream(track.Filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
         {
             int limit = (System.Convert.ToInt32(fs.Length) > (chunkLength * 1024 * (chkrq.CID + 1))) ? chunkLength * 1024 * (chkrq.CID + 1) : (System.Convert.ToInt32(fs.Length));
             int begin = chkrq.CID * chunkLength * 1024;
             data = new byte[limit - begin];
             Console.WriteLine("Reading chunk " + chkrq.CID + " (" + (chkrq.CID * chunkLength * 1024) + " => " + limit + ")");
             fs.Seek(begin, SeekOrigin.Begin);
             fs.Read(data, 0, (limit - begin));
             fs.Close();
         }
         ChunkResponse chkrs = new ChunkResponse(servingBuffer, chkrq.RID, chkrq.CID, data, myAddress);
         if (chkrq.SenderAddress != myAddress)
         {
             ITransportProtocol svc = ChannelFactory<ITransportProtocol>.CreateChannel(
                 new NetUdpBinding(), new EndpointAddress(chkrq.SenderAddress)
             );
             svc.ReturnChunk(chkrs);
         }
         else
         {
             this.ReturnChunk(chkrs);
         }
         log.Debug("Chunk sent to " + chkrq.SenderAddress);
     }
     servingBuffer--;
 }
 /// <summary>
 /// This example shows how to load from the repository a previously stored track.
 /// </summary>
 public static void LoadTrackFromDb()
 {
     ExampleHelper.ExampleMethodPrint("Load a TrackModel from the database",MethodInfo.GetCurrentMethod());
     TrackModel track= new TrackModel();
     RepositoryResponse resp = _trackRep.GetByKey<TrackModel.Track>(_hid, track);
     Console.WriteLine("Response : "+resp);
     if (resp>=0) {
         ExampleHelper.DumpObjectProperties(track.GetAsDatabaseType());
     }
 }
Example #4
0
 /// <summary>
 /// Updates Track information
 /// </summary>
 /// <param name="mdl">New value of the Track</param>
 /// <returns></returns>
 public RepositoryResponse Update(TrackModel mdl)
 {
     return this._repository.Save(mdl);
 }
 /// <summary>
 /// This example shows how to save a track in a generic repository
 /// </summary>
 public static void StoreTrackInDb()
 {
     ExampleHelper.ExampleMethodPrint("Create a TrackModel from file and store it in the database",MethodInfo.GetCurrentMethod());
     TrackModel track = new TrackModel("..\\..\\Resource\\SevenMP3.mp3");
     _hid = track.GetAsDatabaseType().Id;
     ExampleHelper.DumpObjectProperties(track.GetAsDatabaseType());
     Console.WriteLine("Response : "+_trackRep.Save(track));
 }
Example #6
0
 /// <summary>
 /// Inserts the track file information into the repository
 /// </summary>
 /// <param name="mdl">Track to insert</param>
 /// <returns>RepositoryResponse.RepositoryInsert if the track information has been successfully inserted,
 /// RepositoryResponse.RepositoryGenericError otherwise</returns>
 public RepositoryResponse InsertTrack(TrackModel mdl)
 {
     RepositoryResponse rsp = this._repository.Save(mdl);
     if (rsp >= 0 ) {
         return RepositoryResponse.RepositoryInsert;
     } else {
         return rsp;
     }
 }
Example #7
0
 /// <summary>
 /// Inserts the track file information into the repository reading it directly from the file.
 /// </summary>
 /// <param name="filename">Filename where the information can be read</param>
 /// <returns>RepositoryResponse.RepositoryInsert if the track information has been successfully inserted,
 /// RepositoryResponse.RepositoryGenericError otherwise</returns>
 public RepositoryResponse InsertTrack(string filename)
 {
     if (System.IO.File.Exists(filename)) {
         TrackModel tk=new TrackModel(filename);
         RepositoryResponse rsp=this._repository.Save(tk);
         if (rsp>=0) {
             return RepositoryResponse.RepositoryInsert;
         } else {
             return rsp;
         }
     } else if(System.IO.Directory.Exists(filename)) {
         string[] files=System.IO.Directory.GetFiles(filename,"*.mp3");
         Parallel.ForEach(files,file => {
             TrackModel tk = new TrackModel(file);
             this._repository.Save(tk);
         });
         return RepositoryResponse.RepositoryInsert;
     } else {
         return RepositoryResponse.RepositoryGenericError;
     }
 }
Example #8
0
 /// <summary>
 /// Gets all tracks from repository
 /// </summary>
 /// <returns>A collection containing all tracks</returns>
 public ICollection<TrackModel> GetAll()
 {
     LinkedList<TrackModel> list = new LinkedList<TrackModel>();
     LinkedList<TrackModel.Track> dbList= new LinkedList<TrackModel.Track>();
     RepositoryResponse rsp = this._repository.GetAll(dbList);
     Parallel.ForEach(dbList, elem =>
     {
         TrackModel tk = new TrackModel();
         tk.LoadFromDatabaseType(elem);
         list.AddLast(tk);
     });
     return list;
 }
Example #9
0
 /// <summary>
 /// Gets information of the track identified by a given key (file hash)
 /// </summary>
 /// <param name="key">Identifier of the track (file hash)</param>
 /// <returns></returns>
 public TrackModel Get(string key)
 {
     TrackModel tk = new TrackModel();
     if (this._repository.GetByKey<TrackModel.Track>(key, tk) >= 0)
     {
         return tk;
     }
     else
     {
         return null;
     }
 }
Example #10
0
 /// <summary>
 /// <see cref="PeerLibrary.IPeer"/>
 /// </summary>
 /// <param name="filename">filename of the file to download</param>
 /// <returns>true if the filename have been store; false otherwise</returns>
 public bool StoreFile(string filename)
 {
     log.Info("Storing file:" + filename);
     TrackModel track = new TrackModel(filename);
     TrackModel sameTk = new TrackModel();
     this.trackRep.GetByKey<TrackModel.Track>(track.GetAsDatabaseType().Id, sameTk);
     if ((sameTk != null) && (sameTk.GetAsDatabaseType().Id == track.GetAsDatabaseType().Id))
     {
         log.Warn("Unable to store duplicate file "+filename+" !");
         return false;
     }
     else
     {
         this.trackRep.Save(track);
         this.kademliaLayer.Put(filename);
         return true;
     }
 }