Ejemplo n.º 1
0
    def init_host(self, device, service_id, configid = None):
        """
Initializes a host service.

:param device: Host device this UPnP service is added to
:param service_id: Unique UPnP service ID
:param configid: UPnP configId for the host device

:return: (bool) Returns true if initialization was successful.
:since:  v0.2.00
        """

        self.configid = configid
        self.host_service = True
        self.service_id = service_id
        self.udn = device.get_udn()

        self.url_base = "{0}{1}/".format(device.get_url_base(), Link.encode_query_value(service_id))
        self.url_control = "{0}control".format(self.url_base)
        self.url_event_control = "{0}eventsub".format(self.url_base)
        self.url_scpd = "{0}xml".format(self.url_base)

        self._init_host_actions(device)
        self._init_host_variables(device)

        Hook.call("dNG.pas.upnp.Service.initHost", device = device, service = self)
        Hook.register_weakref("dNG.pas.upnp.Gena.onRegistered", self._on_gena_registration)

        return ((len(self.actions) + len(self.variables)) > 0)
Ejemplo n.º 2
0
    def stop(self):
        """
Stops the listener and unqueues all running sockets.

:since: v0.2.00
        """

        # pylint: disable=broad-except

        self._lock.acquire()

        if (self.active):
            if (self.log_handler is not None): self.log_handler.debug("#echo(__FILEPATH__)# -{0!r}.stop()- (#echo(__LINE__)#)", self, context = "pas_server")

            self.active = False

            if (self.stopping_hook is not None and len(self.stopping_hook) > 0): Hook.unregister(self.stopping_hook, self.thread_stop)
            self.stopping_hook = ""

            self._lock.release()

            try: self.close()
            except Exception: pass

            self._active_unqueue_all()
        else: self._lock.release()
Ejemplo n.º 3
0
    def _init_content(self):
        """
Initializes the content of a container.

:return: (bool) True if successful
:since:  v0.2.00
        """

        if (self.log_handler is not None): self.log_handler.debug("#echo(__FILEPATH__)# -{0!r}._init_content()- (#echo(__LINE__)#)", self, context = "pas_upnp")
        _return = False

        if (self.content is None):
            with self._lock:
                # Thread safety
                _return = Abstract._init_content(self)

                if (not _return):
                    if (self.resource_id == "0"):
                        Hook.call("dNG.pas.upnp.Resource.getRootResourceClientContent", container = self)
                        if (len(self.content) == 0): Hook.call("dNG.pas.upnp.Resource.getRootResourceContent", container = self)

                        _return = True
                    #
                #
            #
        #

        return _return
Ejemplo n.º 4
0
    def run(self):
        """
Worker loop

:since: v0.2.00
        """

        # pylint: disable=broad-except

        mainloop = None

        with GlibThread._lock:
            if (self.mainloop is None):
                mainloop = GLib.MainLoop()
                self.mainloop = mainloop
            #
        #

        if (mainloop is not None):
            if (self.log_handler is not None): self.log_handler.debug("#echo(__FILEPATH__)# -{0!r}.run()- (#echo(__LINE__)#)", self, context = "pas_gapi_core")

            try: mainloop.run()
            except Exception as handled_exception: LogLine.error(handled_exception, context = "pas_gapi_core")
            except KeyboardInterrupt: Hook.call("dNG.pas.Status.stop")
            finally: self.stop()
Ejemplo n.º 5
0
    def stop(self, params = None, last_return = None):
        """
Stop the running GLib based main loop.

:param params: Parameter specified
:param last_return: The return value from the last hook called.

:since: v0.2.00
        """

        if (self.mainloop is not None):
            # Thread safety
            with GlibThread._lock:
                if (self.mainloop is not None):
                    if (self.log_handler is not None): self.log_handler.debug("#echo(__FILEPATH__)# -{0!r}.stop()- (#echo(__LINE__)#)", self, context = "pas_gapi_core")

                    if (self.mainloop.is_running()):
                        with ExceptionLogTrap("pas_gapi_core"): self.mainloop.quit()
                    #

                    self.mainloop = None

                    Hook.unregister("dNG.pas.Status.onShutdown", self.stop)
                #
            #
        #

        return last_return
