@@ -30,7 +30,7 @@ def _init_register(virt_root, my_name: str, node: NetQASMFactory):
3030 _setup_netqasm_server (my_name , node )
3131
3232
33- def _connect_to_virt_node (my_name : str , netqasm_factory : NetQASMFactory , virtual_network : SocketsConfig ):
33+ def _connect_to_virt_node (my_name : str , netqasm_factory : NetQASMFactory , virtual_network : SocketsConfig , attempt : int = 0 ):
3434 """Tries to connect to local virtual node.
3535
3636 If connection is refused, we try again after a set amount of time
@@ -49,36 +49,38 @@ def _connect_to_virt_node(my_name: str, netqasm_factory: NetQASMFactory, virtual
4949 defer_virtual_node .addCallback (_init_register , my_name , netqasm_factory )
5050 # If connection fails do:
5151 defer_virtual_node .addErrback (_handle_connection_error , my_name , netqasm_factory , virtual_network ,
52- virtual_node .hostname , virtual_node .port )
52+ virtual_node .hostname , virtual_node .port , attempt )
5353
5454
5555def _handle_connection_error (reason , my_name : str , netqasm_factory : NetQASMFactory , virtual_network : SocketsConfig ,
56- virtual_node_hostname : str , virtual_node_port : int ):
56+ virtual_node_hostname : str , virtual_node_port : int , attempt : int ):
5757 """ Handles errors from trying to connect to local virtual node.
5858
5959 If a ConnectionRefusedError is raised another try will be made after
6060 Settings.CONF_WAIT_TIME seconds. Any other error is raised again.
6161 """
6262 try :
6363 reason .raiseException ()
64- except ConnectionRefusedError as err :
65- # TODO - Implement checking of max number of connections
66- logger .debug ("START_QNODEOS %s: Could not connect to Virtual node (%s, %d), trying again..." , my_name ,
67- virtual_node_hostname , virtual_node_port , exc_info = err )
68- reactor .callLater (
69- simulaqron_settings .conn_retry_time ,
70- _connect_to_virt_node ,
71- my_name ,
72- netqasm_factory ,
73- virtual_network ,
74- )
75- except Exception as e :
76- logger .error (
77- "START_QNODEOS %s: Critical error when connection to local virtual node: %s" ,
78- my_name ,
79- e ,
80- )
81- reactor .stop ()
64+ except Exception as err :
65+ if attempt > simulaqron_settings .conn_max_retries :
66+ logger .exception (
67+ "START_QNODEOS %s: Exhausted the maximum number of attempts to connect to local virtual node" ,
68+ my_name ,
69+ exc_info = err ,
70+ )
71+ reactor .stop ()
72+ return
73+ else :
74+ logger .debug ("START_QNODEOS %s: Could not connect to Virtual node (%s, %d), trying again..." , my_name ,
75+ virtual_node_hostname , virtual_node_port , exc_info = err )
76+ reactor .callLater (
77+ simulaqron_settings .conn_retry_time ,
78+ _connect_to_virt_node ,
79+ my_name ,
80+ netqasm_factory ,
81+ virtual_network ,
82+ attempt + 1
83+ )
8284
8385
8486def _setup_netqasm_server (my_name : str , netqasm_factory : NetQASMFactory ):
@@ -120,7 +122,7 @@ def _sigterm_handler(_signo, _stack_frame):
120122 reactor .stop ()
121123
122124
123- def start_qnodeos (node_name : str , network_config_file : Path , network_name : str = "default" , log_level : str = "WARNING" ):
125+ def start_qnodeos (node_name : str , network_config_file : Path , network_name : str ):
124126 """
125127 Start the QNPU that accepts NetQASM subroutines, and sends them as instructions to the SimulaQron virtual node
126128 backend over twisted PB (Native Mode SimulaQron).
@@ -131,8 +133,6 @@ def start_qnodeos(node_name: str, network_config_file: Path, network_name: str =
131133 :type network_config_file: Path
132134 :param network_name: Name of the network (e.g., 'default').
133135 :type network_name: str
134- :param log_level: Logging level (e.g., 'DEBUG', 'INFO', 'WARNING').
135- :type log_level: str
136136 """
137137
138138 # Let's ensure we read the config file
0 commit comments