コード例 #1
0
        internal VirtualSlot Add(string Name, List<NNTPInput> cList, CancellationToken vToken, ManualResetEventSlim vWait)
        {
            if (cList == null) { return null; }
            if (Name == null) { return null; }

            if (cList.Count == 0) { return null; }

            List<VirtualFile> vFiles = new List<VirtualFile>();

            foreach (NNTPInput cC in cList)
            {
                List<NNTPSegment> Segs = cC.Segments;

                if (Segs == null) { continue; }
                if (Segs.Count == 0) { continue; }

                vFiles.Add(new VirtualFile(cC.Name, Segs));
            }

            if (vFiles.Count == 0) { return null; }
            if (vToken.IsCancellationRequested) { return null; }

            VirtualSlot vSlot = new VirtualSlot(Name, vFiles, vWait);

            if (!zCol.Add(vSlot)) { return null; }

            return vSlot;
        }
コード例 #2
0
ファイル: API.cs プロジェクト: spotnet/fusenet
        internal static bool Slot(XmlWriter xR, VirtualSlot vSlot)
        {
            xR.WriteStartElement("slot");
            xR.WriteElementString("nzo_id", Convert.ToString(vSlot.ID));
            xR.WriteElementString("name", Common.CleanString(vSlot.Name));
            xR.WriteElementString("filename", Common.CleanString(vSlot.Name));
            xR.WriteElementString("status", Common.TranslateStatus((int)vSlot.Status));

            if (vSlot.Status == SlotStatus.Failed)
            {
                xR.WriteElementString("fail_message", Common.CleanString(vSlot.StatusLine));
            }

            if (!(vSlot.History))
            {
                NNTPInfo vInfo    = vSlot.Info;
                int      lSeconds = vInfo.SecondsLeft(vSlot.SpeedAverage, vSlot.TotalTime);

                string sLeft = "00:00:00";
                string ETA   = Common.FormatDate(DateTime.UtcNow);

                if (lSeconds > 0)
                {
                    sLeft = Common.FormatElapsed(new TimeSpan(0, 0, lSeconds));
                    ETA   = Common.FormatDate(DateTime.UtcNow.AddSeconds(lSeconds));
                }

                xR.WriteElementString("index", Convert.ToString(vSlot.Index));
                xR.WriteElementString("percentage", Convert.ToString(Math.Round(vInfo.Percentage, 0)));
                xR.WriteElementString("bytes", Convert.ToString(vInfo.Expected));
                xR.WriteElementString("kbpersec", String.Format(CultureInfo.InvariantCulture, "{0:0.00}", vSlot.Speed / (decimal)1000));
                xR.WriteElementString("mb", String.Format(CultureInfo.InvariantCulture, "{0:0.00}", Common.BytesToMegabytes(vInfo.Expected)));
                xR.WriteElementString("mbleft", String.Format(CultureInfo.InvariantCulture, "{0:0.00}", Common.BytesToMegabytes(vInfo.BytesLeft)));
                xR.WriteElementString("size", String.Format(CultureInfo.InvariantCulture, "{0:0.0}", Common.BytesToMegabytes(vInfo.Expected)) + " MB");
                xR.WriteElementString("eta", ETA);
                xR.WriteElementString("timeleft", sLeft);
                xR.WriteElementString("priority", "Normal");
            }
            else
            {
                if (vSlot.Status == SlotStatus.Failed)
                {
                }
                else
                {
                    //<slot>
                    //    <loaded>False</loaded>
                    //    <id>605</id>
                    //    <size>778.1 MB</size>
                    //    <pp>D</pp>
                    //    <completeness>0</completeness>
                    //    <nzb_name>Ubuntu.nzb</nzb_name>
                    //    <storage>X:\Apps\Ubuntu</storage>
                    //    <completed>1236646078</completed>
                    //    <downloaded>815878352</downloaded>
                    //    <report>00000000</report>
                    //    <path>\Ubuntu</path>
                    //    <bytes>815878352</bytes>
                    //</slot>
                }
            }

            xR.WriteEndElement();
            xR.Flush();

            return(true);
        }
