예제 #1
0
파일: SsdpTests.cs 프로젝트: kthompson/Upnp
        public void AsyncSearchTest()
        {
            using (var client = new SsdpClient())
            using (var server = new SsdpServer())
            {
                server.CreateAnnouncer("urn:schemas-upnp-org:service:test:1", "uuid:979F4CE8-64AF-4653-B207-D7514908356F::urn:schemas-upnp-org:service:test:1");
                server.StartListening();

                client.StartListening();

                var search = client.CreateSearch(false);
                search.SearchType = "urn:schemas-upnp-org:service:test:1";
                search.ResultFound += (sender, e) =>
                {
                    lock (mutex)
                    {
                        if (IsTestAnnouncement(e.Value))
                            Monitor.Pulse(mutex);
                    }
                };

                lock (mutex)
                {
                    search.SearchAsync();

                    if (!Monitor.Wait(mutex, TimeSpan.FromSeconds(30)))
                    {
                        Assert.Fail("The announcement timed out.");
                    }
                }
            }
        }
예제 #2
0
        public UpnpServer(UpnpRoot root, SsdpServer ssdp = null, GenaServer gena = null)
        {
            this.Root = root;
            this.Root.ChildDeviceAdded += OnChildDeviceAdded;
            this.SsdpServer = ssdp ?? new SsdpServer();
            this.GenaServer = gena ?? new GenaServer();

            BuildAdvertisements();
        }
예제 #3
0
파일: Program.cs 프로젝트: automaters/Upnp
        static void Main(string[] args)
        {
            using (var server = new SsdpServer())
            {
                server.StartListening();

                var announcer = server.CreateAnnouncer();
                announcer.Location = "http://localhost/test";
                announcer.NotificationType = "test";
                announcer.MaxAge = 3;
                announcer.Start();

                SsdpClient client = new SsdpClient();
                using (var search = client.CreateSearch(true))
                {
                    search.SearchType = "upnp:rootdevice";

                    Console.WriteLine("Searching for first result:");
                    SsdpMessage first = search.FindFirst(Protocol.DiscoveryEndpoints.IPv4, Protocol.DiscoveryEndpoints.Broadcast);
                    if (first == null)
                        Console.WriteLine("No results found from FindFirst.");
                    else
                        Console.WriteLine("First device found at: {0}", first.Location);
                }

                using (var search = client.CreateSearch(true))
                {
                    search.SearchType = Protocol.SsdpAll;

                    // Attach our events for async search
                    search.ResultFound += new EventHandler<EventArgs<SsdpMessage>>(search_ResultFound);
                    search.SearchComplete += new EventHandler(search_SearchComplete);

                    Console.WriteLine();
                    Console.WriteLine("Performing asynchronous search:");
                    //search.SearchAsync();
                    search.SearchAsync(Protocol.DiscoveryEndpoints.IPv4, Protocol.DiscoveryEndpoints.Broadcast);

                    // Wait for our async search to complete before doing the synchronous search
                    search.WaitForSearch();
                }

                using (var search = client.CreateSearch(true))
                {
                    search.SearchType = "upnp:rootdevice";

                    Console.WriteLine();
                    Console.WriteLine("Performing synchronous search:");
                    search.Search(Protocol.DiscoveryEndpoints.IPv4, Protocol.DiscoveryEndpoints.Broadcast).ForEach(msg =>
                    {
                        Console.WriteLine(msg.Location);
                    });
                }

                Console.ReadLine();
            }
        }
예제 #4
0
        public void AnnoucementTest()
        {
            using (var client = new SsdpClient())
            using (var server = new SsdpServer())
            {
                client.ServiceFound += TestResultFound;
                client.StartListening();

                lock (mutex)
                {
                    server.CreateAnnouncer("urn:schemas-upnp-org:service:test:1", "uuid:979F4CE8-64AF-4653-B207-D7514908356F::urn:schemas-upnp-org:service:test:1");
                    server.StartListening();

                    if (!Monitor.Wait(mutex, TimeSpan.FromSeconds(30)))
                    {
                        Assert.Fail("The announcement timed out.");
                    }
                }
            }
        }
