server.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright (C) 2008 Openismus GmbH <http://openismus.com/>
  2. # Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
  3. #
  4. # Permission is hereby granted, free of charge, to any person
  5. # obtaining a copy of this software and associated documentation
  6. # files (the "Software"), to deal in the Software without
  7. # restriction, including without limitation the rights to use, copy,
  8. # modify, merge, publish, distribute, sublicense, and/or sell copies
  9. # of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be
  13. # included in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. # DEALINGS IN THE SOFTWARE.
  23. __all__ = ('Server', )
  24. __docformat__ = 'reStructuredText'
  25. from _dbus_bindings import _Server
  26. from dbus.connection import Connection
  27. class Server(_Server):
  28. """An opaque object representing a server that listens for connections from
  29. other applications.
  30. This class is not useful to instantiate directly: you must subclass it and
  31. either extend the method connection_added, or append to the
  32. list on_connection_added.
  33. :Since: 0.83
  34. """
  35. def __new__(cls, address, connection_class=Connection,
  36. mainloop=None, auth_mechanisms=None):
  37. """Construct a new Server.
  38. :Parameters:
  39. `address` : str
  40. Listen on this address.
  41. `connection_class` : type
  42. When new connections come in, instantiate this subclass
  43. of dbus.connection.Connection to represent them.
  44. The default is Connection.
  45. `mainloop` : dbus.mainloop.NativeMainLoop or None
  46. The main loop with which to associate the new connections.
  47. `auth_mechanisms` : sequence of str
  48. Authentication mechanisms to allow. The default is to allow
  49. any authentication mechanism supported by ``libdbus``.
  50. """
  51. return super(Server, cls).__new__(cls, address, connection_class,
  52. mainloop, auth_mechanisms)
  53. def __init__(self, *args, **kwargs):
  54. self.__connections = {}
  55. self.on_connection_added = []
  56. """A list of callbacks to invoke when a connection is added.
  57. They receive two arguments: this Server and the new Connection."""
  58. self.on_connection_removed = []
  59. """A list of callbacks to invoke when a connection becomes
  60. disconnected. They receive two arguments: this Server and the removed
  61. Connection."""
  62. # This method name is hard-coded in _dbus_bindings._Server.
  63. # This is not public API.
  64. def _on_new_connection(self, conn):
  65. conn.call_on_disconnection(self.connection_removed)
  66. self.connection_added(conn)
  67. def connection_added(self, conn):
  68. """Respond to the creation of a new Connection.
  69. This base-class implementation just invokes the callbacks in
  70. the on_connection_added attribute.
  71. :Parameters:
  72. `conn` : dbus.connection.Connection
  73. A D-Bus connection which has just been added.
  74. The type of this parameter is whatever was passed
  75. to the Server constructor as the ``connection_class``.
  76. """
  77. if self.on_connection_added:
  78. for cb in self.on_connection_added:
  79. cb(conn)
  80. def connection_removed(self, conn):
  81. """Respond to the disconnection of a Connection.
  82. This base-class implementation just invokes the callbacks in
  83. the on_connection_removed attribute.
  84. :Parameters:
  85. `conn` : dbus.connection.Connection
  86. A D-Bus connection which has just become disconnected.
  87. The type of this parameter is whatever was passed
  88. to the Server constructor as the ``connection_class``.
  89. """
  90. if self.on_connection_removed:
  91. for cb in self.on_connection_removed:
  92. cb(conn)
  93. address = property(_Server.get_address)
  94. id = property(_Server.get_id)
  95. is_connected = property(_Server.get_is_connected)