Ejemplo n.º 6
0
def delete(params, last_return=None):
    """
Called for "dNG.pas.user.Profile.delete"

:param params: Parameter specified
:param last_return: The return value from the last hook called.

:return: (mixed) Return value
:since:  v0.2.00
    """

    # pylint: disable=star-args

    if ("username" not in params):
        raise ValueException("Missing required argument")
    else:
        user_profile_class = NamedLoader.get_class("dNG.data.user.Profile")

        try:
            user_profile = user_profile_class.load_username(params['username'])

            Hook.call("dNG.pas.user.Profile.onDelete",
                      user_profile_id=user_profile.get_id())
            user_profile.delete()
        except NothingMatchedException:
            pass
    #

    return last_return
Ejemplo n.º 7
0
def delete(params, last_return = None):
    """
Called for "dNG.pas.user.Profile.delete"

:param params: Parameter specified
:param last_return: The return value from the last hook called.

:return: (mixed) Return value
:since:  v0.2.00
    """

    # pylint: disable=star-args

    if ("username" not in params): raise ValueException("Missing required argument")
    else:
        user_profile_class = NamedLoader.get_class("dNG.data.user.Profile")

        try:
            user_profile = user_profile_class.load_username(params['username'])

            Hook.call("dNG.pas.user.Profile.onDelete", user_profile_id = user_profile.get_id())
            user_profile.delete()
        except NothingMatchedException: pass
    #

    return last_return
Ejemplo n.º 8
0
    def execute_unsubscribe(self):
        """
Action for "request"

:since: v0.2.00
        """

        self.response.init()
        self.response.set_header("Date", RfcBasics.get_rfc5322_datetime(time()))

        if (not isinstance(self.request, HttpUpnpRequest)): raise UpnpException("pas_http_core_400")
        upnp_service = self.request.get_upnp_service()
        if (not isinstance(upnp_service, AbstractService)): raise UpnpException("pas_http_core_400", 401)

        Hook.call("dNG.pas.http.l10n.upnp.Events.init")

        gena_sid = self.request.get_header("SID")
        upnp_service.set_client_settings(self.get_client_settings())

        if (gena_sid is None): raise UpnpException("pas_http_core_400", 400)

        gena = Gena.get_instance()
        usn = upnp_service.get_usn()

        if (not gena.deregister(usn, gena_sid)): raise UpnpException("pas_http_core_404", 412)

        self.response.set_raw_data("")
Ejemplo n.º 9
0
    def _init_content(self):
        """
Initializes the content of a container.

:return: (bool) True if successful
:since:  v0.2.00
        """

        if (self.log_handler is not None): self.log_handler.debug("#echo(__FILEPATH__)# -{0!r}._init_content()- (#echo(__LINE__)#)", self, context = "pas_upnp")
        _return = False

        if (self.content is None):
            with self._lock:
                # Thread safety
                Resource._init_content(self)
                _type = self.get_type()

                if (_type is not None and
                    _type & Abstract.TYPE_CDS_ITEM == Abstract.TYPE_CDS_ITEM
                   ):
                    Hook.call("dNG.pas.upnp.Resource.getItemResourceClientContent", item = self)
                    if (len(self.content) == 0): Hook.call("dNG.pas.upnp.Resource.getItemResourceContent", item = self)

                    _return = True
                #
            #
        #

        return _return
Ejemplo n.º 10
0
def unregister_plugin():
    """
Unregister plugin hooks.

:since: v0.2.00
    """

    Hook.unregister("dNG.pas.user.Profile.delete", delete)
Ejemplo n.º 11
0
    def __enter__(self):
        """
python.org: Enter the runtime context related to this object.

:since: v0.2.00
        """

        Hook.register("dNG.pas.Status.onShutdown", self.stop)
Ejemplo n.º 12
0
def unregister_plugin():
    """
Unregister plugin hooks.

:since: v0.2.00
    """

    Hook.unregister("dNG.pas.user.Profile.delete", delete)
Ejemplo n.º 13
0
    def _on_shutdown(self):
        """
Callback for shutdown.

:since: v1.0.0
        """

        Hook.free()
def unregister_plugin():
    """
Unregister plugin hooks.

:since: v0.2.00
    """

    Hook.unregister("dNG.pas.upnp.SsdpRequest.filterHeaders", filter_headers)
Ejemplo n.º 15
0
def unregister_plugin():
    """
Unregister plugin hooks.

:since: v0.2.00
    """

    Hook.unregister("dNG.pas.Database.applySchema.after", after_apply_schema)
    Hook.unregister("dNG.pas.Database.loadAll", load_all)
Ejemplo n.º 16
0
def unregister_plugin():
    """
Unregister plugin hooks.

:since: v0.2.00
    """

    Hook.unregister("dNG.pas.upnp.Resource.onRootContainerAdded", on_root_container_added)
    Hook.unregister("dNG.pas.upnp.Resource.onRootContainerDeleted", on_root_container_deleted)
