def tearDownClass(cls): default_network_config_file = simulaqron_settings._default_config[ "network_config_file"] network_config = NetworksConfigConstructor(default_network_config_file) network_config.reset() network_config.write_to_file() simulaqron_settings.default_settings()
def remove(name, network_name=None, force=False): """ Remove a node to the network. NAME: The name of the node, e.g. Alice """ if not force: answer = input( "Do you want to remove the node {} to the network {} in the file {}? (yes/no)." .format(name, network_name, simulaqron_settings.network_config_file)) if not _is_positive_answer(answer): print("Aborting!") return networks_config = NetworksConfigConstructor( simulaqron_settings.network_config_file) networks_config.remove_node(node_name=name, network_name=network_name) networks_config.write_to_file()
def default(network_name=None, force=False): """ Sets the default nodes of the network. The default network consists of the five nodes: Alice, Bob, Charlie, David, Eve """ if not force: answer = input( "Do you want to set the network {} in the file {} to default," "i.e. with nodes Alice, Bob, Charlie, David and Eve? (yes/no).". format(network_name, simulaqron_settings.network_config_file)) if not _is_positive_answer(answer): print("Aborting!") return networks_config = NetworksConfigConstructor( simulaqron_settings.network_config_file) node_names = ["Alice", "Bob", "Charlie", "David", "Eve"] networks_config.add_network(node_names=node_names, network_name=network_name) networks_config.write_to_file()
def add(name, network_name=None, hostname=None, app_port=None, cqc_port=None, vnode_port=None, neighbors=None, force=False): """ Add a node to the network. NAME: The name of the node, e.g. Alice HOSTNAME: The host name of the node, e.g. localhost or 192.168.0.1 """ if not force: answer = input( "Do you want to add the node {} to the network {} in the file {}? (yes/no)." .format(name, network_name, simulaqron_settings.network_config_file)) if not _is_positive_answer(answer): print("Aborting!") return if neighbors is not None: neighbors = neighbors.split(',') neighbors = [neighbor.strip() for neighbor in neighbors] networks_config = NetworksConfigConstructor( simulaqron_settings.network_config_file) networks_config.add_node(node_name=name, network_name=network_name, app_hostname=hostname, cqc_hostname=hostname, vnode_hostname=hostname, app_port=app_port, cqc_port=cqc_port, vnode_port=vnode_port, neighbors=neighbors) networks_config.write_to_file()
from simulaqron.network import Network from simulaqron.settings import simulaqron_settings from simulaqron.toolbox.manage_nodes import NetworksConfigConstructor from simulaqron.toolbox.reset import main as reset_simulaqron CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) PID_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".simulaqron_pids") # Check that the default network_config_file exists default_network_config_file = simulaqron_settings._default_config[ "network_config_file"] if not os.path.exists(default_network_config_file): networks_config = NetworksConfigConstructor() networks_config.reset() networks_config.write_to_file(default_network_config_file) class SimulaQronDaemon(run.RunDaemon): def __init__(self, pidfile, name=None, nrnodes=None, nodes=None, topology=None, new=True): super().__init__(pidfile=pidfile) self.name = name self.nrnodes = nrnodes self.nodes = nodes self.topology = topology
def __init__(self, name=None, nodes=None, topology=None, network_config_file=None, force=False, new=True): """ Used to spin up a simulated network. If new=True then a fresh network with only the specified nodes (or the default Alice, Bob, Charlie, David and Eve) are created and overwriting the current network with the same name in the network config file. Otherwise only the specified nodes are started without changing the config file. Note that if the nodes does not currently exists and new=False, an ValueError is raised. If force=False an input to confirm the overwriting is issued. :param name: None or str (defualts to "default") :param nodes: None or list of str :param topology: None or dict :param network_config_file: None or str (defaults to simulaqron_settings.network_config_file :param force: bool :param new: bool """ self._running = False if name is None: self.name = "default" else: self.name = name if network_config_file is None: self._network_config_file = simulaqron_settings.network_config_file else: self._network_config_file = network_config_file networks_config = NetworksConfigConstructor( file_path=self._network_config_file) if new: if nodes is None: if isinstance(topology, dict): self.nodes = list(topology.keys()) else: self.nodes = ["Alice", "Bob", "Charlie", "David", "Eve"] else: self.nodes = nodes self.topology = construct_topology_config(topology, self.nodes) if not force: answer = input( "Do you want to add/replace the network {} in the file {}" "with a network constisting of the nodes {}? (yes/no)". format(self.name, self._network_config_file, self.nodes)) if answer not in ["yes", "y"]: raise RuntimeError( "User did not want to replace network in file") networks_config.add_network(node_names=self.nodes, network_name=self.name, topology=self.topology) networks_config.write_to_file(self._network_config_file) else: if topology is not None: raise ValueError("If new is False a topology cannot be used.") if self.name in networks_config.networks: node_names = networks_config.get_node_names(self.name) self.topology = networks_config.networks[self.name].topology else: raise ValueError( "Network {} is not in the file {}\n" "If you wish to add this network to the file, use the" "--new flag.".format(self.name, self._network_config_file)) if nodes is None: self.nodes = node_names else: self.nodes = nodes for node_name in self.nodes: if node_name not in node_names: raise ValueError( "Node {} is not in the current network {} in the file {}\n" "If you wish to overwrite the current network in the file, use the" "--new flag.".format(node_name, self.name, self._network_config_file)) self.processes = [] self._setup_processes()
def _create_default_network_config(): networks_config = NetworksConfigConstructor() networks_config.reset() networks_config.write_to_file(simulaqron_settings.network_config_file)
def main(): simulaqron_settings.default_settings() networks_config = NetworksConfigConstructor() networks_config.reset() networks_config.write_to_file(simulaqron_settings.network_config_file)