/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos

  • Committer: Teddy Hogeborn
  • Date: 2009-04-16 19:16:49 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090416191649-stvuj37ts165w5j2
Code cleanup.  Move some global stuff into main.

* mandos (service, bus): No longer global variables.
  (AvahiService.__init__, ClientDBus.__init__): Take new "bus"
                                                argument.  All callers
                                                changed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
from contextlib import closing
59
59
import struct
60
60
import fcntl
 
61
import functools
61
62
 
62
63
import dbus
63
64
import dbus.service
130
131
    def __init__(self, interface = avahi.IF_UNSPEC, name = None,
131
132
                 servicetype = None, port = None, TXT = None,
132
133
                 domain = u"", host = u"", max_renames = 32768,
133
 
                 protocol = avahi.PROTO_UNSPEC):
 
134
                 protocol = avahi.PROTO_UNSPEC, bus = None):
134
135
        self.interface = interface
135
136
        self.name = name
136
137
        self.type = servicetype
143
144
        self.protocol = protocol
144
145
        self.group = None       # our entry group
145
146
        self.server = None
 
147
        self.bus = bus
146
148
    def rename(self):
147
149
        """Derived from the Avahi example code"""
148
150
        if self.rename_count >= self.max_renames:
168
170
        """Derived from the Avahi example code"""
169
171
        if self.group is None:
170
172
            self.group = dbus.Interface(
171
 
                bus.get_object(avahi.DBUS_NAME,
172
 
                               self.server.EntryGroupNew()),
 
173
                self.bus.get_object(avahi.DBUS_NAME,
 
174
                                    self.server.EntryGroupNew()),
173
175
                avahi.DBUS_INTERFACE_ENTRY_GROUP)
174
176
            self.group.connect_to_signal('StateChanged',
175
177
                                         self.entry_group_state_changed)
214
216
        """Derived from the Avahi example code"""
215
217
        if self.server is None:
216
218
            self.server = dbus.Interface(
217
 
                bus.get_object(avahi.DBUS_NAME,
218
 
                               avahi.DBUS_PATH_SERVER),
 
219
                self.bus.get_object(avahi.DBUS_NAME,
 
220
                                    avahi.DBUS_PATH_SERVER),
219
221
                avahi.DBUS_INTERFACE_SERVER)
220
222
        self.server.connect_to_signal(u"StateChanged",
221
223
                                 self.server_state_changed)
480
482
    """
481
483
    # dbus.service.Object doesn't use super(), so we can't either.
482
484
    
483
 
    def __init__(self, *args, **kwargs):
 
485
    def __init__(self, bus = None, *args, **kwargs):
 
486
        self.bus = bus
484
487
        Client.__init__(self, *args, **kwargs)
485
488
        # Only now, when this client is initialized, can it show up on
486
489
        # the D-Bus
487
490
        self.dbus_object_path = (dbus.ObjectPath
488
491
                                 (u"/clients/"
489
492
                                  + self.name.replace(u".", u"_")))
490
 
        dbus.service.Object.__init__(self, bus,
 
493
        dbus.service.Object.__init__(self, self.bus,
491
494
                                     self.dbus_object_path)
492
495
    
493
496
    @staticmethod
1297
1300
        (gnutls.library.functions
1298
1301
         .gnutls_global_set_log_function(debug_gnutls))
1299
1302
    
1300
 
    global service
1301
 
    protocol = avahi.PROTO_INET6 if use_ipv6 else avahi.PROTO_INET
1302
 
    service = AvahiService(name = server_settings[u"servicename"],
1303
 
                           servicetype = u"_mandos._tcp",
1304
 
                           protocol = protocol)
1305
 
    if server_settings["interface"]:
1306
 
        service.interface = (if_nametoindex
1307
 
                             (str(server_settings[u"interface"])))
1308
 
    
1309
1303
    global main_loop
1310
 
    global bus
1311
1304
    # From the Avahi example code
1312
1305
    DBusGMainLoop(set_as_default=True )
1313
1306
    main_loop = gobject.MainLoop()
1315
1308
    # End of Avahi example code
1316
1309
    if use_dbus:
1317
1310
        bus_name = dbus.service.BusName(u"se.bsnet.fukt.Mandos", bus)
 
1311
    protocol = avahi.PROTO_INET6 if use_ipv6 else avahi.PROTO_INET
 
1312
    service = AvahiService(name = server_settings[u"servicename"],
 
1313
                           servicetype = u"_mandos._tcp",
 
1314
                           protocol = protocol, bus = bus)
 
1315
    if server_settings["interface"]:
 
1316
        service.interface = (if_nametoindex
 
1317
                             (str(server_settings[u"interface"])))
1318
1318
    
1319
1319
    client_class = Client
1320
1320
    if use_dbus:
1321
 
        client_class = ClientDBus
 
1321
        client_class = functools.partial(ClientDBus, bus = bus)
1322
1322
    clients.update(set(
1323
1323
            client_class(name = section,
1324
1324
                         config= dict(client_config.items(section)))