Exemplo n.º 1
0
 internal Announcer (Server server, string name, string type, string location)
 {
     this.server = server;
     Name = name;
     Type = type;
     Location = location;
 }
Exemplo n.º 2
0
        protected internal virtual void CreateServer()
        {
            var b = new Mono.Ssdp.Client();
            //var found = b.Browse(string.Format("upnp:{0}", Name));
            var found = b.BrowseAll();

            found.Client.ServiceAdded += HandleServiceAdded;
            found.Start();

            _server = new Mono.Ssdp.Server();
            _server.Announce(string.Format("upnp:{0}", Name), string.Format("uuid:RemoteMonitor-device:{0}", Name), Address.ToString());
        }
Exemplo n.º 3
0
 public void InitialUnicastEventTest ()
 {
     var eventer = new DummyStateVariableEventer ();
     var root = CreateRoot (CreateServiceController (new StateVariable ("Foo", "string", new StateVariableOptions { Eventer = eventer })));
     eventer.SetValue ("foo");
     
     using (var server = new Server (root)) {
         server.Start ();
         var prefix = GeneratePrefix ();
         using (var listener = new HttpListener ()) {
             listener.Prefixes.Add (prefix);
             listener.Start ();
             Exception exception = null;
             listener.BeginGetContext (result => {
                 try {
                     var context = listener.EndGetContext (result);
                     using (var reader = new StreamReader (context.Request.InputStream)) {
                         Assert.AreEqual (Xml.SingleEventReport, reader.ReadToEnd ());
                     }
                     context.Response.Close ();
                 } catch (Exception e) {
                     exception = e;
                 }
                 lock (mutex) {
                     Monitor.Pulse (mutex);
                 }
             }, null);
             
             Subscribe (root, prefix);
             
             if (exception != null) {
                 throw exception;
             }
         }
     }
 }
Exemplo n.º 4
0
 public void AnnouncementTest()
 {
     using (var server = new Server (CreateRoot ())) {
         using (var client = new Mono.Ssdp.Client ()) {
             var announcements = new Dictionary<string,string> ();
             announcements.Add ("upnp:rootdevice/uuid:d1::upnp:rootdevice", null);
             announcements.Add ("uuid:d1/uuid:d1", null);
             announcements.Add ("urn:schemas-upnp-org:device:mono-upnp-tests-device:1/uuid:d1::urn:schemas-upnp-org:device:mono-upnp-tests-device:1", null);
             announcements.Add ("uuid:ed1/uuid:ed1", null);
             announcements.Add ("urn:schemas-upnp-org:device:mono-upnp-tests-embedded-device:1/uuid:ed1::urn:schemas-upnp-org:device:mono-upnp-tests-embedded-device:1", null);
             announcements.Add ("urn:schemas-upnp-org:service:mono-upnp-test-service:1/uuid:d1::urn:schemas-upnp-org:service:mono-upnp-test-service:1", null);
             announcements.Add ("urn:schemas-upnp-org:service:mono-upnp-test-service:2/uuid:d1::urn:schemas-upnp-org:service:mono-upnp-test-service:2", null);
             announcements.Add ("urn:schemas-upnp-org:service:mono-upnp-test-service:1/uuid:ed1::urn:schemas-upnp-org:service:mono-upnp-test-service:1", null);
             announcements.Add ("urn:schemas-upnp-org:service:mono-upnp-test-service:2/uuid:ed1::urn:schemas-upnp-org:service:mono-upnp-test-service:2", null);
             client.ServiceAdded += (obj, args) => {
                 lock (mutex) {
                     Assert.AreEqual (ServiceOperation.Added, args.Operation);
                     var announcement = string.Format ("{0}/{1}", args.Service.ServiceType, args.Service.Usn);
                     if (announcements.ContainsKey (announcement)) {
                         announcements.Remove (announcement);
                     }
                     if (announcements.Count == 0) {
                         Monitor.Pulse (mutex);
                     }
                 }
             };
             client.BrowseAll ();
             lock (mutex) {
                 server.Start ();
                 if (!Monitor.Wait (mutex, TimeSpan.FromSeconds (30))) {
                     Assert.Fail ("The UPnP server announcement timed out.");
                 }
             }
         }
     }
 }
Exemplo n.º 5
0
 public void IconTest ()
 {
     var root = CreateRoot (
         new[] {
             new Icon (100, 100, 32, "image/jpeg", new byte[] { 0 }),
             new Icon (100, 100, 32, "image/png", new byte[] { 1 })
         },
         new[] {
             new Icon (100, 100, 32, "image/jpeg", new byte[] { 2 }),
             new Icon (100, 100, 32, "image/png", new byte[] { 3 })
         }
     );
     using (var server = new Server (root)) {
         server.Start ();
         var url = new Uri (root.UrlBase, "/icon/");
         AssertEquality (url, 0, 0);
         AssertEquality (url, 1, 1);
         url = new Uri (root.UrlBase, "/device/0/icon/");
         AssertEquality (url, 0, 2);
         AssertEquality (url, 1, 3);
     }
 }
