Package brisa :: Package upnp :: Module base_service
[hide private]
[frames] | no frames]

Source Code for Module brisa.upnp.base_service

  1  # Licensed under the MIT license 
  2  # http://opensource.org/licenses/mit-license.php or see LICENSE file. 
  3  # Copyright 2007-2008, Brisa Team <brisa-develop@garage.maemo.org> 
  4   
  5  """ Basic service classes 
  6   
  7  If you want a device-side service class (for publishing a service of yours), 
  8  please refer to module brisa.upnp.device.service. 
  9   
 10  If you want a controlpoint-side service class (for performing actions on 
 11  services), please refer to module brisa.upnp.control_point.service. 
 12  """ 
 13   
 14  __all__ = ('BaseService', 'BaseStateVariable') 
 15   
 16   
 17  from brisa.core import log 
 18  from brisa.core.network import parse_url 
 19   
 20   
21 -def format_rel_url(url):
22 """ Formats from the form path/to to /path/to, if required. 23 24 @return: formatted string 25 @rtype: string 26 """ 27 if not url: 28 return url 29 if not url.startswith('/'): 30 return '/%s' % url 31 else: 32 return url
33 34
35 -def parse_base_url(url):
36 parsed = parse_url(url) 37 return '%s://%s' % (parsed[0], parsed[1])
38 39
40 -class BaseService(object):
41
42 - def __init__(self, id, serv_type, url_base):
43 """ Constructor for the BaseService class. 44 45 @param serv_type: service type 46 @param url_base: base URL on the form schema://URL 47 48 @type serv_type: string 49 @type url_base: string 50 51 @note: url_base is expected on the form schema://URL. For example, 52 suppose your device is located at http://192.168.1.15 and 53 available on the specific port 12345. Also suppose your device 54 has a service called Hello, with its resources available on the 55 following addresses: 56 57 Hello SCPD: http://192.168.1.15:12345/Hello/SCPD 58 Hello Control: http://192.168.1.15:12345/Hello/Control 59 Hello EventSub: http://192.168.1.15:12345/Hello/Event 60 Hello Present.: http://192.168.1.15:12345/Hello/Presentation 61 62 These addresses can be written in the form url_base/rel_url. 63 In this case, url_base is http://192.168.1.15:12345 and 64 rel_urls are /Hello/SCPD, /Hello/Control, and so on. 65 66 Relative URL's must begin with '/' and the base URL must not end 67 in '/' (not http://192.168.1.15:12345/). 68 """ 69 self.id = id 70 self.service_type = serv_type 71 self.url_base = parse_base_url(url_base) 72 self.scpd_url = '/%s/%s' % (id, 'scpd.xml') 73 self.control_url = '/%s/%s' % (id, 'control') 74 self.event_sub_url = '/%s/%s' % (id, 'eventSub') 75 self.presentation_url = '/%s/%s' % (id, 'presentation') 76 self._actions = {} 77 self._state_variables = {}
78
79 - def get_actions(self):
80 """ Returns a dict of the service actions. 81 """ 82 return self._actions
83
84 - def get_variables(self):
85 """ Returns a dict of the service state variables. 86 """ 87 return self._state_variables
88
89 - def add_state_variable(self, state_variable):
90 """ Adds a service state variable. 91 92 @param state_variable: the service state variable 93 @type state_variable: BaseStateVariable 94 """ 95 self._state_variables[state_variable.name] = state_variable
96 97
98 -class BaseStateVariable(object):
99 """ Represents a service state variable. 100 """ 101
102 - def __init__(self, service, name, send_events, multicast, data_type, values):
103 """ Constructor for the StateVariable class. 104 105 @param service: Service which holds this variable 106 @param name: variable name 107 @param send_events: send events option 108 @param multicast: multicast option 109 @param data_type: data type 110 @param values: values 111 112 @type service: Service 113 @type name: string 114 @type send_events: boolean 115 @type multicast: boolean 116 @type data_type: string 117 @type values: string 118 """ 119 self.parent_service = service 120 self.name = name 121 self.send_events = send_events 122 self.multicast = multicast 123 self.data_type = data_type 124 self.allowed_values = values 125 self._value = None 126 self._callbacks = []
127
128 - def get_value(self):
129 """ Returns the value of the variable. 130 """ 131 return self._value
132
133 - def update(self, value):
134 """ Updates the state variable value. The new value must 135 has the same type as specified at data type. 136 137 @param value: new value. 138 @type value: data_type 139 """ 140 log.debug("Updating state variable") 141 if self._value == value: 142 log.debug("Don't update") 143 return 144 145 self._value = value 146 if self.send_events: 147 for callback in self._callbacks: 148 try: 149 callback(self.name, self._value) 150 except Exception, e: 151 log.error('Error at callback %s' % str(callback)) 152 raise e
153
154 - def subscribe_for_update(self, callback):
155 """ Subscribes the callback for variable update event. 156 157 @param callback: callback which will listen on the event 158 @type callback: callable 159 """ 160 self._callbacks.append(callback)
161
162 - def unsubscribe_for_update(self, callback):
163 """ Unsubscribes the callback for variable update event. 164 165 @param callback: callback which listens for the event 166 @type callback: callable 167 """ 168 self._callbacks.remove(callback)
169