public void Discover_ServiceInstance()
        {
            var service = new ServiceProfile("y", "_sdtest-2._udp", 1024);
            var done    = new ManualResetEvent(false);
            var mdns    = new MulticastService();
            var sd      = new ServiceDiscovery(mdns);

            mdns.NetworkInterfaceDiscovered += (s, e) =>
            {
                sd.QueryServiceInstances(service.ServiceName);
            };

            sd.ServiceInstanceDiscovered += (s, e) =>
            {
                if (e.ServiceInstanceName == service.FullyQualifiedName)
                {
                    Assert.IsNotNull(e.Message);
                    done.Set();
                }
            };
            try
            {
                sd.Advertise(service);
                mdns.Start();
                Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "instance not found");
            }
            finally
            {
                sd.Dispose();
                mdns.Stop();
            }
        }
Esempio n. 2
0
        public void Discover_ServiceInstance_WithAnswersContainingAdditionRecords()
        {
            var service = new ServiceProfile("y", "_sdtest-2._udp", 1024, new[] { IPAddress.Parse("127.1.1.1") });
            var done    = new ManualResetEvent(false);

            using (var mdns = new MulticastService())
                using (var sd = new ServiceDiscovery(mdns)
                {
                    AnswersContainsAdditionalRecords = true
                })
                {
                    Message discovered = null;

                    mdns.NetworkInterfaceDiscovered += (s, e) =>
                    {
                        sd.QueryServiceInstances(service.ServiceName);
                    };

                    sd.ServiceInstanceDiscovered += (s, e) =>
                    {
                        if (e.ServiceInstanceName == service.FullyQualifiedName)
                        {
                            Assert.IsNotNull(e.Message);
                            discovered = e.Message;
                            done.Set();
                        }
                    };

                    sd.Advertise(service);

                    mdns.Start();

                    Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(3)), "instance not found");

                    var additionalRecordsCount =
                        1 + // SRVRecord
                        1 + // TXTRecord
                        1;  // AddressRecord

                    var answersCount = additionalRecordsCount +
                                       1; // PTRRecord

                    Assert.AreEqual(0, discovered.AdditionalRecords.Count);
                    Assert.AreEqual(answersCount, discovered.Answers.Count);
                }
        }
Esempio n. 3
0
        public void Discover_ServiceInstance_WithAnswersContainingAdditionRecords()
        {
            var service = new ServiceProfile("y", "_sdtest-2._udp", 1024);
            var done    = new ManualResetEvent(false);
            var mdns    = new MulticastService();
            var sd      = new ServiceDiscovery(mdns)
            {
                AnswersContainsAdditionalRecords = true
            };
            Message discovered = null;

            mdns.NetworkInterfaceDiscovered += (s, e) =>
            {
                sd.QueryServiceInstances(service.ServiceName);
            };

            sd.ServiceInstanceDiscovered += (s, e) =>
            {
                if (e.ServiceInstanceName == service.FullyQualifiedName)
                {
                    Assert.IsNotNull(e.Message);
                    discovered = e.Message;
                    done.Set();
                }
            };
            try
            {
                sd.Advertise(service);
                mdns.Start();
                Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "instance not found");
                Assert.AreEqual(0, discovered.AdditionalRecords.Count);
                Assert.IsTrue(discovered.Answers.Count > 1);
            }
            finally
            {
                sd.Dispose();
                mdns.Stop();
            }
        }