Beispiel #1
0
 def _on_device_available(self, event, upnp_device):
     self._update_message_env(upnp_device)
     self._send_device_messages(upnp_device, "available")
     # Service announcements
     for service in upnp_device.services:
         self._send_service_message(upnp_device, service, "available")
     # Handle repeats
     if getattr(event, 'times_sent', 0) < 3:
         self._timers[upnp_device.uuid] \
             = Timer(0.25, event, *event.channels).register(self)
         event.times_sent = getattr(event, 'times_sent', 0) + 1
     else:
         self._timers[upnp_device.uuid] \
             = Timer(self._message_expiry / 4,
                     event, *event.channels).register(self)
Beispiel #2
0
    def _send_request(self, method, url, body=None, headers={}, timeout=None):
        p = urlparse(url)
        if p.hostname and p.hostname != self._host \
            or p.scheme == "http" and self._secure \
            or p.scheme == "https" and not self._secure \
            or p.port and p.port != self._port:
            self.fire(NotConnected())
            return

        resource = p.path
        if p.query:
            resource += "?" + p.query
        headers = Headers([(k, v) for k, v in list(headers.items())])
        # Clients MUST include Host header in HTTP/1.1 requests (RFC 2616)
        if "Host" not in headers:
            headers["Host"] = self._host \
                + (":" + str(self._port)) if self._port else ""
        command = "%s %s HTTP/1.1" % (method, resource)
        if body is not None:
            headers["Content-Length"] = len(body)
        message = "%s\r\n%s" % (command, headers)
        self._outstanding += 1
        if timeout is not None:
            self._timer = Timer(timeout, SocketError(ETIMEDOUT), self.channel) \
                .register(self)
        self.fire(write(message.encode('utf-8')), self._transport)
        if body is not None:
            self.fire(write(body), self._transport)
Beispiel #3
0
 def _on_search_request(self, event, search_target=UPNP_ROOTDEVICE, mx=1):
     self._send_template("m-search-request", 
                         { "ST": search_target, "MX": mx })
     # Handle repeats
     if getattr(event, 'times_sent', 0) < 3:
         Timer(mx, event, *event.channels).register(self)
         event.times_sent = getattr(event, 'times_sent', 0) + 1
Beispiel #4
0
 def test_datetime(self):
     now = datetime.now()
     d = now + timedelta(seconds=0.1)
     timer = Timer(d, test(), "timer")
     timer.register(self.app)
     wait_for(self.app, "flag")
     self.app.reset()
Beispiel #5
0
 def flushChanges(self):
     if self._updates_locked or len(self._changes) == 0:
         return
     self._updates_locked = True
     self.fire(Notification({"LastChange": self.LastChange()}),
               self.notification_channel)
     self._changes.clear()
     Timer(0.2, Event.create("UnlockUpdates"), self).register(self)
Beispiel #6
0
    def test_persistent(self):
        timer = Timer(0.1, test(), "timer", persist=True)
        timer.register(self.app)

        for _ in range(2):
            wait_for(self.app, "flag")
            self.app.reset()

        timer.unregister()
 def _on_off_changed(self, value, session=None, **kwargs):
     if value and self._timer is None:
         evt = Event.create("time_over", session)
         evt.channels = (self.channel, )
         self._timer = Timer(1, evt, persist=True).register(self)
         locales = kwargs.get("locales", [])
         self.fire(portal_message \
                   (session, self.translation(locales) \
                    .ugettext("TimeUpdateOn")), self._portal_channel)
     if not value and self._timer is not None:
         self._timer.unregister()
         self._timer = None
Beispiel #8
0
 def _on_provider_updated_handler(self, provider, changed):
     if "state" in changed:
         state = changed["state"]
         if state == "PLAYING":
             self.current_track_duration = 60
             if self._timer:
                 self._timer.unregister()
             self._timer = Timer(self.current_track_duration, 
                                 MediaPlayer.EndOfMedia()).register(self)
         elif state == "IDLE":
             if self._timer:
                 self._timer.unregister()
Beispiel #9
0
 def _on_play(self):
     if self.source is None or self.state == "PLAYING":
         return
     if self._timer:
         self._timer.unregister()
     from cocy.upnp import DIDL_LITE_NS
     duration = self.source_meta_dom.find(
         str(QName(DIDL_LITE_NS, "item")) + "/" + str(QName(DIDL_LITE_NS, "res"))) \
         .get("duration")
     self.current_track_duration = duration_to_secs(duration)
     self._timer = Timer(self.current_track_duration,
                         end_reached()).register(self)
     self.state = "PLAYING"
Beispiel #10
0
 def _on_provider_updated_handler(self, provider, changed):
     if "source" in changed:
         if self.state == "PLAYING":
             if self._timer:
                 self._timer.unregister()
             from cocy.upnp import DIDL_LITE_NS
             duration = self.source_meta_dom.find(
                 str(QName(DIDL_LITE_NS, "item")) + "/" + str(QName(DIDL_LITE_NS, "res"))) \
                 .get("duration")
             self.current_track_duration = duration_to_secs(duration)
             self._timer = Timer(self.current_track_duration,
                                 end_reached()).register(self)
     if "state" in changed:
         state = changed["state"]
         if state == "IDLE":
             if self._timer:
                 self._timer.unregister()
Beispiel #11
0
 def __init__(self, location, max_age, usn):
     super(UPnPRootDevice, self).__init__(channel=usn)
     self._location = location
     self._usn = usn
     self._ready = False
     self._comm_chan = "client." + usn
     self._client = Client(location, channel=self._comm_chan).register(self)
     @handler("response", channel=self._comm_chan)
     def _on_response(self, response):
         if response.status == http_client.OK:
             self._initialize(response.read())
     self.addHandler(_on_response)
     @handler("error", channel=self._comm_chan)
     def _on_error(self, *args, **kwargs):
         self._client.close()
         self.unregister()
     self.addHandler(_on_error)
     self.fire(Request("GET", self._location), self._client)
     self._expiry_timer \
         = Timer(max_age, UPnPDeviceByeBye(usn)).register(self)
Beispiel #12
0
 def _on_seek(self, position):
     if self.state != "PLAYING":
         return
     self._timer.unregister()
     self._timer = Timer(self.current_track_duration - position,
                         end_reached()).register(self)
Beispiel #13
0
 def test_basic(self):
     timer = Timer(0.1, test(), "timer")
     timer.register(self.app)
     wait_for(self.app, "flag")
     self.app.reset()