def initial_notify(self): """Notify this subscriber of all evented state variables and their values""" if self.is_expired: return False # Event Key must be 0 if self.event_key != 0: return False # All Evented Variables evented_variables = self.service.get_evented_variables() variables = [] for evar in evented_variables: try: data = upnpmarshal.dumps(evar.get()) except upnpmarshal.MarshalError, why: raise NotifyError, why variables.append((evar.the_name, data))
def _service_description_toxml(service): """This function produces the UPnP XML service description.""" svs_str = "" # Evented Variables for evar in service.get_evented_variables(): data_type = upnpmarshal.dumps_data_type(evar.the_type) default_value = upnpmarshal.dumps(evar.default_value) args = (evar.the_name, data_type, default_value) svs_str += _EVENTED_VARIABLE_FMT % args actions_str = "" arg_counter = 0 # One state variable per type (event variables of arguments) unique_variables = {} # type : variable name for evar in service.get_evented_variables(): if not unique_variables.has_key(evar.the_type): unique_variables[evar.the_type] = evar.the_name # Arguments for action in service.get_actions(): args_str = "" for arg in action.in_arg_list + action.out_arg_list: # Check if argument can be related to event variable if unique_variables.has_key(arg.the_type): related_variable_name = unique_variables[arg.the_type] else: arg_counter += 1 related_variable_name = "A_ARG_TYPE_%d" % arg_counter unique_variables[arg.the_type] = related_variable_name # New State Variable data_type = upnpmarshal.dumps_data_type(arg.the_type) svs_str += _ARG_VARIABLE_FMT % (related_variable_name, data_type) # New Argument direction = 'in' if isinstance(arg, _InArg) else 'out' args_str += _ARGUMENT_FMT % (arg.the_name, related_variable_name, direction) # Action actions_str += _ACTION_FMT % (action.name, args_str) return _SERVICE_DESCRIPTION_FMT % (actions_str, svs_str)
def notify(self, evented_variables): """Notify this subscriber that given evented variables have been updated.""" if self.is_expired : return False # should not be neccesary else: self.event_key += 1 # Construct list of tuples [(name, value), ...] variables = [] for evar in evented_variables: try: data = upnpmarshal.dumps(evar.get()) except upnpmarshal.MarshalError, why: raise NotifyError, why variables.append((evar.the_name, data)) # Dispatch Notification edp = self.service.service_manager.get_event_dispatcher() edp.dispatch(self.sid, self.event_key, self.callback_url, variables) return True
# Check that result holds the correct number of values if len(result) != len(self.out_arg_list): raise ActionError, "Wrong number of Results" # Check that each value has the correct type # Also convert python type objects to string representations. # Construct out_args list of tuples [(name, data), ...] out_args = [] for i in range(len(result)): out_arg = self.out_arg_list[i] value = result[i] if not isinstance(value, out_arg.the_type): raise ActionError, "Result is wrong type." else: try: data = upnpmarshal.dumps(value) except upnpmarshal.MarshalError, why: raise ActionError, why out_args.append((out_arg.the_name, data)) return out_args ############################################## # SUBSCRIPTION ############################################## class NotifyError (exceptions.Exception): """Error associated with event notification.""" pass class _Subscription: