def __init__(self):
        log.info("Queue startin up")
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        self.session_bus = dbus.SessionBus()
        self.name = dbus.service.BusName("included.errors.Client", self.session_bus)
        dbus.service.Object.__init__(self, self.session_bus,
                                     '/Queue')
        self.db = shared.data.create_database()
        self.output = NetworkOutQueue(self.socket, self.db, config.client.name)
        self.input = NetworkInQueue(self.socket, self.db)
        self.inputs = [sys.stdin]

        self.input.connect("socket-broken", self._socket_broken)
        self.output.connect("socket-broken", self._socket_broken)
class ClientNetworkHandler(dbus.service.Object):
    output = None
    input = None
    server = ()
    socket = None
    connected = False
    closing = False
    use_backup = False

    def __init__(self):
        log.info("Queue startin up")
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        self.session_bus = dbus.SessionBus()
        self.name = dbus.service.BusName("included.errors.Client", self.session_bus)
        dbus.service.Object.__init__(self, self.session_bus,
                                     '/Queue')
        self.db = shared.data.create_database()
        self.output = NetworkOutQueue(self.socket, self.db, config.client.name)
        self.input = NetworkInQueue(self.socket, self.db)
        self.inputs = [sys.stdin]

        self.input.connect("socket-broken", self._socket_broken)
        self.output.connect("socket-broken", self._socket_broken)

    @dbus.service.method(dbus_interface='included.errors.Client',
                         in_signature='si', out_signature='i')
    def enqueue(self, msg, prio):
        local_id = self.output.enqueue(msg, prio)
        #print "Queued: ", msg
        #print self.output
        return local_id

    @dbus.service.method(dbus_interface='included.errors.Client',
                         in_signature='', out_signature='s')
    def dbus_close(self):
        self.close()
        return "queue closing"

    def connect_to_server(self):
        log.info("Connecting to %s..." % ("primary", "backup")[self.use_backup])
        if self.use_backup:
            host = config.server.backup.ip
            port = config.server.backup.port
        else:
            host = config.server.ip
            port = config.server.port

        if config.server.ssh == True:
            ssh_options = ["ssh",
                             "-C", "-f",
                             "-L", str(config.server.localport)+":127.0.0.1:"+str(port),
                             host, "-o", "TCPKeepAlive=yes", "sleep", "10"]
            print " ".join(ssh_options)
            subprocess.call(ssh_options)
            host = "127.0.0.1"
            port = config.server.localport
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            self.socket.connect((host, port))
            self.connected = True
            self.closing = False
            self.inputs.append(self.socket)
            self._login()
            log.info("Connected :D")
            return True
        except socket.error, (errno, errmsg):
            self._handle_error(errno)
        return False