Esempio n. 1
0
 /// <summary>
 /// Creates protocol unit for posting article to server
 /// </summary>
 /// <param name="draftArticle"></param>
 /// <param name="server"></param>
 /// <param name="finishedMethod">can be null</param>
 public NntpPostArticleUnit(IResource draftArticle,
                            IResource server,
                            AsciiProtocolUnitDelegate finishedMethod,
                            bool invokedByUser)
 {
     _draftArticle  = draftArticle;
     _server        = new ServerResource(server);
     _invokedByUser = invokedByUser;
     _error         = "Failed to connect, posting abandoned.";
     if (finishedMethod != null)
     {
         Finished += finishedMethod;
     }
 }
Esempio n. 2
0
        public static void PostArticle(IResource draftArticle, AsciiProtocolUnitDelegate finishedMethod, bool invokedByUser)
        {
            Guard.NullArgument(draftArticle, "draftArticle");

            if (!Utils.IsNetworkConnectedLight())
            {
                return;
            }

            lock ( _articlesBeenPosted )
            {
                if (_articlesBeenPosted.Contains(draftArticle))
                {
                    return;
                }
                _articlesBeenPosted.Add(draftArticle);
            }
            IResourceList groups = draftArticle.GetLinksFrom(NntpPlugin._newsGroup, NntpPlugin._propTo);

            if (groups.Count > 0)
            {
                IResource server = new NewsgroupResource(groups[0]).Server;
                if (server != null)
                {
                    NntpConnection      postConnection = NntpConnectionPool.GetConnection(server, "foreground");
                    NntpPostArticleUnit postUnit       =
                        new NntpPostArticleUnit(draftArticle, server, finishedMethod, invokedByUser);
                    postUnit.Finished += postUnit_Finished;
                    postConnection.StartUnit(invokedByUser ? Int32.MaxValue - 2 : 0, postUnit);
                    return;
                }
            }
            ArticlePostedOrFailed(draftArticle);
            if (finishedMethod != null)
            {
                finishedMethod(null);
            }
        }
Esempio n. 3
0
        public static void DeliverNewsFromServer(IResource server,
                                                 IResource preferableGroup,
                                                 bool invokedByUser,
                                                 AsciiProtocolUnitDelegate finishedMethod)
        {
            Guard.NullArgument(server, "server");

            ServerResource serverResource = new ServerResource(server);

            if (Utils.IsNetworkConnectedLight())
            {
                NntpConnection connection = NntpConnectionPool.GetConnection(server, "background");
                IResourceList  groups     = serverResource.Groups;

                if (groups.Count > 0)
                {
                    /**
                     * at first deliver news from prefered group
                     */
                    if (preferableGroup != null && groups.Contains(preferableGroup))
                    {
                        connection.StartUnit(Int32.MaxValue - 2,
                                             new NntpDownloadHeadersUnit(new NewsgroupResource(preferableGroup), JobPriority.AboveNormal));
                        if (serverResource.DownloadBodiesOnDeliver)
                        {
                            connection.StartUnit(Int32.MaxValue - 3,
                                                 new NntpDeliverEmptyArticlesFromGroupsUnit(preferableGroup.ToResourceList(), null));
                        }
                    }

                    /**
                     * then deliver headers of other groups
                     */
                    NntpDeliverHeadersFromGroupsUnit deliverHeadersFromGroupsUnit =
                        new NntpDeliverHeadersFromGroupsUnit(groups, preferableGroup);
                    if (finishedMethod != null)
                    {
                        deliverHeadersFromGroupsUnit.Finished += finishedMethod;
                    }
                    int clientPriority = 0;
                    if (invokedByUser && preferableGroup != null && groups.Contains(preferableGroup))
                    {
                        clientPriority = Int32.MaxValue - 3;
                    }
                    connection.StartUnit(clientPriority, deliverHeadersFromGroupsUnit);

                    /**
                     * then download empty bodies
                     */
                    if (serverResource.DownloadBodiesOnDeliver)
                    {
                        NntpDeliverEmptyArticlesFromGroupsUnit deliverEmptyArticlesUnit =
                            new NntpDeliverEmptyArticlesFromGroupsUnit(groups, preferableGroup);
                        if (finishedMethod != null)
                        {
                            deliverEmptyArticlesUnit.Finished += finishedMethod;
                        }
                        connection.StartUnit(clientPriority, deliverEmptyArticlesUnit);
                    }
                }

                /**
                 * finally replicate new groups
                 */
                NntpDownloadGroupsUnit groupsUnit = new NntpDownloadGroupsUnit(server, false, JobPriority.Lowest);
                if (finishedMethod != null)
                {
                    groupsUnit.Finished += finishedMethod;
                }
                connection.StartUnit(0, groupsUnit);
            }

            /**
             * queue timed news delivering is necessary
             */
            int freq = serverResource.DeliverFreq;

            if (freq > 0)
            {
                Core.NetworkAP.QueueJobAt(
                    DateTime.Now.AddMinutes(freq), new ResourceDelegate(DeliverNewsFromServer), server);
            }
        }