示例#1
0
        public void Get_NotExpired()
        {
            using (var router = new ContentRouter())
            {
                router.Add(cid1, self.Id, DateTime.MinValue);
                var providers = router.Get(cid1);
                Assert.AreEqual(0, providers.Count());

                router.Add(cid1, self.Id, DateTime.MaxValue - router.ProviderTTL);
                providers = router.Get(cid1);
                Assert.AreEqual(1, providers.Count());
            }
        }
示例#2
0
 public void Get_NonexistentCid()
 {
     using (var router = new ContentRouter())
     {
         var providers = router.Get(cid1);
         Assert.AreEqual(0, providers.Count());
     }
 }
示例#3
0
        /// <inheritdoc />
        public async Task <IEnumerable <Peer> > FindProvidersAsync(
            Cid id,
            int limit                = 20,
            Action <Peer> action     = null,
            CancellationToken cancel = default(CancellationToken))
        {
            var dquery = new DistributedQuery <Peer>
            {
                QueryType     = MessageType.GetProviders,
                QueryKey      = id.Hash,
                Dht           = this,
                AnswersNeeded = limit,
            };

            if (action != null)
            {
                dquery.AnswerObtained += (s, e) => action.Invoke(e);
            }

            // Add any providers that we already know about.
            var providers = ContentRouter
                            .Get(id)
                            .Select(pid =>
            {
                return((pid == Swarm.LocalPeer.Id)
                        ? Swarm.LocalPeer
                        : Swarm.RegisterPeer(new Peer {
                    Id = pid
                }));
            });

            foreach (var provider in providers)
            {
                dquery.AddAnswer(provider);
            }

            // Ask our peers for more providers.
            if (limit > dquery.Answers.Count())
            {
                await dquery.RunAsync(cancel).ConfigureAwait(false);
            }

            return(dquery.Answers.Take(limit));
        }
示例#4
0
        public void Add()
        {
            using (var router = new ContentRouter())
            {
                router.Add(cid1, self.Id);

                var providers = router.Get(cid1);
                Assert.AreEqual(1, providers.Count());
                Assert.AreEqual(self.Id, providers.First());
            }
        }
示例#5
0
        public void Add_MultipleProviders()
        {
            using (var router = new ContentRouter())
            {
                router.Add(cid1, self.Id);
                router.Add(cid1, other.Id);

                var providers = router.Get(cid1).ToArray();
                Assert.AreEqual(2, providers.Length);
                CollectionAssert.Contains(providers, self.Id);
                CollectionAssert.Contains(providers, other.Id);
            }
        }