コード例 #3
0
ファイル: API.cs プロジェクト: spotnet/fusenet
        //internal VirtualSlot Get(int ID) { lock (zServers) { return zServers.Slots.Item(ID); } }

        //public string Add(string Name, Stream NZB)
        //{
        //    // Asynchronous, returns the Slot ID

        //    CancellationTokenSource vToken = null;

        //    if (NZB == null) { return ""; }
        //    if (Name == null) { return ""; }
        //    if (Name.Length == 0) { return ""; }

        //    //string SID = Module.RandomString(6);

        //    Action aStart = (Action)(() => ImportNZB(Name, SID, zServers.Slots, NZB, vToken));
        //    Task zAdd = new Task(aStart, vToken.Token);
        //    zAdd.Start();

        //    return SID;
        //}

        //public string Add(string Name, List<string> Commands)
        //{
        //    // Asynchronous, returns the Slot ID

        //    int cI = 1;

        //    if (Commands == null) { return ""; }
        //    if (Commands.Count == 0) { return ""; }

        //    NNTPInput nI = new NNTPInput(Name);
        //    List<NNTPInput> cList = new List<NNTPInput>();

        //    foreach (string sC in Commands)
        //    {
        //        if (sC == null) { continue; }
        //        if (sC.Length == 0) { continue; }

        //        NNTPSegment nS = new NNTPSegment();

        //        nS.Index = cI;
        //        nS.Command = sC;
        //        nI.Segments.Add(nS);
        //    }

        //    if (nI.Segments.Count == 0) { return ""; }

        //    cList.Add(nI);

        //    //string SID = Module.RandomString(6);
        //    CancellationTokenSource vToken = new CancellationTokenSource();

        //    Action aStart = (Action)(() => InternalAdd(nI.Name, SID, zServers.Slots, cList, vToken.Token));
        //    Task zAdd = new Task(aStart, vToken.Token);
        //    zAdd.Start();

        //    return SID;
        //}

        public string Send(string Newsgroup, List <string> Command, CancellationTokenSource vToken = null)
        {
            // Synchronous, returns the last commands response

            if (Newsgroup == null)
            {
                throw new Exception("No group");
            }
            if (Command == null)
            {
                throw new Exception("No command");
            }
            if (zServers.Count == 0)
            {
                throw new Exception("No server");
            }

            List <string> tList = new List <string>();

            tList.Add("GROUP " + Newsgroup.ToLower());
            tList.AddRange(Command);

            NNTPSegment      nS    = new NNTPSegment();
            NNTPInput        nI    = new NNTPInput("");
            List <NNTPInput> cList = new List <NNTPInput>();

            nS.Index    = 1;
            nS.Commands = tList;

            nI.Segments.Add(nS);
            cList.Add(nI);

            ManualResetEventSlim wHandle;

            wHandle = new ManualResetEventSlim(false);

            if (vToken == null)
            {
                vToken = new CancellationTokenSource();
            }

            VirtualSlot vSlot = InternalAdd(nI.Name, zServers.Slots, cList, vToken.Token, wHandle);

            if (vSlot == null)
            {
                throw new Exception("No slot");
            }
            if (vToken.Token.IsCancellationRequested)
            {
                throw new Exception("Cancelled");
            }

            List <WaitHandle> wList = new List <WaitHandle>();

            wList.Add(vToken.Token.WaitHandle);

            if (vSlot.WaitHandle == null)
            {
                throw new Exception("No waithandle");
            }

            wList.Add(vSlot.WaitHandle.WaitHandle);

            vSlot.Status = SlotStatus.Downloading;
            Notify();

            WaitHandle wRet = Common.WaitList(wList);

            if ((wRet == null) || (wRet.Handle == vToken.Token.WaitHandle.Handle))
            {
                throw new Exception("Cancelled");
            }

            if (vToken.Token.IsCancellationRequested)
            {
                throw new Exception("Cancelled");
            }

            string zOut = null;

            if (vSlot.Status != SlotStatus.Completed)
            {
                if (vSlot.Status == SlotStatus.Failed)
                {
                    zOut = vSlot.StatusLine;
                }
                else
                {
                    zOut = Common.TranslateStatus((int)vSlot.Status);
                }

                int vId = vSlot.ID;
                vSlot = null;

                Remove(vId);

                if ((zOut == null) || (zOut.Length == 0))
                {
                    zOut = "Unknown";
                }

                throw new Exception(zOut);
            }
            else
            {
                foreach (VirtualFile vFile in vSlot.List())
                {
                    if (vFile.Output == null)
                    {
                        continue;
                    }
                    if (vFile.Output.Data == null)
                    {
                        continue;
                    }
                    if (vFile.Output.Data.Length == 0)
                    {
                        continue;
                    }

                    vFile.Output.Data.Position = 0;
                    zOut = Common.GetString(vFile.Output.Data);
                }

                int vId = vSlot.ID;
                vSlot = null;

                Remove(vId);
                return(zOut);
            }
        }