QueueHandler() { collections = new ArrayList(); currentFileJob = new FileJob(); // Inits thread ThreadStart job = new ThreadStart(Run); Thread thread = new Thread(job); thread.IsBackground = true; thread.Start(); }
public void setParent(ref FileJob parent) { this.parent = parent; }
public Segment getNextQueueItem() { if (currentFileJob == null) { if (collections.Count == 0) { return null; } FileCollection coll = (FileCollection)collections[0]; currentFileJob = coll.GetNextFileJob();//coll.files[coll.FileProgress++]; } lock (this) { if ((currentFileJob.SegmentCount - currentFileJob.SegmentProgress) == 0) { if (collections.Count != 0) { foreach (FileCollection col in collections) { if (col.status != CollectionStatus.PAUSE && col.FilesLeft > 0) { col.status = CollectionStatus.DOWNLOADING; currentFileJob = col.GetNextFileJob();//(FileJob)col.files[col.FileProgress++]; break; } } } return null; } else if (currentFileJob.parent.status != CollectionStatus.PAUSE) { return (Segment)currentFileJob.segments[currentFileJob.SegmentProgress++]; } else return null; } }
public void removeCollection(FileCollection collection) { if (currentFileJob != null && currentFileJob.parent == collection) currentFileJob = null; if (collection.id != nextId - 1) { for (int i = collection.id + 1; i < nextId; i++) { foreach (FileCollection col in collections) { if (col.id == i) { col.id--; break; } } } } else nextId--; collections.Remove(collection); changed = true; }
//// Generates queues for a filecollection object //public static Queue genQueue(ArrayList list) //{ // Queue queue = new Queue(list.Count); // foreach (FileJob job in list) // { // job.queue = new Queue(job.segments.Count); // foreach (Segment seg in job.segments) // { // job.queue.Enqueue(seg); // } // queue.Enqueue(job); // } // return queue; //} // Parses XML structure into file objects private static List<FileJob> ParseXML(XmlDocument document, FileCollection filecollection) { XmlNodeList nodes = document.GetElementsByTagName("file"); List<FileJob> files = new List<FileJob>(); Random random = new Random(); int collectionID = random.Next(); int fileId = 0; // Parsing for each file node foreach (XmlElement fileNode in nodes) { FileJob filejob = new FileJob(ref filecollection); filejob.filename = ""+collectionID; XmlAttributeCollection collection = fileNode.Attributes; // File Attributes foreach (XmlAttribute attr in collection) { switch (attr.Name) { case "poster": filejob.poster = attr.Value; break; case "date": filejob.date = int.Parse(attr.Value); break; case "subject": if (attr.Value.Contains("\"")) { // try to get inner filename... try { int first = attr.Value.IndexOf("\"") + 1; string filename = attr.Value.Substring(first, attr.Value.Length - first); filename = filename.Substring(0, filename.IndexOf("\"")); filejob.filename = filejob.filename + "-"+ filename.Substring(0, filename.LastIndexOf(".")+4); } catch (Exception ex) { filejob.filename = filejob.filename+"-"+fileId; } } else filejob.filename = filejob.filename + "-" + fileId; filejob.subject = attr.Value; break; } } int maxnumber = 0; foreach (XmlElement subNode in fileNode.ChildNodes) { if (subNode.Name == "segments") { foreach (XmlElement segmentNode in subNode) { Segment segment = new Segment(); segment.addr = "<"+segmentNode.InnerXml+">"; segment.addr = segment.addr.Replace("&", "&"); segment.setParent(ref filejob); foreach (XmlAttribute attr in segmentNode.Attributes) { switch (attr.Name) { case "bytes": segment.bytes = int.Parse(attr.Value); filejob.size += (ulong)segment.bytes; break; case "number": segment.id = int.Parse(attr.Value); if (segment.id > maxnumber) maxnumber = segment.id; break; } } // Check and add element to arraylist if (segment.id != 0 && segment.bytes != 0 && segment.addr.Length > 10) filejob.segments.Add(segment); } } else if (subNode.Name == "groups") { filejob.groups.Add(subNode.FirstChild.InnerText); } } filejob.yparts = maxnumber; // Will fail if source isnt yenc // Sort segments filejob.segments.Sort(new SegmentComparer()); files.Add(filejob); fileId++; } return files; }