Eigene Dateien/Visual Studio 2005/Projects/DCPlusPlus/DCPlusPlus/Queue.cs

Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using System.Xml.Serialization;
00005 using System.IO;
00006 using System.Collections;
00007 
00008 namespace DCPlusPlus
00009 {
00010     [Serializable]
00011     public class Queue : ICollection<Queue.QueueEntry>
00012     {
00013 
00014         public class QueueEntry
00015         {
00016             public class Source
00017             {
00018                 public delegate void SourceStatusChangedEventHandler(Source source);
00019                 public event SourceStatusChangedEventHandler SourceStatusChanged;
00020 
00021                 protected string user_name = "";
00022                 public string UserName
00023                 {
00024                     get
00025                     {
00026                         return (user_name);
00027                     }
00028                     set
00029                     {
00030                         user_name = value;
00031                     }
00032                 }
00033                 protected string filename = "";
00034                 public string Filename
00035                 {
00036                     get
00037                     {
00038                         return (filename);
00039                     }
00040                     set
00041                     {
00042                         filename = value;
00043                     }
00044                 }
00045                 //TODO add last connection attempt time field
00046                 protected bool is_online = false;
00047                 [XmlIgnoreAttribute]
00048                 public bool IsOnline
00049                 {
00050                     get
00051                     {
00052                         //lock (source_lock)
00053                         //{
00054                         return (is_online);
00055                         //}
00056                     }
00057                     set
00058                     {
00059                         //lock (source_lock)
00060                         //{
00061                         is_online = value;
00062                         if (SourceStatusChanged != null)
00063                             SourceStatusChanged(this);
00064                         //}
00065                     }
00066                 }
00067                 //TODO maybe scratch this .. or replace it with a find hub of user function
00068                 protected Hub hub = null;
00069                 [XmlIgnoreAttribute]
00070                 public Hub Hub
00071                 {
00072                     get
00073                     {
00074                         return (hub);
00075                     }
00076                     set
00077                     {
00078                         hub = value;
00079                     }
00080                 }
00081                 public bool HasHub
00082                 {
00083                     get
00084                     {
00085                         if (hub != null) return (true);
00086                         else return (false);
00087                     }
00088                 }
00089                 /*
00090                 protected string ip = "";
00091                 [XmlIgnoreAttribute]
00092                 public Hub IP
00093                 {
00094                     get
00095                     {
00096                         return (ip);
00097                     }
00098                     set
00099                     {
00100                         ip = value;
00101                     }
00102                 }
00103                 protected int port = "";
00104                 [XmlIgnoreAttribute]
00105                 public Hub Port
00106                 {
00107                     get
00108                     {
00109                         return (port);
00110                     }
00111                     set
00112                     {
00113                         port = value;
00114                     }
00115                 }
00116                 public bool HasAddress
00117                 {
00118                     get
00119                     {
00120                         if (!string.IsNullOrEmpty(ip)) return (true);
00121                         else return (false);
00122                     }
00123                 }
00124                 */
00125                 public Source()
00126                 {
00127                 }
00128                 public Source(string user_name, string filename)
00129                 {
00130                     this.user_name = user_name;
00131                     this.filename = filename;
00132                 }
00133                 public Source(string user_name, string filename, Hub source_hub)
00134                 {
00135                     this.hub = source_hub;
00136                     if (this.hub != null) is_online = true;
00137                     this.user_name = user_name;
00138                     this.filename = filename;
00139                 }
00140                 public void Ungrab()
00141                 {
00142                     SourceStatusChanged = null;
00143                 }
00144             }
00145 
00146             public delegate void EntrySourceAddedEventHandler(QueueEntry entry, QueueEntry.Source source);
00147             public event EntrySourceAddedEventHandler EntrySourceAdded;
00148             public delegate void EntrySourceRemovedEventHandler(QueueEntry entry, QueueEntry.Source source);
00149             public event EntrySourceRemovedEventHandler EntrySourceRemoved;
00150             public delegate void EntrySourceStatusChangedEventHandler(QueueEntry entry, QueueEntry.Source source);
00151             public event EntrySourceStatusChangedEventHandler EntrySourceStatusChanged;
00152             public delegate void EntryClaimedEventHandler(QueueEntry entry, Peer claiming_peer);
00153             public event EntryClaimedEventHandler EntryClaimed;
00154             public delegate void EntryUnclaimedEventHandler(QueueEntry entry);
00155             public event EntryUnclaimedEventHandler EntryUnclaimed;
00156 
00157             protected Object sources_lock = new Object();
00158             /*[XmlIgnoreAttribute]
00159             public Object SourcesLock
00160             {
00161                 get
00162                 {
00163                     return (sources_lock);
00164                 }
00165                 set
00166                 {
00167                     sources_lock = value;
00168                 }
00169             }*/
00170             protected Object is_in_use_lock = new Object();
00171             /*[XmlIgnoreAttribute]
00172             public Object IsInUseLock
00173             {
00174                 get
00175                 {
00176                     return (is_in_use_lock);
00177                 }
00178                 set
00179                 {
00180                     is_in_use_lock = value;
00181                 }
00182             }*/
00183 
00184             protected List<Source> sources = new List<Source>();
00185             public List<Source> Sources
00186             {
00187                 get
00188                 {
00189                     return (sources);
00190                 }
00191             }
00192             protected long filesize = 0;
00193             public long Filesize
00194             {
00195                 get
00196                 {
00197                     return (filesize);
00198                 }
00199                 set
00200                 {
00201                     filesize = value;
00202                 }
00203             }
00204             //protected bool has_tth = false;
00205             public bool HasTTH
00206             {
00207                 get
00208                 {
00209                     if (!string.IsNullOrEmpty(tth)) return (true);
00210                     else return (false);
00211                     //return (has_tth);
00212                 }
00213             }
00214             protected string tth = "";
00215             public string TTH
00216             {
00217                 get
00218                 {
00219                     return (tth);
00220                 }
00221                 set
00222                 {
00223                     tth = value;
00224                 }
00225             }
00226 
00227 
00228             protected byte[] tthl = new byte[0];
00229             public byte[] TTHL
00230             {
00231                 get
00232                 {
00233                     return (tthl);
00234                 }
00235                 set
00236                 {
00237                     tthl = value;
00238                 }
00239             }
00240 
00241             protected bool want_tthl = false;
00242             public bool WantTTHL
00243             {
00244                 get
00245                 {
00246                     return (want_tthl);
00247                 }
00248                 set
00249                 {
00250                     want_tthl = value;
00251                 }
00252             }
00253 
00254             protected string output_filename = "";
00255             public string OutputFilename
00256             {
00257                 get
00258                 {
00259                     return (output_filename);
00260                 }
00261                 set
00262                 {
00263                     output_filename = value;
00264                 }
00265             }
00266             public enum Priority
00267             {
00268                 higher, normal, lesser
00269             };
00270             protected Priority download_priority = Priority.normal;
00271             public Priority DownloadPriority
00272             {
00273                 get
00274                 {
00275                     return (download_priority);
00276                 }
00277                 set
00278                 {
00279                     download_priority = value;
00280 
00281                 }
00282             }
00283             public enum EntryType
00284             {
00285                 File, Directory, FileList
00286             };
00287             protected EntryType type = EntryType.File;
00288             public EntryType Type
00289             {
00290                 get
00291                 {
00292                     return (type);
00293                 }
00294                 set
00295                 {
00296                     type = value;
00297 
00298                 }
00299             }
00300             public int SourcesOnline
00301             {
00302                 get
00303                 {
00304                     int sources_online = 0;
00305                     lock (sources_lock)
00306                     {
00307                         foreach (Source source in sources)
00308                         {
00309                             if (source.IsOnline) sources_online++;
00310                         }
00311                     }
00312                     return (sources_online);
00313                 }
00314             }
00319             public long FilesizeOnDisk
00320             {
00321                 get
00322                 {
00323                     if (File.Exists(output_filename) == false) return (0);
00324                     FileInfo fi = new FileInfo(output_filename);
00325                     return (fi.Length);
00326                 }
00327             }
00328             protected bool is_in_use = false;
00329             [XmlIgnoreAttribute]
00330             public bool IsInUse
00331             {
00332                 get
00333                 {
00334                     bool ret = false;
00335                     lock (is_in_use_lock)
00336                     {
00337                         ret = is_in_use;
00338                     }
00339                     return (ret);
00340                 }
00341                 /*    set
00342                     {
00343                         lock (sources_lock)
00344                         {
00345                             is_in_use = value;
00346                         }
00347                     }*/
00348             }
00349 
00350 
00351             public bool TryToClaimEntry(Peer claiming_peer)
00352             {
00353                 lock (is_in_use_lock)
00354                 {
00355                     if (!is_in_use)
00356                     {
00357                         is_in_use = true;
00358                         if (EntryClaimed != null)
00359                             EntryClaimed(this, claiming_peer);
00360                         return (true);
00361                     }
00362                     else return (false);
00363                 }
00364             }
00365             public void UnclaimEntry()
00366             {
00367                 lock (is_in_use_lock)
00368                 {
00369                     is_in_use = false;
00370                 }
00371                 if (EntryUnclaimed != null)
00372                     EntryUnclaimed(this);
00373             }
00374 
00375             public QueueEntry()
00376             {
00377              
00378             }
00379 
00380             public void StartDownload()
00381             {
00382                 lock (sources_lock)
00383                 {//TODO put this in queue class
00384                     foreach (Source source in sources)
00385                     {
00386                         if (source.HasHub && source.IsOnline)
00387                         {
00388                             source.Hub.SendConnectToMe(source.UserName); //signal download to hub to start it
00389                         }
00390                     }
00391                 }
00392                 //sources strategy 
00393                 //for selection and skipping if source offline
00394                 //source offline detection
00395 
00396                 //result.Hub.SendCommand("ConnectToMe", result.UserName); //signal download to hub to start it
00397                 //Console.WriteLine("Hub was not resolved from result hub address: " + result.HubAddress);
00398 
00399             }
00400             public Source FindFirstSourceByUser(string username)
00401             {
00402                 Source ret = null;
00403                 lock (sources_lock)
00404                 {
00405                     foreach (Source source in sources)
00406                     {
00407                         if (source.UserName == username) ret = source;
00408                     }
00409                 }
00410                 return (ret);
00411             }
00412             public Source FindFirstSourceByUserAndHub(string username, Hub hub)
00413             {
00414                 Source ret = null;
00415                 lock (sources_lock)
00416                 {
00417                     foreach (Source source in sources)
00418                     {
00419                         if (source.UserName == username && source.HasHub && source.Hub == hub) ret = source;
00420                     }
00421                 }
00422                 return (ret);
00423             }
00424             public void UpdateSourcesByUsername(string username, Hub source_hub, bool is_online)
00425             {
00426                 lock (sources_lock)
00427                 {
00428                     foreach (Source source in sources)
00429                     {
00430                         if (source.UserName == username)
00431                         {
00432                             source.Hub = source_hub;
00433                             source.IsOnline = is_online;
00434 
00435                         }
00436                     }
00437                 }
00438             }
00439             public bool AddSource(Source me)
00440             {
00441                 if (FindFirstSourceByUser(me.UserName) == null)
00442                 {
00443                     lock (sources_lock)
00444                     {
00445                         sources.Add(me);
00446                     }
00447                     me.SourceStatusChanged += delegate(Source status_changed)
00448                      {
00449                          if (EntrySourceStatusChanged != null)
00450                              EntrySourceStatusChanged(this, status_changed);
00451                      };
00452                     if (EntrySourceAdded != null)
00453                         EntrySourceAdded(this, me);
00454                     return (true);
00455                 }
00456                 else
00457                 {
00458                     return (false);
00459                 }
00460             }
00461 
00462             public void DeleteOutputFile()
00463             {
00464                 if (File.Exists(output_filename))
00465                 {
00466                     try
00467                     {
00468                         File.Delete(output_filename);
00469                     }
00470                     catch (Exception ex)
00471                     {
00472                         Console.WriteLine("Error deleting file: "+ output_filename);
00473                     }
00474                 }
00475             }
00476 
00477             
00478             public void RemoveSource(Source me)
00479             {
00480                 lock (sources_lock)
00481                 {
00482                     sources.Remove(me);
00483                 }
00484                 me.Ungrab();
00485                 if (EntrySourceRemoved != null)
00486                     EntrySourceRemoved(this, me);
00487             }
00488 
00489             public void ClearSources()
00490             {
00491                 lock (sources_lock)
00492                 {
00493                     foreach (Source source in sources)
00494                     {
00495                         sources.Remove(source);
00496                         source.Ungrab();
00497                         if (EntrySourceRemoved != null)
00498                             EntrySourceRemoved(this, source);
00499                     }
00500                 }
00501             }
00502 
00503 
00504             public void GrabSources()
00505             {
00506                 lock (sources_lock)
00507                 {
00508                     foreach (Source source in sources)
00509                     {
00510                         source.SourceStatusChanged += delegate(Source status_changed)
00511                         {
00512                             if (EntrySourceStatusChanged != null)
00513                                 EntrySourceStatusChanged(this, status_changed);
00514                         };
00515 
00516                     }
00517                 }
00518             }
00519             //TODO check if sources get ungrabbed too at the end
00520             public void Ungrab()
00521             {
00522                 EntrySourceAdded = null;
00523                 EntrySourceRemoved = null;
00524                 EntryUnclaimed = null;
00525                 EntryClaimed = null;
00526                 EntrySourceStatusChanged = null;
00527             }
00528 
00529         }
00530         
00531         public delegate void EntryAddedEventHandler(QueueEntry entry);
00532         public event EntryAddedEventHandler EntryAdded;
00533         public delegate void EntryCompletedEventHandler(QueueEntry entry);
00534         public event EntryCompletedEventHandler EntryCompleted;
00535         public delegate void EntryRemovedEventHandler(QueueEntry entry);
00536         public event EntryRemovedEventHandler EntryRemoved;
00537         public delegate void EntriesChangedEventHandler();
00538         public event EntriesChangedEventHandler EntriesChanged;
00539         public delegate void EntriesClearedEventHandler();
00540         public event EntriesClearedEventHandler EntriesCleared;
00541 
00542         public event QueueEntry.EntrySourceAddedEventHandler EntrySourceAdded;
00543         public event QueueEntry.EntrySourceRemovedEventHandler EntrySourceRemoved;
00544         public event QueueEntry.EntrySourceStatusChangedEventHandler EntrySourceStatusChanged;
00545         public event QueueEntry.EntryClaimedEventHandler EntryClaimed;
00546         public event QueueEntry.EntryUnclaimedEventHandler EntryUnclaimed;
00547 
00548 
00549         protected List<QueueEntry> items = new List<QueueEntry>();
00550         //[XmlArrayAttribute("Queue")]
00551         public List<QueueEntry> Items
00552         {
00553             get
00554             {
00555                 return (items);
00556             }
00557             set
00558             {
00559                 items = value;
00560             }
00561         }
00562         public int Count
00563         {
00564             get
00565             {
00566                 return (items.Count);
00567             }
00568         }
00569         protected string download_directory = ".\\downloads";
00570         public string DownloadDirectory
00571         {
00572             get
00573             {
00574                 return (download_directory);
00575             }
00576             set
00577             {
00578                 download_directory = value;
00579                 if (download_directory.EndsWith("\\"))
00580                     download_directory.TrimEnd('\\');
00581             }
00582         }
00583         protected string filelists_directory = ".\\filelists";
00584         public string FileListsDirectory
00585         {
00586             get
00587             {
00588                 return (filelists_directory);
00589             }
00590             set
00591             {
00592                 filelists_directory = value;
00593                 if (filelists_directory.EndsWith("\\"))
00594                     filelists_directory.TrimEnd('\\');
00595             }
00596         }
00597         protected Object queue_lock = new Object();
00598         /*public Object QueueLock
00599         {
00600             get
00601             {
00602                 return (queue_lock);
00603             }
00604             set
00605             {
00606                 queue_lock = value;
00607             }
00608         }*/
00609 
00610         //deprecated , because automatic adding of sources should make this a never to be called again function
00611         public QueueEntry FindExistingEntryForSearchResult(SearchResults.SearchResult result)
00612         {
00613             QueueEntry ret = null;
00614             lock (queue_lock)
00615             {
00616                 foreach (QueueEntry entry in items)
00617                 {
00618                     //if ((entry.OutputFilename == download_directory + "\\" + System.IO.Path.GetFileName(result.Filename) && entry.Filesize == result.Filesize)
00619                     //    || (result.HasTTH && entry.HasTTH && entry.TTH == result.TTH))
00620                     if (result.HasTTH && entry.HasTTH && entry.TTH == result.TTH)
00621                     {
00622                         ret = (entry);
00623                         break;
00624                     }
00625                 }
00626             }
00627             return (ret);
00628         }
00629         public QueueEntry FindExistingEntryForFileList(Hub hub,string username)
00630         {
00631             QueueEntry ret = null;
00632             lock (queue_lock)
00633             {
00634                 foreach (QueueEntry entry in items)
00635                 {
00636                     if (entry.FindFirstSourceByUserAndHub(username, hub) != null)
00637                     {
00638                         ret = entry;
00639                         break;
00640                     }
00641                 }
00642             }
00643             return (ret);
00644         }
00645         private void GrabEntry(QueueEntry entry)
00646         {
00647             entry.EntrySourceAdded += delegate(QueueEntry add_entry, QueueEntry.Source added_source)
00648             {
00649                 if (EntrySourceAdded != null)
00650                     EntrySourceAdded(add_entry, added_source);
00651             };
00652             entry.EntrySourceRemoved += delegate(QueueEntry remove_entry, QueueEntry.Source removed_source)
00653             {
00654                 if (EntrySourceRemoved != null)
00655                     EntrySourceRemoved(remove_entry, removed_source);
00656             };
00657             entry.EntrySourceStatusChanged += delegate(QueueEntry change_entry, QueueEntry.Source changed_source)
00658             {
00659                 if (EntrySourceStatusChanged != null)
00660                     EntrySourceStatusChanged(change_entry, changed_source);
00661             };
00662             entry.EntryClaimed += delegate(QueueEntry claimed_entry, Peer claiming_peer)
00663             {
00664                 if (EntryClaimed != null)
00665                     EntryClaimed(claimed_entry, claiming_peer);
00666             };
00667             entry.EntryUnclaimed += delegate(QueueEntry unclaimed_entry)
00668             {
00669                 if (EntryUnclaimed != null)
00670                     EntryUnclaimed(unclaimed_entry);
00671             };
00672         }
00673 
00674         public void CheckDownloadedData(QueueEntry queueEntry)
00675         {
00676             throw new Exception("The method or operation is not implemented.");
00677         }
00678         /*
00679         public void AddTTHL(QueueEntry me)
00680         {
00681             QueueEntry existing = FindExistingEntryForFileList(hub, username);
00682             if (existing == null)
00683             {
00684                 QueueEntry entry = new QueueEntry();
00685                 entry.Type = QueueEntry.EntryType.FileList;
00686                 string temp_hub_address = hub.Address.Replace(":", "_");
00687                 entry.OutputFilename = filelists_directory + "\\" + temp_hub_address + "-" + Base32.ToBase32String(Encoding.Default.GetBytes(username)) + ".xml.bz2";//TODO .. maybe changes needed here to incorporate other filelist formats
00688                 entry.AddSource(new Queue.QueueEntry.Source(username, "", hub));
00689                 lock (queue_lock)
00690                 {
00691                     items.Add(entry);
00692                 }
00693                 GrabEntry(entry);
00694                 try
00695                 {
00696                     if (EntryAdded != null)
00697                         EntryAdded(entry);
00698                     if (EntriesChanged != null)
00699                         EntriesChanged();
00700                 }
00701                 catch (Exception ex)
00702                 {
00703                     Console.WriteLine("Exception occured in added event callback: " + ex.Message);
00704                 }
00705 
00706             }
00707         }*/
00708 
00709         public void AddFileList(Hub hub,string username)
00710         {
00711             QueueEntry existing = FindExistingEntryForFileList(hub, username);
00712             if (existing == null)
00713             {
00714                 QueueEntry entry = new QueueEntry();
00715                 entry.Type = QueueEntry.EntryType.FileList;
00716                 string temp_hub_address = hub.Address.Replace(":", "_");
00717                 entry.OutputFilename = filelists_directory + "\\" + temp_hub_address + "-" + Base32.ToBase32String(Encoding.Default.GetBytes(username)) + ".xml.bz2";//TODO .. maybe changes needed here to incorporate other filelist formats
00718                 entry.AddSource(new Queue.QueueEntry.Source(username, "", hub));
00719                 lock (queue_lock)
00720                 {
00721                     items.Add(entry);
00722                 }
00723                 GrabEntry(entry);
00724                 try
00725                 {
00726                     if (EntryAdded != null)
00727                         EntryAdded(entry);
00728                     if (EntriesChanged != null)
00729                         EntriesChanged();
00730                 }
00731                 catch (Exception ex)
00732                 {
00733                     Console.WriteLine("Exception occured in added event callback: " + ex.Message);
00734                 }
00735 
00736             }
00737         }
00738         public void AddSearchResult(SearchResults.SearchResult result)
00739         {
00740             if (result.IsFile)
00741             {
00742                 QueueEntry existing = FindExistingEntryForSearchResult(result);
00743                 if (existing != null)
00744                 {//This should be a deprecated case.. never ever be called again :-)
00745                     //lock (queue_lock)
00746                     //{
00747                         existing.AddSource(new QueueEntry.Source(result.UserName, result.Filename, result.Hub));
00748                         //TODO source add event
00749                     //}
00750                     return;
00751                 }
00752                 QueueEntry entry = new QueueEntry();
00753                 entry.Type = QueueEntry.EntryType.File;
00754                 entry.Filesize = result.Filesize;
00755                 entry.OutputFilename = download_directory + "\\" + System.IO.Path.GetFileName(result.Filename); //TODO add directory support somehow
00756                 if (File.Exists(entry.OutputFilename))
00757                 {//already some file existing with this name ... try a (i) at the end increment until file is not there
00758                     int i = 1;
00759                     string new_extension = Path.GetExtension(entry.OutputFilename);
00760 
00761                     string new_filename = Path.GetDirectoryName(entry.OutputFilename) + "\\" + Path.GetFileNameWithoutExtension(entry.OutputFilename) + "(" + i + ")" + new_extension;
00762                     while (File.Exists(new_filename))
00763                     {
00764                         i++;
00765                         new_filename = Path.GetDirectoryName(entry.OutputFilename) + "\\" + Path.GetFileNameWithoutExtension(entry.OutputFilename) + "(" + i + ")" + new_extension;
00766                     }
00767                     entry.OutputFilename = new_filename;
00768                 }
00769                 if (result.HasTTH) entry.TTH = result.TTH;
00770                 //if(result.IsHubResolved)
00771                 entry.AddSource(new Queue.QueueEntry.Source(result.UserName, result.Filename, result.Hub));
00772                 lock (queue_lock)
00773                 {
00774                     items.Add(entry);
00775                 }
00776                 GrabEntry(entry);
00777                 //Console.WriteLine("Queue Entry added: '"+entry.OutputFilename+"'");
00778                 try
00779                 {
00780                     if (EntryAdded != null)
00781                         EntryAdded(entry);
00782                     if (EntriesChanged != null)
00783                         EntriesChanged();
00784                 }
00785                 catch (Exception ex)
00786                 {
00787                     Console.WriteLine("Exception occured in added event callback: " + ex.Message);
00788                 }
00789 
00790             }
00791             //download_directory + "\\" + 
00792 
00793         }
00794 
00795         public void RestartDownload(QueueEntry entry)
00796         {
00797             if (entry.FilesizeOnDisk != 0 && File.Exists(entry.OutputFilename) && !entry.IsInUse)
00798             {
00799                 entry.DeleteOutputFile();
00800             }
00801         }
00802 
00803         public Queue.QueueEntry FindFirstQueueEntryBySourceUser(string username)
00804         {
00805             QueueEntry ret = null;
00806             lock (queue_lock)
00807             {
00808 
00809                 foreach (Queue.QueueEntry entry in items)
00810                 {
00811                     foreach (QueueEntry.Source source in entry.Sources)
00812                     {
00813                         if (source.UserName == username)
00814                         {
00815                             ret = entry;
00816                             break;
00817                         }
00818                     }
00819                     if (ret != null)
00820                         break;
00821                 }
00822             }
00823             return (ret);
00824         }
00825         public Queue.QueueEntry FindFirstUnusedQueueEntryBySourceUser(string username)
00826         {
00827             QueueEntry ret = null;
00828             lock (queue_lock)
00829             {
00830                 foreach (Queue.QueueEntry entry in items)
00831                 {
00832                     foreach (QueueEntry.Source source in entry.Sources)
00833                     {
00834                         if (source.UserName == username && !entry.IsInUse)
00835                         {
00836                             ret = entry;
00837                             break;
00838                         }
00839                     }
00840                     if (ret != null)
00841                         break;
00842                 }
00843             }
00844             return (ret);
00845         }
00846         public Queue.QueueEntry FindQueueEntryByOutputFilename(string output_filename)
00847         {
00848             QueueEntry ret = null;
00849             lock (queue_lock)
00850             {
00851                 foreach (Queue.QueueEntry entry in items)
00852                 {
00853                     if (entry.OutputFilename == output_filename)
00854                     {
00855                         ret = entry;
00856                         break;
00857                     }
00858                 }
00859             }
00860             return (ret);
00861         }
00862         public Queue.QueueEntry FindQueueEntryByTTH(string tth)
00863         {
00864             QueueEntry ret = null;
00865             lock (queue_lock)
00866             {
00867                 foreach (Queue.QueueEntry entry in items)
00868                 {
00869                     if (entry.HasTTH && entry.TTH == tth)
00870                     {
00871                         ret =  entry;
00872                         break;
00873                     }
00874                 }
00875             }
00876             return (ret);
00877         }
00878         public void UpdateSourcesByUsername(string username, Hub source_hub, bool is_online)
00879         {
00880             lock (queue_lock)
00881             {
00882                 foreach (Queue.QueueEntry entry in items)
00883                 {
00884                     entry.UpdateSourcesByUsername(username, source_hub, is_online);
00885                 }
00886             }
00887         }
00888         public void Remove(string output_filename)
00889         {
00890                 foreach (QueueEntry entry in items)
00891                 {
00892                     if (entry.OutputFilename == output_filename)
00893                     {
00894                         lock (queue_lock)
00895                         {
00896                             items.Remove(entry);
00897                         }
00898                         entry.Ungrab();
00899                         try
00900                         {
00901                             if (EntryRemoved != null)
00902                                 EntryRemoved(entry);
00903                             if (EntriesChanged != null)
00904                                 EntriesChanged();
00905                         }
00906                         catch (Exception ex)
00907                         {
00908                             Console.WriteLine("Exception occured in remove event callback: " + ex.Message);
00909                         }
00910                         return;
00911                     }
00912             }
00913         }
00914         public void Add(Queue.QueueEntry item)
00915         {
00916             lock (queue_lock)
00917             {
00918                 items.Add(item);
00919             }
00920             try
00921             {
00922                 if (EntryAdded != null)
00923                     EntryAdded(item);
00924                 if (EntriesChanged != null)
00925                     EntriesChanged();
00926             }
00927             catch (Exception ex)
00928             {
00929                 Console.WriteLine("Exception occured in added event callback: " + ex.Message);
00930             }
00931         }
00932 
00933 
00934         public bool Remove(Queue.QueueEntry item)
00935         {
00936             bool ret = false;
00937             lock (queue_lock)
00938             {
00939                 ret = items.Remove(item);
00940             }
00941             try
00942             {
00943                 if (EntryRemoved != null)
00944                     EntryRemoved(item);
00945                 if (EntriesChanged != null)
00946                     EntriesChanged();
00947             }
00948             catch (Exception ex)
00949             {
00950                 Console.WriteLine("Exception occured in remove event callback: " + ex.Message);
00951             }
00952             return (ret);
00953         }
00954         public void Clear()
00955         {
00956             lock (queue_lock)
00957             {
00958                 items.Clear();
00959             }
00960             try
00961             {
00962 
00963                 if (EntriesCleared != null)
00964                     EntriesCleared();
00965                 if (EntriesChanged != null)
00966                     EntriesChanged();
00967             }
00968             catch (Exception ex)
00969             {
00970                 Console.WriteLine("Exception occured in clear event callback: " + ex.Message);
00971             }
00972         }
00973         public void LoadQueueFromXml(string xml)
00974         {
00975             lock (queue_lock)
00976             {
00977                 items.Clear();
00978             }
00979             if (EntriesCleared != null)
00980                 EntriesCleared();
00981 
00982             Queue q = new Queue();
00983             try
00984             {
00985                 XmlSerializer serializer = new XmlSerializer(typeof(Queue));
00986                 MemoryStream ms = new MemoryStream(System.Text.Encoding.Default.GetBytes(xml));
00987                 q = (Queue)serializer.Deserialize(ms);
00988             }
00989             catch (Exception ex)
00990             {
00991                 Console.WriteLine("Error deserializing queue: " + ex.Message);
00992             }
00993             lock (queue_lock)
00994             {
00995                 items = q.Items;
00996             }
00997             download_directory = q.download_directory;
00998             if (EntryAdded != null)
00999             {
01000                 foreach (QueueEntry entry in items)
01001                 {
01002                     GrabEntry(entry);
01003                     entry.GrabSources();
01004                     EntryAdded(entry);
01005                 }
01006             }
01007             if (EntriesChanged != null)
01008                 EntriesChanged();
01009         }
01010         public string SaveQueueToXml()
01011         {
01012             //nice way but seems to not work with list<> members
01013             string ret = "";
01014             lock (queue_lock)
01015             {
01016                 try
01017                 {
01018                     XmlSerializer serializer = new XmlSerializer(typeof(Queue));
01019                     MemoryStream ms = new MemoryStream();
01020                     serializer.Serialize(ms, this);
01021                     ms.Flush();
01022                     ret = System.Text.Encoding.Default.GetString(ms.GetBuffer(), 0, (int)ms.Length);//TODO ... 4gb crash border
01023                     //ret = ret.TrimEnd((char)0);
01024                 }
01025                 catch (Exception ex)
01026                 {
01027                     Console.WriteLine("Error serializing queue: " + ex.Message);
01028                     ret = null;
01029                 }
01030             }
01031             return (ret);
01032         }
01033         public void LoadQueueFromXmlFile(string filename)
01034         {
01035             //try
01036             //{
01037                 if (!File.Exists(filename)) return;
01038                 LoadQueueFromXml(System.IO.File.ReadAllText(filename));
01039             //}
01040             //catch (Exception ex)
01041             //{
01042             //    Console.WriteLine("Error loading queue from: " + filename + " : " + ex.Message);
01043             //}
01044         }
01045         public void SaveQueueToXmlFile(string filename)
01046         {
01047             try
01048             {
01049                 if (File.Exists(filename + ".backup") && File.Exists(filename))
01050                     File.Delete(filename + ".backup");
01051                 if (File.Exists(filename))
01052                 {
01053                     File.Move(filename, filename + ".backup");
01054                     Console.WriteLine("Created Backup of queue: "+filename+".backup");
01055                 }
01056 
01057                 System.IO.File.WriteAllText(filename, SaveQueueToXml());
01058             }
01059             catch (Exception ex)
01060             {
01061                 Console.WriteLine("Error saving queue to: " + filename + " : " + ex.Message);
01062             }
01063         }
01064 
01065         #region ICollection<QueueEntry> Members
01066         public bool Contains(Queue.QueueEntry item)
01067         {
01068             bool ret = false;
01069             lock (queue_lock)
01070             {
01071                 ret =items.Contains(item);
01072             }
01073             return (ret);
01074         }
01075         public void CopyTo(Queue.QueueEntry[] array, int arrayIndex)
01076         {
01077             lock (queue_lock)
01078             {
01079                 foreach (Queue.QueueEntry entry in items)
01080                 {
01081                     array.SetValue(entry, arrayIndex);
01082                     arrayIndex = arrayIndex + 1;
01083                 }
01084             }
01085         }
01086         public bool IsReadOnly
01087         {
01088             get
01089             {
01090                 return (false);
01091             }
01092         }
01093         #endregion
01094         //TODO check for thread safety
01095         #region IEnumerable<QueueEntry> Members
01096 
01097         public class QueueEnumerator2 : IEnumerator<QueueEntry>
01098         {
01099             private Queue.QueueEntry[] queue_array;
01100             private int Cursor;
01101             public QueueEnumerator2(Queue.QueueEntry[] my_array)
01102             {
01103                 queue_array = my_array;
01104                 Cursor = -1;
01105             }
01106 
01107             #region IEnumerator<QueueEntry> Members
01108 
01109             public object Current
01110             {
01111                 get
01112                 {
01113                     if ((Cursor < 0) || (Cursor == queue_array.Length))
01114                         throw new InvalidOperationException();
01115                     return ((object)queue_array[Cursor]);
01116                 }
01117             }
01118 
01119 
01120             QueueEntry IEnumerator<QueueEntry>.Current
01121             {
01122                 get 
01123                 {
01124                     if ((Cursor < 0) || (Cursor == queue_array.Length))
01125                         throw new InvalidOperationException();
01126                     return (queue_array[Cursor]);
01127                 }
01128             }
01129 
01130             public bool MoveNext()
01131             {
01132                 if (Cursor < queue_array.Length)
01133                     Cursor++;
01134                 return (!(Cursor == queue_array.Length));
01135             }
01136 
01137             public void Reset()
01138             {
01139                 Cursor = -1;
01140             }
01141 
01142 
01143 
01144             #endregion
01145 
01146             #region IDisposable Members
01147 
01148             public void Dispose()
01149             {
01150                 //Dispose();
01151                 //base.Dispose(disposing);
01152             }
01153 
01154             #endregion
01155         }
01156 
01157         public IEnumerator<Queue.QueueEntry> GetEnumerator()
01158         {
01159             return (new QueueEnumerator2(items.ToArray()));
01160         }
01161 
01162         #endregion
01163         #region IEnumerable Members
01164 
01165         public class QueueEnumerator : IEnumerator
01166         {
01167             private Queue.QueueEntry[] queue_array;
01168             private int Cursor;
01169             public QueueEnumerator(Queue.QueueEntry[] my_array)
01170             {
01171                 queue_array = my_array;
01172                 Cursor = -1;
01173             }
01174 
01175 
01176 #region IEnumerator Members
01177 
01178             public object Current
01179             {
01180                 get
01181                 {
01182                     if ((Cursor < 0) || (Cursor == queue_array.Length))
01183                         throw new InvalidOperationException();
01184                     return ((object)queue_array[Cursor]);
01185                 }
01186             }
01187 
01188             public bool MoveNext()
01189             {
01190                 if (Cursor < queue_array.Length)
01191                     Cursor++;
01192                 return (!(Cursor == queue_array.Length));
01193             }
01194 
01195             public void Reset()
01196             {
01197                 Cursor = -1;
01198             }
01199 
01200 #endregion
01201         }
01202 
01203         IEnumerator IEnumerable.GetEnumerator()
01204         {
01205             return (new QueueEnumerator(items.ToArray()));
01206         }
01207 
01208         #endregion
01209 
01210 
01211     }
01212 }

Generated on Wed Mar 7 19:09:21 2007 for DCPlusPlus by  doxygen 1.5.1-p1