1 # Copyright (C) 2008 Huang Peng <phuang@redhat.com>
3 # Permission is hereby granted, free of charge, to any person
4 # obtaining a copy of this software and associated documentation
5 # files (the "Software"), to deal in the Software without
6 # restriction, including without limitation the rights to use, copy,
7 # modify, merge, publish, distribute, sublicense, and/or sell copies
8 # of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 # DEALINGS IN THE SOFTWARE.
24 import dbus
.connection
27 class Server (_dbus_bindings
.Server
):
28 def __init__ (self
, address
):
29 _dbus_bindings
.Server
.__init
__ (self
, address
)
30 self
._connections
= []
32 self
.set_new_connection_function (self
._new
_connection
_cb
, dbus
.connection
.Connection
)
34 def register_object (self
, obj
, path
):
35 if path
in self
._objects
:
36 raise Exception ("%s has been registered", path
)
37 self
._objects
[path
] = obj
39 for conn
in self
._connections
:
40 obj
.add_to_connection (conn
, path
)
42 def _new_connection_cb (self
, server
, new_connection
):
43 self
._connections
.append (new_connection
)
44 new_connection
.add_message_filter (self
._message
_filter
_cb
)
46 # add all objects to the new connection
47 for path
, obj
in self
._objects
.items ():
48 obj
.add_to_connection (new_connection
, path
)
50 self
.new_connection (server
, new_connection
)
52 def _message_filter_cb (self
, connection
, message
):
53 if message
.get_interface() == "org.freedesktop.DBus.Local" and \
54 message
.get_member() == "Disconnected":
55 # remove all object from this connection
56 for path
, obj
in self
._objects
.items ():
57 obj
.remove_from_connection (connection
, path
)
59 self
.remove_connection (connection
)
60 self
._connections
.remove (connection
)
61 return dbus
.lowlevel
.HANDLER_RESULT_HANDLED
63 return dbus
.lowlevel
.HANDLER_RESULT_NOT_YET_HANDLED
65 def new_connection (self
, server
, connection
):
68 def remove_connection (self
, server
, connection
):