def handle_register_peer_config(self, message_obj): params = cmsg.params2dict(message_obj) ext_params = cmsg.params2dict(message_obj, field_name="ext_params") peer_id = message_obj.sender launcher_msg = None if peer_id in self._configs: mtype = types.CONFIG_ERROR msg = cmsg.fill_msg(mtype) else: self._configs[peer_id] = params self._ext_configs[peer_id] = ext_params mtype = types.PEER_REGISTERED msg = cmsg.fill_msg(mtype, peer_id=peer_id) launcher_msg = self.mtool.fill_msg('obci_peer_registered', peer_id=peer_id, params=params) self._save_config() return msg, mtype, launcher_msg
def _request_ext_params(self, connection, retries=400): #TODO set timeout and retry count self.logger.info("requesting external parameters") if self.peer is None: raise NoPeerError def _unset_param_count(): return reduce(lambda x, y: x + y, [len(self.core.unset_params_for_source(src)) \ for src in self.core.used_config_sources()], 0) ready, details = self.core.config_ready() while not ready and retries: for src in self.core.used_config_sources(): params = self.core.unset_params_for_source(src).keys() msg = cmsg.fill_msg(types.GET_CONFIG_PARAMS, sender=self.peer_id, param_names=params, receiver=self.core.config_sources[src]) #print "requesting: {0}".format(msg) reply = self.__query(connection, cmsg.pack_msg(msg), types.GET_CONFIG_PARAMS) if reply == None: # raise something? continue if reply.type == types.CONFIG_ERROR: self.logger.warning("peer {0} has not yet started".format( msg.receiver)) elif reply.type == types.CONFIG_PARAMS: reply_msg = cmsg.unpack_msg(reply.type, reply.message) params = cmsg.params2dict(reply_msg) for par, val in params.iteritems(): self.core.set_param_from_source( reply_msg.sender, par, val) else: self.logger.error("WTF? {0}".format(reply.message)) # print '.',#"{0} external params still unset".format(_unset_param_count()) time.sleep(0.4) ready, details = self.core.config_ready() retries -= 1 if ready: self.logger.info("External parameters initialised %s", str(self.core.config_ready())) return ready, details
def _request_ext_params(self, connection, retries=400): # TODO set timeout and retry count self.logger.info("requesting external parameters") if self.peer is None: raise NoPeerError def _unset_param_count(): return reduce(lambda x, y: x + y, [len(self.core.unset_params_for_source(src)) for src in self.core.used_config_sources()], 0) ready, details = self.core.config_ready() while not ready and retries: for src in self.core.used_config_sources(): params = list(self.core.unset_params_for_source(src).keys()) msg = cmsg.fill_msg(types.GET_CONFIG_PARAMS, sender=self.peer_id, param_names=params, receiver=self.core.config_sources[src]) # print "requesting: {0}".format(msg) reply = self.__query(connection, cmsg.pack_msg(msg), types.GET_CONFIG_PARAMS) if reply is None: # raise something? continue if reply.type == types.CONFIG_ERROR: self.logger.warning("peer {0} has not yet started".format(msg.receiver)) elif reply.type == types.CONFIG_PARAMS: reply_msg = cmsg.unpack_msg(reply.type, reply.message) params = cmsg.params2dict(reply_msg) for par, val in params.items(): self.core.set_param_from_source(reply_msg.sender, par, val) else: self.logger.error("WTF? {0}".format(reply.message)) # print '.',#"{0} external params still unset".format(_unset_param_count()) time.sleep(0.4) ready, details = self.core.config_ready() retries -= 1 if ready: self.logger.info("External parameters initialised %s", str(self.core.config_ready())) return ready, details
def _handle_params_changed(self, p_msg): self.logger.info("PARAMS CHANGED - %s" % p_msg.sender) params = cmsg.params2dict(p_msg) param_owner = p_msg.sender old_values = {} updated = {} if param_owner in self.core.config_sources: src_params = self.core.params_for_source(param_owner) for par_name in [par for par in params if par in src_params]: old = self.core.get_param(par_name) new = params[par_name] if old != params[par_name]: old_values[par_name] = old updated[par_name] = new self.core.set_param_from_source(p_msg.sender, par_name, new) if not self.peer_params_changed(updated): #restore... for par, val in old_values.iteritems(): self.core.set_param_from_source(p_msg.sender, par, val) if param_owner == self.peer_id: local_params = self.core.local_params for par, val in params.iteritems(): if par not in local_params: ## protest? continue if val != self.core.get_param(par): old_values[par] = self.core.get_param(par) updated[par] = val self.core.update_local_param(par, val) if not self.peer_params_changed(updated): for par, val in old_values.iteritems(): self.core.update_local_param(par, val) return None, None
def _handle_params_changed(self, p_msg): self.logger.info("PARAMS CHANGED - %s" % p_msg.sender) params = cmsg.params2dict(p_msg) param_owner = p_msg.sender old_values = {} updated = {} if param_owner in self.core.config_sources: src_params = self.core.params_for_source(param_owner) for par_name in [par for par in params if par in src_params]: old = self.core.get_param(par_name) new = params[par_name] if old != params[par_name]: old_values[par_name] = old updated[par_name] = new self.core.set_param_from_source(p_msg.sender, par_name, new) if not self.peer_params_changed(updated): # restore... for par, val in old_values.items(): self.core.set_param_from_source(p_msg.sender, par, val) if param_owner == self.peer_id: local_params = self.core.local_params for par, val in params.items(): if par not in local_params: # protest? continue if val != self.core.get_param(par): old_values[par] = self.core.get_param(par) updated[par] = val self.core.update_local_param(par, val) if not self.peer_params_changed(updated): for par, val in old_values.items(): self.core.update_local_param(par, val) return None, None
def handle_update_params(self, message_obj): params = cmsg.params2dict(message_obj) param_owner = message_obj.sender if param_owner not in self._configs: launcher_msg = None return cmsg.fill_msg(types.CONFIG_ERROR, error_str="Peer unknown: {0}".format(param_owner)),\ types.CONFIG_ERROR,\ launcher_msg updated = {} for param in params: if param in self._configs[param_owner]: self._configs[param_owner][param] = params[param] updated[param] = params[param] if updated: mtype = types.PARAMS_CHANGED msg = cmsg.fill_msg(types.PARAMS_CHANGED, sender=param_owner) cmsg.dict2params(updated, msg) launcher_msg = self.mtool.fill_msg('obci_peer_params_changed', peer_id=param_owner, params=updated) self._save_config() return msg, mtype, launcher_msg return None, None, None
def restart_scenario(conn, new_scenario, comment="Wait...", leave_on=[], overwrites=[]): """ new_scenario: scenario_path relative to obci_root overwrites: {'peer_id' : ['-p', 'param_name', 'param_value', '-p', 'etc', 'etc', '-f' 'config_ini_file_path_relative_to_obci_root']} leave_on: ['list', 'of', 'peer_ids', 'which', 'are', 'in', 'both', 'scenarios', 'and', 'we', 'do','not', 'want', 'them', 'to', 'restart'] """ if new_scenario.startswith('/') or new_scenario.startswith('~'): new_scenario = os.path.expanduser(new_scenario) else: new_scenario = os.path.join(obci_root(), new_scenario) conf_msg = cmsg.fill_msg(types.GET_CONFIG_PARAMS, sender='', param_names=['experiment_uuid'], receiver='config_server') try: reply = conn.query(message=conf_msg, type=types.GET_CONFIG_PARAMS) except OperationFailed: print "OperationFailed (in restart_scenario) Could not connect to config server" reply = None except OperationTimedOut: print "Operation timed out! (in restart_scenario) (could not connect to config server)" reply = None if reply == None: return if reply.type == types.CONFIG_ERROR: print "(in restart_scenario) could not acquire current experiment uuid" return elif reply.type == types.CONFIG_PARAMS: reply_msg = cmsg.unpack_msg(reply.type, reply.message) params = cmsg.params2dict(reply_msg) if not 'experiment_uuid' in params: print "no experiment_uuid!!!" else: uid = params['experiment_uuid'] if not uid: print 'experiment uuid unset!!!' else: ovr_list = ['--ovr'] for peer in overwrites: scan = overwrites[peer] params = overwrites[peer] cut = 0 while scan: if '-f' in scan: ind = scan.index('-f') params[cut+ind+1] = os.path.join(obci_root(), params[cut+ind+1]) cut = cut+ind+1 scan = params[cut:] else: break # for i in range(len(params)): # params[i] = params[i].replace(" ", "\ ") # print params[i] ovr_list += ['--peer', peer] ovr_list += params # leave_on_str = '' if not leave_on else ' --leave_on ' + ' '.join(leave_on) # overwr_str = '' if not overwrites else ' '.join(ovr_list) # command = "sleep 0.5 && obci morph " + str(uid) + " " + new_scenario + \ # " " + leave_on_str + overwr_str + " &" subpr_call = ['obci', 'morph', str(uid), new_scenario, '--leave_on'] + leave_on + ovr_list print "---------------\n", subpr_call, "\n--------------" subprocess.call(subpr_call)
def restart_scenario(conn, new_scenario, comment="Wait...", leave_on=[], overwrites=[]): """ new_scenario: scenario_path relative to obci_root overwrites: {'peer_id' : ['-p', 'param_name', 'param_value', '-p', 'etc', 'etc', '-f' 'config_ini_file_path_relative_to_obci_root']} leave_on: ['list', 'of', 'peer_ids', 'which', 'are', 'in', 'both', 'scenarios', 'and', 'we', 'do','not', 'want', 'them', 'to', 'restart'] """ if new_scenario.startswith('/') or new_scenario.startswith('~'): new_scenario = os.path.expanduser(new_scenario) else: new_scenario = os.path.join(obci_root(), new_scenario) conf_msg = cmsg.fill_msg(types.GET_CONFIG_PARAMS, sender='', param_names=['experiment_uuid'], receiver='config_server') try: reply = conn.query(message=conf_msg, type=types.GET_CONFIG_PARAMS) except OperationFailed: print "OperationFailed (in restart_scenario) Could not connect to config server" reply = None except OperationTimedOut: print "Operation timed out! (in restart_scenario) (could not connect to config server)" reply = None if reply == None: return if reply.type == types.CONFIG_ERROR: print "(in restart_scenario) could not acquire current experiment uuid" return elif reply.type == types.CONFIG_PARAMS: reply_msg = cmsg.unpack_msg(reply.type, reply.message) params = cmsg.params2dict(reply_msg) if not 'experiment_uuid' in params: print "no experiment_uuid!!!" else: uid = params['experiment_uuid'] if not uid: print 'experiment uuid unset!!!' else: ovr_list = ['--ovr'] for peer in overwrites: scan = overwrites[peer] params = overwrites[peer] cut = 0 while scan: if '-f' in scan: ind = scan.index('-f') params[cut+ind+1] = os.path.join(params[cut+ind+1]) cut = cut+ind+1 scan = params[cut:] else: break # for i in range(len(params)): # params[i] = params[i].replace(" ", "\ ") # print params[i] ovr_list += ['--peer', peer] ovr_list += params # leave_on_str = '' if not leave_on else ' --leave_on ' + ' '.join(leave_on) # overwr_str = '' if not overwrites else ' '.join(ovr_list) # command = "sleep 0.5 && obci morph " + str(uid) + " " + new_scenario + \ # " " + leave_on_str + overwr_str + " &" obci_call = ["obci"] try: subprocess.call(obci_call) except: # (OSError, WindowsError) can't call launcher like that obci_call = ["python", os.path.join(obci_root(), "control", "launcher", "obci_script.py")] subpr_call = obci_call + ['morph', str(uid), new_scenario, '--leave_on'] + leave_on + ovr_list print "---------------\n", subpr_call, "\n--------------" subprocess.call(subpr_call)