Blame dbus/server.py

Packit 130fc8
# Copyright (C) 2008 Openismus GmbH <http://openismus.com/>
Packit 130fc8
# Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
Packit 130fc8
#
Packit 130fc8
# Permission is hereby granted, free of charge, to any person
Packit 130fc8
# obtaining a copy of this software and associated documentation
Packit 130fc8
# files (the "Software"), to deal in the Software without
Packit 130fc8
# restriction, including without limitation the rights to use, copy,
Packit 130fc8
# modify, merge, publish, distribute, sublicense, and/or sell copies
Packit 130fc8
# of the Software, and to permit persons to whom the Software is
Packit 130fc8
# furnished to do so, subject to the following conditions:
Packit 130fc8
#
Packit 130fc8
# The above copyright notice and this permission notice shall be
Packit 130fc8
# included in all copies or substantial portions of the Software.
Packit 130fc8
#
Packit 130fc8
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 130fc8
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 130fc8
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 130fc8
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit 130fc8
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
Packit 130fc8
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit 130fc8
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit 130fc8
# DEALINGS IN THE SOFTWARE.
Packit 130fc8
Packit 130fc8
__all__ = ('Server', )
Packit 130fc8
__docformat__ = 'reStructuredText'
Packit 130fc8
Packit 130fc8
from _dbus_bindings import _Server
Packit 130fc8
from dbus.connection import Connection
Packit 130fc8
Packit 130fc8
class Server(_Server):
Packit 130fc8
    """An opaque object representing a server that listens for connections from
Packit 130fc8
    other applications.
Packit 130fc8
Packit 130fc8
    This class is not useful to instantiate directly: you must subclass it and
Packit 130fc8
    either extend the method connection_added, or append to the
Packit 130fc8
    list on_connection_added.
Packit 130fc8
Packit 130fc8
    :Since: 0.83
Packit 130fc8
    """
Packit 130fc8
Packit 130fc8
    def __new__(cls, address, connection_class=Connection,
Packit 130fc8
        mainloop=None, auth_mechanisms=None):
Packit 130fc8
        """Construct a new Server.
Packit 130fc8
Packit 130fc8
        :Parameters:
Packit 130fc8
            `address` : str
Packit 130fc8
                Listen on this address.
Packit 130fc8
            `connection_class` : type
Packit 130fc8
                When new connections come in, instantiate this subclass
Packit 130fc8
                of dbus.connection.Connection to represent them.
Packit 130fc8
                The default is Connection.
Packit 130fc8
            `mainloop` : dbus.mainloop.NativeMainLoop or None
Packit 130fc8
                The main loop with which to associate the new connections.
Packit 130fc8
            `auth_mechanisms` : sequence of str
Packit 130fc8
                Authentication mechanisms to allow. The default is to allow
Packit 130fc8
                any authentication mechanism supported by ``libdbus``.
Packit 130fc8
        """
Packit 130fc8
        return super(Server, cls).__new__(cls, address, connection_class,
Packit 130fc8
                mainloop, auth_mechanisms)
Packit 130fc8
Packit 130fc8
    def __init__(self, *args, **kwargs):
Packit 130fc8
Packit 130fc8
        self.__connections = {}
Packit 130fc8
Packit 130fc8
        self.on_connection_added = []
Packit 130fc8
        """A list of callbacks to invoke when a connection is added.
Packit 130fc8
        They receive two arguments: this Server and the new Connection."""
Packit 130fc8
Packit 130fc8
        self.on_connection_removed = []
Packit 130fc8
        """A list of callbacks to invoke when a connection becomes
Packit 130fc8
        disconnected. They receive two arguments: this Server and the removed
Packit 130fc8
        Connection."""
Packit 130fc8
Packit 130fc8
    # This method name is hard-coded in _dbus_bindings._Server.
Packit 130fc8
    # This is not public API.
Packit 130fc8
    def _on_new_connection(self, conn):
Packit 130fc8
        conn.call_on_disconnection(self.connection_removed)
Packit 130fc8
        self.connection_added(conn)
Packit 130fc8
Packit 130fc8
    def connection_added(self, conn):
Packit 130fc8
        """Respond to the creation of a new Connection.
Packit 130fc8
Packit 130fc8
        This base-class implementation just invokes the callbacks in
Packit 130fc8
        the on_connection_added attribute.
Packit 130fc8
Packit 130fc8
        :Parameters:
Packit 130fc8
            `conn` : dbus.connection.Connection
Packit 130fc8
                A D-Bus connection which has just been added.
Packit 130fc8
Packit 130fc8
                The type of this parameter is whatever was passed
Packit 130fc8
                to the Server constructor as the ``connection_class``.
Packit 130fc8
        """
Packit 130fc8
        if self.on_connection_added:
Packit 130fc8
            for cb in self.on_connection_added:
Packit 130fc8
                cb(conn)
Packit 130fc8
Packit 130fc8
    def connection_removed(self, conn):
Packit 130fc8
        """Respond to the disconnection of a Connection.
Packit 130fc8
Packit 130fc8
        This base-class implementation just invokes the callbacks in
Packit 130fc8
        the on_connection_removed attribute.
Packit 130fc8
Packit 130fc8
        :Parameters:
Packit 130fc8
            `conn` : dbus.connection.Connection
Packit 130fc8
                A D-Bus connection which has just become disconnected.
Packit 130fc8
Packit 130fc8
                The type of this parameter is whatever was passed
Packit 130fc8
                to the Server constructor as the ``connection_class``.
Packit 130fc8
        """
Packit 130fc8
        if self.on_connection_removed:
Packit 130fc8
            for cb in self.on_connection_removed:
Packit 130fc8
                cb(conn)
Packit 130fc8
Packit 130fc8
    address      = property(_Server.get_address)
Packit 130fc8
    id           = property(_Server.get_id)
Packit 130fc8
    is_connected = property(_Server.get_is_connected)
Packit 130fc8