1111
1212from simulaqron .network import Network
1313from simulaqron .settings import LOCAL_SIMULAQRON_SETTINGS , LOCAL_NETWORK_SETTINGS , HOME_NETWORK_SETTINGS
14- from simulaqron .settings import simulaqron_settings , network_config
14+ from simulaqron .settings import simulaqron_settings , get_default_network_config_file , network_config
1515from simulaqron .settings .network_config import NodeConfig , DEFAULT_SIMULAQRON_NETWORK_FILENAME
1616from simulaqron .settings .simulaqron_config import SimBackend
1717
@@ -38,12 +38,41 @@ def __init__(self, pidfile: Path):
3838 )
3939
4040class SimulaQronDaemon (run .RunDaemon ):
41- def __init__ (self , pidfile : Path , name : str , nodes : List [str ]):
41+ """
42+ Daemon process that runs a SimulaQron network in the background.
43+
44+ This daemon spawns virtual nodes and QNodeOS servers for each node
45+ in the network configuration. It runs until explicitly stopped.
46+
47+ Attributes
48+ ----------
49+ name : str
50+ Name of the network (e.g., 'default').
51+ nodes : List[str]
52+ List of node names to start (e.g., ['Alice', 'Bob']).
53+ network_config_file : Path
54+ Path to the network configuration JSON file.
55+ """
56+ def __init__ (self , pidfile : Path , name : str , nodes : List [str ], network_config_file : Path ):
57+ """
58+ Initialize the SimulaQron daemon.
59+
60+ :param pidfile: Path to the PID file used to track the daemon process.
61+ :type pidfile: Path
62+ :param name: Name of the network (e.g., 'default').
63+ :type name: str
64+ :param nodes: List of node names to start (e.g., ['Alice', 'Bob']).
65+ :type nodes: List[str]
66+ :param network_config_file: Path to the network configuration file.
67+ :type network_config_file: Path
68+
69+ """
4270 super ().__init__ (
4371 pidfile = pidfile ,
4472 )
4573 self .name = name
4674 self .nodes = nodes
75+ self .network_config_file = network_config_file
4776
4877 def run (self ):
4978 """Starts all nodes defined in netsim's config directory."""
@@ -52,7 +81,15 @@ def run(self):
5281 sys .stdout = open ('/tmp/simulaqron.out' , 'w' , buffering = 1 )
5382 sys .stderr = open ('/tmp/simulaqron.err' , 'w' , buffering = 1 )
5483
55- network = Network (network_name = self .name , nodes = self .nodes )
84+ # Let's read the config file we should be working from
85+ network_config .read_from_file (self .network_config_file )
86+
87+ # Start the network to be simulated on this node
88+ network = Network (
89+ nodes = self .nodes ,
90+ network_config_file = self .network_config_file ,
91+ network_name = self .name ,
92+ )
5693 network .start ()
5794
5895 while True :
@@ -87,7 +124,7 @@ def version():
87124 help = f"Use the given network config file. Defaults to the file named " # noqa: E131
88125 f"'{ DEFAULT_SIMULAQRON_NETWORK_FILENAME } ' on the current directory." , # noqa: E131
89126 type = click .Path (exists = True , dir_okay = False , resolve_path = True , path_type = Path ),
90- default = LOCAL_NETWORK_SETTINGS
127+ default = None
91128)
92129@click .option (
93130 "--name" ,
@@ -112,7 +149,13 @@ def version():
112149)
113150def start (name : str , nrnodes : int , nodes : str , network_config_file : Path ):
114151 """Starts a network with the given parameters or from config files."""
115- network_config .read_from_file (network_config_file )
152+
153+ # Read the network configuration from the indicated file
154+ if network_config_file is None :
155+ network_config_file = get_default_network_config_file ()
156+ elif not network_config_file .exists ():
157+ raise click .BadParameter (f"File '{ network_config_file } ' does not exist." )
158+
116159 pidfile = PID_FOLDER / f"simulaqron_network_{ name } .pid"
117160 if pidfile .exists ():
118161 logging .warning ("Network with name %s is already running" , name )
@@ -124,7 +167,10 @@ def start(name: str, nrnodes: int, nodes: str, network_config_file: Path):
124167 "this can be normal. Please check your invocation line if needed." )
125168 if nrnodes > 0 and len (nodes ) < nrnodes :
126169 nodes += [f"Node{ i } " for i in range (nrnodes - len (nodes ))]
127- d = SimulaQronDaemon (pidfile = pidfile , name = name , nodes = nodes )
170+
171+ # Let's start the simulaqron daemon. We will pass the config file so it will be available
172+ # in the child process and load the same config
173+ d = SimulaQronDaemon (pidfile = pidfile , name = name , nodes = nodes , network_config_file = network_config_file )
128174 try :
129175 d .start ()
130176 except SystemExit as e :
0 commit comments