Fixed the documentation
[zukebox] / zukebox_server / src / server / zukebox_server.py
1
2 from ziface import ZIface
3 import dbus.service
4 import dbus.mainloop.glib
5
6 from brisa.core.reactors import GLib2Reactor
7 reactor = GLib2Reactor()
8
9 from brisa.core import log
10 from brisa.core import config
11 from brisa.upnp.device import Device, Service
12 from brisa.upnp.services.cds import ContentDirectory
13 from brisa.upnp.services.connmgr import ConnectionManagerServer
14
15 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
16
17 class ZukeBoxServer(ZIface, dbus.service.Object):
18     """
19     Introduction
20     ============
21     ZukeBox is an abstraction for a JukeBox.
22     How works?
23         In a JukeBox somebody pay and choose a song for play, this way the
24     JukeBox should have default set of songs. ZukeBox has the same idea,
25     except for payment ;) , you can choose a song availble in the server 
26     called ZukeBox Server or send a request for server to play your music, 
27     
28     Thus the clientes should be scan the network and get services availble 
29     in ZukeBox Server.
30     Services availble are:
31         1. Show the songs availble in the ZukeBox Server.
32         2. Play a song availble.
33         3. Play a song sent by a control point.
34     Some services will be availble too throught dbus.service a method for 
35     rescan the default paths and save in the database.
36     """
37
38     DBUS_SERVICE_NAME = "br.org.zagaia"
39     DBUS_OBJ_PATH = "/br/org/zagaia/ZukeBox"
40     DBUS_IFACE = "br.org.zagaia.ZukeBox"
41
42     plugins_folder = config.get_parameter("zukebox_server", "plugins")
43     plugins_module_path = "zukebox_server.plugins"
44
45     def __init__(self, _server_name, _listen_url):
46         """ ZukeBox Server Construct
47         @param _server_name: Name of the ZukeBox Server
48         @param _listen_url: url to listen for requests
49
50         @type _server_name: string
51         @type _listen_url: string
52         """
53         ZIface.__init__(self)
54         bus = dbus.SessionBus()
55         busname = dbus.service.BusName(self.DBUS_SERVICE_NAME, bus=bus)
56
57         dbus.service.Object.__init__(self, busname, self.DBUS_OBJ_PATH)
58         self.server_name = _server_name
59         self.listen_url = _listen_url
60         self.device = None
61         self.cds = None
62
63     def _create_device(self):
64         model_name = "ZukeBox Server version 0.1"
65         project_page = "http://portal.fucapi.edu.br/nepcomp/zagaia"
66         serial_no = '0000010'
67         model_description = 'A UPnP Server for ZukeBox'
68         self.device = Device('urn:schemas-upnp-org:device:ZukeBoxServer:1',
69                 self.server_name, force_listen_url=self.listen_url,
70                 manufacturer="Zagaia Laboratory and INdT Brazil",
71                 manufacturer_url=project_page,
72                 model_description=model_description,
73                 model_name=model_name, model_number=serial_no,
74                 model_url=project_page, serial_number=serial_no)
75
76
77
78     def _create_service(self):
79         self.cds = ContentDirectory(self.plugins_folder,
80                 self.plugins_module_path)
81         cm = ConnectionManagerServer()
82         self.device.add_service(self.cds)
83         self.device.add_service(cm)
84
85     def start(self):
86         """Start the ZukeBox Server"""
87         self._create_device()
88         self._create_service()
89         self.device.start()
90         reactor.add_after_stop_func(self.device.stop)
91         reactor.main()
92
93     # DBUS
94     @dbus.service.method(DBUS_IFACE)
95     def halt(self):
96         reactor.main_quit()
97
98     @dbus.service.method(DBUS_IFACE)
99     def rescan_audio_folder(self):
100         if not self.cds:
101             return
102         pm = self.cds.control_controller.plugin_manager
103         if not "audio_library" in pm.plugins_instances:
104             return
105         pm.plugins_instances["audio_library"].process_audio_folder()
106
107     @dbus.service.method(DBUS_IFACE)
108     def reload_config(self):
109         config.manager.update()
110
111
112