Ejemplo n.º 17
0
def register_plugin():
    """
Register plugin hooks.

:since: v1.0.0
    """

    Hook.register("dNG.pas.Database.applySchema.after", after_apply_schema)
    Hook.register("dNG.pas.Database.loadAll", load_all)
def register_plugin():
#
	"""
Register plugin hooks.

:since: v0.1.00
	"""

	Hook.register("dNG.pas.http.Form.sendEMail", send_email)
Ejemplo n.º 19
0
def unregister_plugin():
    """
Unregister plugin hooks.

:since: v0.2.00
    """

    Hook.unregister("dNG.pas.user.Profile.changesConfirmed", changes_confirmed)
    Hook.unregister("dNG.pas.user.Profile.registrationValidated", registration_validated)
Ejemplo n.º 20
0
def unregister_plugin():
    """
Unregister plugin hooks.

:since: v0.1.00
    """

    Hook.unregister("dNG.pas.Database.applySchema.after", after_apply_schema)
    Hook.unregister("dNG.pas.Database.loadAll", load_all)
Ejemplo n.º 21
0
def unregister_plugin():
    """
Unregister plugin hooks.

:since: v0.2.00
    """

    Hook.unregister("dNG.pas.http.Server.onStartup", on_startup)
    Hook.unregister("dNG.pas.http.Wsgi.onStartup", on_startup)
Ejemplo n.º 22
0
def unregister_plugin():
    """
Unregister plugin hooks.

:since: v0.2.00
    """

    Hook.unregister("dNG.pas.upnp.ControlPoint.onDeviceAdded", on_device_added)
    Hook.unregister("dNG.pas.upnp.ControlPoint.onStartup", on_startup)
Ejemplo n.º 23
0
    def __exit__(self, exc_type, exc_value, traceback):
        """
python.org: Exit the runtime context related to this object.

:return: (bool) True to suppress exceptions
:since:  v0.2.00
        """

        Hook.unregister("dNG.pas.Status.onShutdown", self.stop)
        return False
Ejemplo n.º 24
0
def register_plugin():
#
	"""
Register plugin hooks.

:since: v0.1.00
	"""

	Hook.register("dNG.pas.http.Server.onStartup", on_startup)
	Hook.register("dNG.pas.http.Wsgi.onStartup", on_startup)
Ejemplo n.º 25
0
    def _on_shutdown(self):
        """
Callback for shutdown.

:since: v0.2.00
        """

        Hook.call("dNG.pas.Status.onShutdown")

        if (self.cache_instance is not None): self.cache_instance.disable()
        Hook.free()
Ejemplo n.º 26
0
    def _init(self):
        """
Initializes the dispatcher and stopping hook.

:since: v0.2.00
        """

        if (self.log_handler is not None): self.log_handler.debug("#echo(__FILEPATH__)# -{0!r}._init()- (#echo(__LINE__)#)", self, context = "pas_server")

        if (self.stopping_hook is not None):
            stopping_hook = ("dNG.pas.Status.onShutdown" if (self.stopping_hook == "") else self.stopping_hook)
            Hook.register_weakref(stopping_hook, self.thread_stop)
Ejemplo n.º 27
0
def unregister_plugin():
    """
Unregister plugin hooks.

:since: v0.2.00
    """

    Hook.unregister("dNG.pas.http.Server.onShutdown", on_shutdown)
    Hook.unregister("dNG.pas.http.Server.onStartup", on_startup)
    Hook.unregister("dNG.pas.http.Wsgi.onShutdown", on_shutdown)
    Hook.unregister("dNG.pas.http.Wsgi.onStartup", on_startup)
    Hook.unregister("dNG.pas.upnp.ControlPoint.onHostDeviceAdded", on_host_device_added)
