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