Esempio n. 1
0
        public Article GetArticleBy(string title, bool checkPeers)
        {
            OnMethodCalled(this, new MethodCallEventArgs { Name = "GetArticle",Id=this.Id });

            if (title == null)
            {
                throw new ArgumentNullException();
            }

            title = title.ToLower();

            Article article = Articles.Find(a => a.Title.ToLower().Equals(title));

            if (!article.IsDefault())
                return article;

            foreach (KeyValuePair<int, IPeer> p in OnlinePeers)
            {
                try
                {
                    article = p.Value.GetArticleBy(title, false);

                    if (!article.IsDefault())
                        return article;
                }
                catch (WebException)
                {
                    OnlinePeers.Remove(p.Key);
                }
            }

            if (!checkPeers)
                return default(Article);

            IPeerListCtx plc = new PeerListCtx();
            IPeerRequestContext ctx = new PeerRequestContext(plc);
            ctx.CheckAndAdd(this.Id);//so faz add pois e o inicio da chain de getpeers

            IList<KeyValuePair<int, IPeer>> peers = new List<KeyValuePair<int, IPeer>>();

            // KeyValuePair para se poder remover caso o proxy tenha sido fechado
            foreach (KeyValuePair<int, ISuperPeer> sp in SuperPeers)
            {
                try
                {
                    if(ctx.CheckAndAdd(sp.Key))
                        PeerHelpers.ConcatAndReturnDif(ref peers, OnlinePeers, sp.Value.GetPeers(ctx),this);
                }
                catch (WebException)
                {
                    SuperPeers.Remove(sp.Key);
                }

                if (peers != null)
                {
                    foreach (KeyValuePair<int, IPeer> p in peers)
                    {
                        try
                        {
                            article = p.Value.GetArticleBy(title, false);

                            if (!article.IsDefault())
                                return article;
                        }
                        catch (WebException)
                        {
                            OnlinePeers.Remove(p.Key);
                        }
                    }
                }
            }

            return default(Article);
        }
Esempio n. 2
0
        public Article GetArticleBy(string title, bool checkPeers)
        {
            Console.WriteLine(Id + " - Getting article - " + title);
            if (string.IsNullOrEmpty(title))
            {
                throw new EmptyTitleException();
            }

            if (SuperPeer == null || !SuperPeer.IsAlive())
            {
                throw new NotRegisteredToSuperPeerException();
            }

            title = title.ToLower();

            Article article = Articles.Find(a => a.Title.ToLower().Equals(title));

            if (!article.IsDefault())
                return article;

            foreach (KeyValuePair<int, IPeer> p in OnlinePeers)
            {
                try
                {
                    article = p.Value.GetArticleBy(title, false);

                    if (!article.IsDefault())
                        return article;
                }
                catch (WebException)
                {
                    OnlinePeers.Remove(p.Key);
                }
            }

            if (!checkPeers)
                return default(Article);

            IList<KeyValuePair<int, IPeer>> peers = new List<KeyValuePair<int,IPeer>>();

            try
            {
                IPeerListCtx plc = new PeerListCtx();
                IPeerRequestContext ctx = new PeerRequestContext(plc);
                ctx.CheckAndAdd(SuperPeer.Id);//so faz add pois e o inicio da chain de getpeers

                PeerHelpers.ConcatAndReturnDif(ref peers, OnlinePeers, SuperPeer.GetPeers(ctx),this);
            }
            catch (WebException)
            {
                throw new NotRegisteredToSuperPeerException();
            }

            if (peers != null)
            {
                foreach (KeyValuePair<int, IPeer> p in peers)
                {
                    try
                    {
                        article = p.Value.GetArticleBy(title, false);

                        if (!article.IsDefault())
                            return article;
                    }
                    catch (WebException)
                    {
                        OnlinePeers.Remove(p.Key);
                    }
                }
            }

            return default(Article);
        }