Ejemplo n.º 28
0
    def execute_subscribe(self):
        """
Action for "request"

:since: v0.2.00
        """

        self.response.init()
        self.response.set_header("Date", RfcBasics.get_rfc5322_datetime(time()))

        if (not isinstance(self.request, HttpUpnpRequest)): raise UpnpException("pas_http_core_400")
        upnp_service = self.request.get_upnp_service()
        if (not isinstance(upnp_service, AbstractService)): raise UpnpException("pas_http_core_400", 401)

        Hook.call("dNG.pas.http.l10n.upnp.Events.init")

        callback_value = self.request.get_header("Callback")
        client_settings = self.get_client_settings()
        gena_sid = self.request.get_header("SID")

        upnp_service.set_client_settings(client_settings)

        if ((callback_value is None or self.request.get_header("NT") != "upnp:event")
            and gena_sid is None
           ): raise UpnpException("pas_http_core_400", 400)

        gena = Gena.get_instance()
        timeout = self.request.get_header("Timeout")

        re_result = (None if (timeout is None) else re.match("^Second-(\\d+)$", timeout))

        if (re_result is None): timeout = int(client_settings.get("upnp_subscription_timeout", 1800))
        else: timeout = int(re_result.group(1))

        usn = upnp_service.get_usn()

        if (gena_sid is None):
            gena_variables = self.request.get_header("StateVar")

            gena_sid = gena.register(usn, callback_value, timeout, variables = gena_variables)
            if (gena_sid is None): raise UpnpException("pas_http_core_404", 412)

            self.response.set_header("SID", gena_sid)
            self.response.set_header("Timeout", "Second-{0:d}".format(timeout))
            if (gena_variables != ""): self.response.set_header("Accepted-StateVar", gena_variables)
            self.response.set_raw_data("")
        else:
            result = gena.reregister(usn, gena_sid, timeout)
            if (result == False): raise UpnpException("pas_http_core_404", 412)

            self.response.set_header("SID", gena_sid)
            self.response.set_header("Timeout", "Second-{0:d}".format(timeout))
            self.response.set_raw_data("")
def unregister_plugin():
#
	"""
Unregister plugin hooks.

:since: v0.1.00
	"""

	Hook.unregister("dNG.pas.http.Form.sendEMail", send_email)
#

##j## EOF
Ejemplo n.º 30
0
    def log_handler(self, log_handler):
        """
Sets the LogHandler.

:param log_handler: LogHandler to use

:since: v1.0.0
        """

        InteractiveCli.log_handler.fset(self, log_handler)

        Hook.set_log_handler(log_handler)
        NamedLoader.set_log_handler(log_handler)
Ejemplo n.º 31
0
    def stop(self, params = None, last_return = None):
        """
Stop the server

:param params: Parameter specified
:param last_return: The return value from the last hook called.

:return: (mixed) Return value
:since:  v1.0.0
        """

        Hook.call("dNG.pas.http.Server.onShutdown", server = self)
        return last_return
Ejemplo n.º 32
0
def handle_upnp_stream_request(request, virtual_config):
    """
Handles a UPnP stream related HTTP request.

:param request: Originating request instance
:param virtual_config: Virtual path configuration

:return: (object) Request object if valid
:since:  v0.2.00
    """

    if (not isinstance(request, AbstractHttpRequest)): raise TranslatableException("pas_http_core_400", 400)

    user_agent = request.get_header("User-Agent")
    stream_path = request.get_dsd("upnp_path")

    client_settings = ClientSettings(user_agent)

    if (client_settings.get("upnp_stream_path_use_filter", False)):
        stream_path_filtered = Hook.call("dNG.pas.http.HttpUpnpRequest.filterStreamPath", path = stream_path, user_agent = user_agent)
        if (stream_path_filtered is not None): stream_path = stream_path_filtered
    #

    if (stream_path is not None): stream_path = unquote(stream_path)

    _return = PredefinedHttpRequest()
    _return.set_module("upnp")
    _return.set_service("stream")
    _return.set_action("resource")
    _return.set_dsd("urid", stream_path)

    return _return
Ejemplo n.º 33
0
def call_task(request, virtual_config):
    """
Called for requests with the path prefix "/tasks/".

:param request: Originating request instance
:param virtual_config: Virtual path configuration

:return: (object) Request object if valid
:since:  v0.2.00
    """

    _return = None

    tid = request.get_dsd("tid")

    with ExceptionLogTrap("pas_http_site"):
        _return = (None
                   if (tid is None) else
                   Hook.call("dNG.pas.Tasks.call", client = request.get_client_host(), tid = tid)
                  )
    #

    if (_return is None):
        LogLine.warning("pas.Tasks.call refused TID '{0}'", tid, context = "pas_http_site")
        _return = handle_task_result_none()
    #

    return _return
Ejemplo n.º 34
0
    def start(self, params = None, last_return = None):
        """
Start the server

:param params: Parameter specified
:param last_return: The return value from the last hook called.

:return: (mixed) Return value
:since:  v1.0.0
        """

        self._configure()
        Thread.start(self)

        Hook.call("dNG.pas.http.Server.onStartup", server = self)
        return self