Exemplo n.º 1
0
        public static void saveFileInDfs(Common.File file, int numOfReplcas)
        {
            var chunks  = splitFileIntoChunks(file);
            var workers = WorkersUtil.getClientsToActiveWorkers();

            if (workers.Count < numOfReplcas)
            {
                throw new ArgumentException("You can not create more replicas than workers!");
            }

            int workerId = 0;

            foreach (var chunk in chunks)
            {
                for (int i = 0; i < numOfReplcas; i++)
                {
                    var worker = workers [workerId++ % workers.Count];
                    log.InfoFormat("Saving {0} chunk of {1} in {2}", chunk.chunkId, chunk.fileName, worker.BaseUri);

                    var request = new Worker.SaveChunk();
                    request.chunk = chunk;
                    worker.Put(request);
                }
            }
        }
Exemplo n.º 2
0
    /// <summary>
    /// Apply renaming function to the specified
    /// string using data from input tag.
    /// </summary>
    /// <param name="music">Object to apply function to</param>
    /// <returns>Renamed string</returns>
    /// <remarks>
    /// Method needs access to the current objects
    /// file.Metadata. Therefore the client must provide
    /// a <see cref="mp3tag.Metadata"/> object.
    /// </remarks>
    public string Rename(File file)
    {
      string result = String.Empty;
      switch (file.FileType)
      {
        case FileTypes.RELEASE :
          result = ReleaseFunction;
          break;
        case FileTypes.SONG :
          result = SongFunction; break;
        case FileTypes.FLAT :
          result = FlatFunction; break;
      }

      Trace.WriteLine("Applying rename function: " + result + " to " + file.Metadata.Title);
      
      result = result.Replace("%Track%", file.Metadata.Track, StringComparison.OrdinalIgnoreCase);
      result = result.Replace("%ReleaseArtist%", file.Metadata.AlbumArtist, StringComparison.OrdinalIgnoreCase);
      result = result.Replace("%Artist%", file.Metadata.Artist, StringComparison.OrdinalIgnoreCase);
      result = result.Replace("%Release%", file.Metadata.Release, StringComparison.OrdinalIgnoreCase);
      result = result.Replace("%Title%", file.Metadata.Title, StringComparison.OrdinalIgnoreCase);
      result = result.Replace("%Genre%", file.Metadata.Genre, StringComparison.OrdinalIgnoreCase);
      result = result.Replace("%ReleaseYear%", file.Metadata.ReleaseYear, StringComparison.OrdinalIgnoreCase);

      return result;
    }
Exemplo n.º 3
0
        public static Common.File readFileFromDfs(string fileName)
        {
            var lsResult          = listFiles();
            var files             = lsResult.Item1;
            var workersWithErrors = lsResult.Item2;

            if (!files.ContainsKey(fileName))
            {
                throw new Exception("File does not exists!");
            }
            var fileHeader = files [fileName];

            if (fileHeader.corrupted)
            {
                throw new Exception("Can't read file: File is corrupted!");
            }

            var file = new Common.File(fileName, fileHeader.chunksCount);

            //int idx = 0;

            foreach (var chunk in fileHeader.getChunksSorted())
            {
                bool chunkDownloaded = false;
                foreach (var worker in chunk.storedInWorkers)
                {
                    var client = new JsonServiceClient(worker);
                    Worker.GetChunkResponse response = null;
                    try {
                        log.DebugFormat("Getting chunk {1}-{0} from {2}", chunk.chunkId, fileName, worker);
                        response = client.Get(new Worker.GetChunk {
                            chunkId = chunk.chunkId, FileName = fileName
                        });
                    } catch (Exception e) {
                        log.Warn(e);
                    }
                    if (response == null || response.IsErrorResponse())
                    {
                        workersWithErrors.Add(worker);
                        continue;
                    }

                    file.data.AddRange(response.Result.data);
                    //Array.Copy (response.Result.data, 0, file.data, idx, response.Result.data.Length);
                    //idx += response.Result.data.Length;
                    chunkDownloaded = true;
                    break;
                }
                if (!chunkDownloaded)
                {
                    throw new Exception("Donwloading of chunk {0} of {1} failed!".FormatWith(chunk.chunkId, file.fileName));
                }
            }

            return(file);
        }
Exemplo n.º 4
0
    /// <summary>
    /// Searches service for supplied metadata.
    /// </summary>
    /// <param name="album">Metadata to search service for</param>
    /// <param name="maxResults">Maximum number of results to be returned
    /// by the service search</param>
    public ITunesService(File album, int maxResults)
    {
      Trace.WriteLine("Initializing MusicBrainz session");

      // Initialize search tokens
      Tokens = new Dictionary<string, string>();
      Metadata tag = album.Metadata.First();

      // Strip url-sensitive chars
      Regex pattern = new Regex("[()_-]");
      Tokens.Add("artist", pattern.Replace(tag.Artist, " "));
      Tokens.Add("album", pattern.Replace(tag.Album, " "));
      Tokens.Add("title", pattern.Replace(tag.Title, " "));

      MaxResults = maxResults;

      Trace.WriteLine("Session started");
    }
Exemplo n.º 5
0
        private static List <Chunk> splitFileIntoChunks(Common.File file)
        {
            List <Chunk> chunks      = new List <Chunk> ();
            var          fileContent = file.data;

            if (fileContent.Count < file.chunksCount)
            {
                throw new ArgumentException("You can not create more chunks than elements!");
            }

            int sizeOfChunk   = fileContent.Count / file.chunksCount;
            int howManyChunks = Convert.ToInt32(Math.Ceiling(fileContent.Count / (double)sizeOfChunk));

            for (int i = 0; i < howManyChunks; i++)
            {
                var start = i * sizeOfChunk;
                var size  = Math.Min(fileContent.Count - start, sizeOfChunk);
                //var data = new ArraySegment<string> (fileContent, start, end);
                chunks.Add(new Chunk {
                    fileName = file.fileName, chunkId = chunks.Count, data = fileContent.GetRange(start, size)
                });
            }
            return(chunks);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Add item to child collection if one exists
 /// </summary>
 /// <param name="Item">Item to be added to collection</param>
 /// <remarks>
 /// Only implemented by composite extension. 
 /// </remarks>
 public virtual void Add(File file)
 {
   throw new NotImplementedException();
 }
Exemplo n.º 7
0
    /// <summary>
    /// Constructs last.fm service object 
    /// assigning data from tag into local search
    /// tokens. A last.fm authenticated session
    /// is also setup based on user parameters and keys.
    /// </summary>
    /// <param name="tag">Song tag to search for</param>
    public LastfmService(File release, int maxMaxResults=20)
    {
      Trace.WriteLine("Initializing LastFM Session");
      Metadata metadata = release.Metadata;

      // Initialize search tokens
      Tokens = new Dictionary<string, string>();
      Tokens.Add("artist", metadata.Artist);
      Tokens.Add("release", metadata.Release);
      Tokens.Add("title", metadata.Title);

      MaxResults = maxMaxResults;

      // Initialize lastfm session
      session = new Session(key, secret);
      session.Authenticate(username, Lastfm.Utilities.md5(passwd));
      Trace.WriteLine("Session established with key: " + session.SessionKey);
    }
Exemplo n.º 8
0
 /// <summary>
 /// Calculates hash value of file based on the 
 /// concrete checksum class.
 /// </summary>
 /// <param name="file">File to calculate the hash for</param>
 /// <returns>String hash representation</returns>
 public abstract StringBuilder Hash(File file);