예제 #5
0
        public void FilteredAnnouncementTest2()
        {
            using (var client = new SsdpClient())
            using (var server = new SsdpServer())
            {
                client.Filter = message => message.Type == "urn:schemas-upnp-org:service:test:1";
                client.StartListening();

                client.ServiceFound += (sender, args) =>
                {
                    lock (mutex)
                    {
                        if (args.Value.Type != "urn:schemas-upnp-org:service:test:1")
                        {
                            Monitor.Pulse(mutex);
                        }
                    }
                };

                lock (mutex)
                {
                    server.CreateAnnouncer("upnp:test:fail", "uuid:upnp-tests:test1");
                    server.CreateAnnouncer("upnp", "uuid:upnp-tests:test2");
                    server.CreateAnnouncer("urn:schemas-upnp-org:service:test:1", "uuid:979F4CE8-64AF-4653-B207-D7514908356F::urn:schemas-upnp-org:service:test:1");
                    server.StartListening();

                    if (Monitor.Wait(mutex, TimeSpan.FromSeconds(10)))
                    {
                        Assert.Fail("The client recieved invalid announcements.");
                    }
                }
            }
        }
예제 #6
0
        public void SynchronousAnnouncementTest()
        {
            using (var client = new SsdpClient())
            using (var server = new SsdpServer())
            {
                client.Filter = message => message.Type == "urn:schemas-upnp-org:service:test:1";
                client.StartListening();

                server.CreateAnnouncer("urn:schemas-upnp-org:service:test:1", "uuid:979F4CE8-64AF-4653-B207-D7514908356F::urn:schemas-upnp-org:service:test:1");
                server.StartListening();

                var result = client.FindFirst(TimeSpan.FromSeconds(30));
                Assert.IsNotNull(result);
                Assert.That(IsTestAnnouncement(result));
            }
        }
예제 #7
0
        public void SearchTest()
        {
            using (var client = new SsdpClient())
            using (var server = new SsdpServer())
            {
                server.CreateAnnouncer("urn:schemas-upnp-org:service:test:1", "uuid:979F4CE8-64AF-4653-B207-D7514908356F::urn:schemas-upnp-org:service:test:1");
                server.StartListening();

                client.StartListening();

                var search = client.CreateSearch(false);
                var results = search.Search();
                Assert.That(results.Exists(IsTestAnnouncement));
            }
        }
예제 #8
0
        public void RootDeviceSearchTest()
        {
            using (var client = new SsdpClient())
            using (var server = new SsdpServer())
            {
                server.CreateAnnouncer("upnp:rootdevice", "uuid:979F4CE8-64AF-4653-B207-D7514908356F::upnp:rootdevice");
                server.StartListening();
                client.StartListening();

                var search = client.CreateSearch(false);
                search.SearchType = "upnp:rootdevice";
                var results = search.Search();
                Assert.AreEqual(0, results.Count(msg => msg.Type != "upnp:rootdevice"));
                Assert.That(results.Exists(IsRootAnnouncement));
            }
        }
예제 #9
0
        public void RootDeviceAsyncSearchTest()
        {
            using (var client = new SsdpClient())
            using (var server = new SsdpServer())
            {
                server.CreateAnnouncer("upnp:rootdevice", "uuid:979F4CE8-64AF-4653-B207-D7514908356F::upnp:rootdevice");
                server.StartListening();

                client.StartListening();

                var search = client.CreateSearch(false);
                search.SearchType = "upnp:rootdevice";
                search.ResultFound += RootResultFound;

                lock (mutex)
                {
                    search.SearchAsync();

                    if (!Monitor.Wait(mutex, TimeSpan.FromSeconds(30)))
                    {
                        Assert.Fail("The announcement timed out.");
                    }
                }
            }
        }