Exemple #1
0
        public static void AddDownload(byte[] fileHash, string fileName, long fileSize, string subFolder)
        {
            if (fileHash == null)
                throw new ArgumentNullException("fileHash");
            if (fileHash.Length != 64)
                throw new ArgumentException();
            if (fileName == null)
                throw new ArgumentNullException("fileName");
            if (fileSize < 0)
                throw new ArgumentOutOfRangeException("fileSize");

            try
            {
                m_DownloadsAndQueue.Lock();
                if (m_DownloadsAndQueue.ContainsKey(ByteArrayToString(fileHash), DownloadCollection.KeyAccess.FileHash))
                    return;
                Download download = new Download(fileHash, fileName, fileSize);
                if (m_DownloadsAndQueue.Count >= Constants.MaximumDownloadsCount)
                {
                    download.SetSubFolderAndTime(subFolder, null);
                    download.RemoveSources();
                    if (bool.Parse(Settings.Instance["NewDownloadsToBeginngingOfQueue"]))
                        m_DownloadsAndQueue.Insert(Constants.MaximumDownloadsCount, download);
                    else
                        m_DownloadsAndQueue.Add(download);
                }
                else
                {
                    download.SetSubFolderAndTime(subFolder, DateTime.Now);
                    m_DownloadsAndQueue.Add(download);
                }
            }
            finally
            {
                m_DownloadsAndQueue.Unlock();
            }
            DownloadsXmlWriter.Write(m_DownloadsFilePath, m_DownloadsAndQueue);
        }
Exemple #2
0
        public static void AddDownload(Search.Result result, string subFolder)
        {
            if (result == null)
                throw new ArgumentNullException("result");

            try
            {
                m_DownloadsAndQueue.Lock();
                if (m_DownloadsAndQueue.ContainsKey(ByteArrayToString(result.FileHash), DownloadCollection.KeyAccess.FileHash))
                    return;
                Download download = new Download(result);
                if (m_DownloadsAndQueue.Count >= Constants.MaximumDownloadsCount)
                {
                    download.SetSubFolderAndTime(subFolder, null);
                    download.RemoveSources();
                    if (bool.Parse(Settings.Instance["NewDownloadsToBeginngingOfQueue"]))
                        m_DownloadsAndQueue.Insert(Constants.MaximumDownloadsCount, download);
                    else
                        m_DownloadsAndQueue.Add(download);
                }
                else
                {
                    download.SetSubFolderAndTime(subFolder, DateTime.Now);
                    m_DownloadsAndQueue.Add(download);
                }
            }
            finally
            {
                m_DownloadsAndQueue.Unlock();
            }
            DownloadsXmlWriter.Write(m_DownloadsFilePath, m_DownloadsAndQueue);
        }
Exemple #3
0
 /// <summary>
 /// Neue ResumeDownloads()
 /// 10.06.2009 Lars
 /// 03.07.2009 Lars (Neue Downloadwarteschlange)
 /// 04.07.2009 Lars (Einfacheres und besseres Handling)
 /// </summary>
 private static void ResumeDownloads()
 {
     try
     {
         // Alle gesicherten Downloads einlesen
         RIndexedHashtable<string, XmlNode> downloadsXml = new RIndexedHashtable<string, XmlNode>();
         if (File.Exists(m_DownloadsFilePath))
         {
             XmlDocument downloadsXmlDocument = new XmlDocument();
             downloadsXmlDocument.Load(m_DownloadsFilePath);
             foreach (XmlNode downloadNode in downloadsXmlDocument.SelectSingleNode("downloads"))
                 try
                 {
                     downloadsXml.Add(downloadNode.Attributes["hash"].InnerText, downloadNode);
                 }
                 catch (Exception ex)
                 {
                     m_Logger.Log(ex, "A download cannot be resumed due to non existent information about it!");
                     continue;
                 }
         }
         // Alle vorhandenen Dateien durchgehen
         RList<Download> temporary = new RList<Download>(downloadsXml.Count);
         foreach (string filePath in Directory.GetFiles(Settings.Instance["TemporaryDirectory"]))
         {
             string fileName = new FileInfo(filePath).Name;
             try
             {
                 if (!Regex.IsMatch(fileName, "^[0-9A-F]{128,128}$", RegexOptions.IgnoreCase))
                 {
                     m_Logger.Log("The file \"{0}\" is no valid temporary download!", fileName);
                 }
                 XmlNode node;
                 if (!downloadsXml.TryGetValue(fileName, out node))
                 {
                     m_Logger.Log("The download of \"{0}\" cannot be resumed due to non existent information about it!", fileName);
                     continue;
                 }
                 bool hasInformation = true;
                 if ((node as XmlElement).HasAttribute("hasinformation") && (node as XmlElement).GetAttribute("hasinformation") == "none")
                     hasInformation = false;
                 string lastSeenString = null;
                 DateTime? lastSeen = null;
                 string lastReceptionString = null;
                 DateTime? lastReception = null;
                 String subfolder = string.Empty;
                 if (node.SelectSingleNode("lastseen") != null)
                 {
                     lastSeenString = node.SelectSingleNode("lastseen").InnerText;
                     if (lastSeenString != null && lastSeenString.Length > 0)
                         lastSeen = DateTime.Parse(lastSeenString);
                 }
                 if (node.SelectSingleNode("lastreception") != null)
                 {
                     lastReceptionString = node.SelectSingleNode("lastreception").InnerText;
                     if (lastReceptionString != null && lastReceptionString.Length > 0)
                         lastReception = DateTime.Parse(lastReceptionString);
                 }
                 if (node.SelectSingleNode("subfolder") != null)
                     subfolder = node.SelectSingleNode("subfolder").InnerText;
                 Download download = new Download(Core.FileHashStringToFileHash(fileName), node.SelectSingleNode("filename").InnerText, long.Parse(node.SelectSingleNode("filesize").InnerText), hasInformation, lastSeen, lastReception, hasInformation ? Convert.FromBase64String(node.SelectSingleNode("sectorsmap").InnerText) : null);
                 download.SetSubFolderAndTime(subfolder, null);
                 temporary.Add(download);
             }
             catch (Exception ex)
             {
                 m_Logger.Log(ex, "An exception was thrown while resuming the download of \"{0}\"!", fileName);
             }
         }
         // Wiederaufzunehmende Download sortieren
         for (int n = 1; n <= temporary.Count - 1; n++)
             for (int m = 0; m < temporary.Count - n; m++)
             {
                 Download a = temporary[m];
                 Download b = temporary[m + 1];
                 if (downloadsXml.IndexOfKey(a.FileHashString) > downloadsXml.IndexOfKey(b.FileHashString))
                 {
                     temporary[m] = b;
                     temporary[m + 1] = a;
                 }
             }
         // Downloads wiederaufnehmen
         try
         {
             m_DownloadsAndQueue.Lock();
             foreach (Download download in temporary)
                 m_DownloadsAndQueue.Add(download);
         }
         finally
         {
             m_DownloadsAndQueue.Unlock();
         }
     }
     catch (Exception ex)
     {
         m_Logger.Log(ex, "An exception was thrown while resuming downloads!");
     }
     finally
     {
         // Erst jetzt kann die downloads.xml wieder geschreiben werden...
         DownloadsXmlWriter.SetIsReady();
     }
 }