Exemplo n.º 6
0
 public void ScpdTest ()
 {
     var controller = CreateServiceController ();
     var root = new DummyRoot (
         new DeviceType ("schemas-upnp-org", "mono-upnp-tests-device", new Version (1, 0)),
         "uuid:d1",
         "Mono.Upnp.Tests Device",
         "Mono Project",
         "Device",
         new DeviceOptions {
             Services = new[] {
                 new Service (
                     new ServiceType ("uschemas-upnp-org", "mono-upnp-test-service", new Version (1, 0)),
                     "urn:upnp-org:serviceId:testService1",
                     controller
                 )
             }
         }
     );
     using (var server = new Server (root)) {
         server.Start ();
         var request = WebRequest.Create (new Uri (root.UrlBase, "/service/0/scpd/"));
         using (var response = (HttpWebResponse)request.GetResponse ()) {
             Assert.AreEqual (HttpStatusCode.OK, response.StatusCode);
             using (var reader = XmlReader.Create (response.GetResponseStream ())) {
                 var target_controller = deserializer.DeserializeServiceController (reader);
                 ServiceDescriptionTests.AssertEquality (controller, target_controller);
             }
         }
     }
 }
Exemplo n.º 7
0
 public void DescriptionTest ()
 {
     var root = CreateRoot ();
     using (var server = new Server (root)) {
         server.Start ();
         var request = WebRequest.Create (root.UrlBase);
         using (var response = (HttpWebResponse)request.GetResponse ()) {
             Assert.AreEqual (HttpStatusCode.OK, response.StatusCode);
             using (var reader = XmlReader.Create (response.GetResponseStream ())) {
                 var target_root = deserializer.DeserializeRoot (reader, root.UrlBase);
                 DeviceDescriptionTests.AssertEquality (root, target_root);
             }
         }
     }
 }
Exemplo n.º 8
0
 public void ControlTest ()
 {
     var root = CreateRoot (CreateServiceController ());
     
     using (var server = new Server (root)) {
         server.Start ();
         var request = (HttpWebRequest)WebRequest.Create (new Uri (root.UrlBase, "/service/0/control/"));
         request.Method = "POST";
         request.Headers.Add ("SOAPACTION", "urn:schemas-upnp-org:service:mono-upnp-test-service:1#Foo");
         request.ContentType = @"text/xml; charset=""utf-8""";
         var bytes = System.Text.Encoding.UTF8.GetBytes (Xml.SimpleSoapRequest);
         using (var stream = request.GetRequestStream ()) {
             stream.Write (bytes, 0, bytes.Length);
         }
         using (var response = (HttpWebResponse)request.GetResponse ()) {
             Assert.AreEqual (HttpStatusCode.OK, response.StatusCode);
             using (var stream = response.GetResponseStream ()) {
                 using (var reader = XmlReader.Create (stream)) {
                     reader.ReadToFollowing ("result");
                     Assert.AreEqual ("You said hello world!", reader.ReadElementContentAsString ());
                 }
             }
         }
     }
 }
Exemplo n.º 9
0
 public void UnsubscribeUnicastEventTest ()
 {
     string sid = null;
     var eventer = new DummyStateVariableEventer ();
     var root = CreateRoot (CreateServiceController (new StateVariable ("Foo", "string", new StateVariableOptions { Eventer = eventer })));
     eventer.SetValue ("foo");
     
     using (var server = new Server (root)) {
         server.Start ();
         var prefix = GeneratePrefix ();
         var url = new Uri (root.UrlBase, "/service/0/event/");
         using (var listener = new HttpListener ()) {
             listener.Prefixes.Add (prefix);
             listener.Start ();
             Exception exception = null;
             listener.BeginGetContext (result => {
                 lock (mutex) {
                     try {
                         var context = listener.EndGetContext (result);
                         using (var reader = new StreamReader (context.Request.InputStream)) {
                             Assert.AreEqual (Xml.SingleEventReport, reader.ReadToEnd ());
                         }
                         context.Response.Close ();
                         var unsub_request = WebRequest.Create (url);
                         unsub_request.Method = "UNSUBSCRIBE";
                         unsub_request.Headers.Add ("SID", sid);
                         using (var response = (HttpWebResponse)unsub_request.GetResponse ()) {
                             Assert.AreEqual (HttpStatusCode.OK, response.StatusCode);
                         }
                         listener.BeginGetContext (r => {
                             lock (mutex) {
                                 Monitor.Pulse (mutex);
                             }
                         }, null);
                         eventer.SetValue ("foo");
                     } catch (Exception e) {
                         exception = e;
                         Monitor.Pulse (mutex);
                     }
                 }
             }, null);
             var request = WebRequest.Create (url);
             request.Method = "SUBSCRIBE";
             request.Headers.Add ("CALLBACK", string.Format ("<{0}>", prefix));
             request.Headers.Add ("NT", "upnp:event");
             lock (mutex) {
                 using (var response = (HttpWebResponse)request.GetResponse ()) {
                     Assert.AreEqual (HttpStatusCode.OK, response.StatusCode);
                     Assert.IsNotNull (response.Headers["SID"]);
                     sid = response.Headers["SID"];
                 }
                 if (Monitor.Wait (mutex, TimeSpan.FromSeconds (10))) {
                     Assert.Fail ("The event server sent updates to an unsubscribed client.");
                 }
             }
             
             if (exception != null) {
                 throw exception;
             }
         }
     }
 }