#!/bin/python
#
# Copyright (C) 2012 Intel Corporation
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Authors:
#              ChengTao.Liu  <chengtaox.liu@intel.com>
#              Yuanyuan,Zou  <yuanyuanx.zou@intel.com>
""" testkit dbus service"""

import sys
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
import gobject


class DeviceKeeper(dbus.service.Object):
    """
    testkit dbus service implementation
    """

    def init(self):
        self._devices = []

    @dbus.service.method(dbus_interface="com.intel.testkit", in_signature="s", out_signature="b")
    def addDevice(self, device_id):
        if device_id in self._devices:
            return False
        else:
            self._devices.append(device_id)
            return True

    @dbus.service.method(dbus_interface="com.intel.testkit", in_signature="s", out_signature="")
    def removeDevice(self, device_id):
        if device_id in self._devices:
            self._devices.remove(device_id)
        return None


if __name__ == '__main__':
    DBusGMainLoop(set_as_default=True)
    gobject.threads_init()
    bus = dbus.SessionBus()
    if bus.name_has_owner('com.intel.testkit'):
        print 'An instance of testkit service is running!'
        sys.exit()
    bus_name = dbus.service.BusName('com.intel.testkit', bus)
    t_object = DeviceKeeper(bus_name, "/com/intel/testkit/devices")
    t_object.init()
    loop = gobject.MainLoop()
    loop.run()