def get(network_name=None):
    """Get the current nodes of the network."""
    networks_config = NetworksConfigConstructor(
        simulaqron_settings.network_config_file)
    try:
        nodes = networks_config.get_node_names(network_name=network_name)
    except ValueError:
        print("No network {}".format(network_name))
    else:
        print(("{} " * len(nodes))[:-1].format(*nodes))
Exemple #2
0
    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()