@@ -83,11 +83,14 @@ def __init__(
83
83
sumo_port : Optional [int ],
84
84
base_params : List [str ],
85
85
sumo_binary : str = "sumo" , # Literal["sumo", "sumo-gui"]
86
+ host : str = "localhost" ,
86
87
):
87
88
self ._sumo_proc = None
88
89
self ._traci_conn = None
89
90
self ._sumo_port = None
90
91
self ._sumo_version : Tuple [int , ...] = tuple ()
92
+ self ._host = host
93
+ self ._log = logging .Logger (self .__class__ .__name__ )
91
94
92
95
if sumo_port is None :
93
96
sumo_port = networking .find_free_port ()
@@ -98,7 +101,7 @@ def __init__(
98
101
* base_params ,
99
102
]
100
103
101
- logging .debug ("Starting sumo process:\n \t %s" , sumo_cmd )
104
+ self . _log .debug ("Starting sumo process:\n \t %s" , sumo_cmd )
102
105
self ._sumo_proc = subprocess .Popen (
103
106
sumo_cmd ,
104
107
stdin = subprocess .PIPE ,
@@ -124,22 +127,23 @@ def connect(
124
127
"""Attempt a connection with the SUMO process."""
125
128
traci_conn = None
126
129
try :
127
- with suppress_output (stderr = not debug , stdout = False ):
130
+ with suppress_output (stderr = not debug , stdout = True ):
128
131
traci_conn = traci .connect (
129
132
self ._sumo_port ,
133
+ host = self ._host ,
130
134
numRetries = max (0 , int (20 * timeout )),
131
135
proc = self ._sumo_proc ,
132
136
waitBetweenRetries = 0.05 ,
133
137
) # SUMO must be ready within timeout seconds
134
138
# We will retry since this is our first sumo command
135
139
except traci .exceptions .FatalTraCIError :
136
- logging .debug ("TraCI could not connect in time." )
140
+ self . _log .debug ("TraCI could not connect in time." )
137
141
raise
138
142
except traci .exceptions .TraCIException :
139
- logging . error ("SUMO process died." )
143
+ self . _log . warning ("SUMO process died." )
140
144
raise
141
145
except ConnectionRefusedError :
142
- logging . error (
146
+ self . _log . warning (
143
147
"Connection refused. Tried to connect to an unpaired TraCI client."
144
148
)
145
149
raise
@@ -155,8 +159,12 @@ def connect(
155
159
) # e.g. "SUMO 1.11.0" -> (1, 11, 0)
156
160
if self ._sumo_version < minimum_sumo_version :
157
161
raise OSError (f"SUMO version must be >= SUMO { minimum_sumo_version } " )
158
- except traci .exceptions .FatalTraCIError as err :
159
- logging .debug ("TraCI disconnected, process may have died." )
162
+ except (traci .exceptions .FatalTraCIError , TypeError ) as err :
163
+ logging .error (
164
+ "TraCI disconnected from '%s:%s', process may have died." ,
165
+ self ._host ,
166
+ self ._sumo_port ,
167
+ )
160
168
# XXX: the error type is changed to TraCIException to make it consistent with the
161
169
# process died case of `traci.connect`.
162
170
raise traci .exceptions .TraCIException (err )
@@ -217,6 +225,7 @@ def __safe_close(conn):
217
225
pass
218
226
219
227
if self ._traci_conn :
228
+ self ._log .debug ("Closing TraCI connection to %s" , self ._sumo_port )
220
229
__safe_close (self ._traci_conn )
221
230
222
231
if self ._sumo_proc :
0 commit comments