1313# SPDX-License-Identifier: Apache-2.0
1414
1515"""
16- This script defines the GrpcConnection class, implementing the
16+ This script defines the CommandInterfaceConnection class, implementing the
1717Connection abstraction over a direct gRPC connection to the Ankaios
1818server, used to connect to Ankaios from outside a workload.
1919
2020Classes
2121-------
2222
23- - :class:`GrpcConnection `:
23+ - :class:`CommandInterfaceConnection `:
2424 Handles the interaction with Ankaios over gRPC.
2525
2626Enums
2727-----
2828
29- - :class:`GrpcConnectionState `:
29+ - :class:`CommandInterfaceState `:
3030 Represents the state of the gRPC connection.
3131
3232Usage
3333-----
3434
35- - Create a GrpcConnection instance, connect and disconnect.
35+ - Create a CommandInterfaceConnection instance, connect and disconnect.
3636 .. code-block:: python
3737
38- conn = GrpcConnection (
38+ conn = CommandInterfaceConnection (
3939 "http://127.0.0.1:25551", <callbacks from Ankaios>
4040 )
4141 conn.connect()
4444"""
4545
4646
47- __all__ = ["GrpcConnection " , "GrpcConnectionState " ]
47+ __all__ = ["CommandInterfaceConnection " , "CommandInterfaceState " ]
4848
4949
5050import queue
6464from .connection import Connection
6565
6666
67- class GrpcConnectionState (Enum ):
67+ class CommandInterfaceState (Enum ):
6868 """The state of the gRPC connection."""
6969
7070 INITIALIZED = 1
@@ -87,11 +87,11 @@ def __str__(self) -> str:
8787
8888
8989# pylint: disable=too-many-instance-attributes
90- class GrpcConnection (Connection ):
90+ class CommandInterfaceConnection (Connection ):
9191 """
92- This class handles the interaction with an Ankaios server over a
93- direct gRPC connection, playing the commander role via the
94- CommandConnection service.
92+ This class handles the interaction with an Ankaios server over the
93+ command interface: a direct gRPC connection to the server,
94+ playing the commander role via the CommandConnection service.
9595
9696 The initial :func:`connect` attempt is never retried. Once a
9797 connection has been established, losing it is treated as
@@ -119,8 +119,8 @@ def __init__(
119119 key_pem : Optional [str ] = None ,
120120 ) -> None :
121121 """
122- Initialize the GrpcConnection object. This is used to
123- interact with an Ankaios server directly over gRPC .
122+ Initialize the CommandInterfaceConnection object. This is used to
123+ interact with an Ankaios server directly over the command interface .
124124
125125 If none of ca_pem, crt_pem and key_pem are provided, the
126126 connection is a plaintext (insecure) one. If all three are
@@ -170,31 +170,31 @@ def __init__(
170170 self ._key_pem = key_pem
171171
172172 # The state of the connection must not be changed directly.
173- self ._state_value = GrpcConnectionState .TERMINATED
173+ self ._state_value = CommandInterfaceState .TERMINATED
174174 self ._state_lock = threading .Lock ()
175175 self ._channel : Optional [grpc .Channel ] = None
176176 self ._call = None
177177 self ._write_queue : "queue.Queue" = queue .Queue ()
178178 self ._reader_thread : Optional [threading .Thread ] = None
179179
180180 @property
181- def _state (self ) -> GrpcConnectionState :
181+ def _state (self ) -> CommandInterfaceState :
182182 """
183183 Get the current state of the connection.
184184
185185 :returns: The current state.
186- :rtype: GrpcConnectionState
186+ :rtype: CommandInterfaceState
187187 """
188188 with self ._state_lock :
189189 return self ._state_value
190190
191191 @_state .setter
192- def _state (self , value : GrpcConnectionState ) -> None :
192+ def _state (self , value : CommandInterfaceState ) -> None :
193193 """
194194 Set the current state of the connection.
195195
196196 :param value: The new state to set.
197- :type value: GrpcConnectionState
197+ :type value: CommandInterfaceState
198198 """
199199 with self ._state_lock :
200200 self ._state_value = value
@@ -207,7 +207,7 @@ def connected(self) -> bool:
207207 :returns: True if connected, False otherwise.
208208 :rtype: bool
209209 """
210- return self ._state == GrpcConnectionState .CONNECTED
210+ return self ._state == CommandInterfaceState .CONNECTED
211211
212212 def connect (self ) -> None :
213213 """
@@ -221,35 +221,35 @@ def connect(self) -> None:
221221 the connection could not be established.
222222 """
223223 if self ._state in (
224- GrpcConnectionState .INITIALIZED ,
225- GrpcConnectionState .CONNECTED ,
226- GrpcConnectionState .RECONNECTING ,
224+ CommandInterfaceState .INITIALIZED ,
225+ CommandInterfaceState .CONNECTED ,
226+ CommandInterfaceState .RECONNECTING ,
227227 ):
228228 raise ConnectionException ("Already connected." )
229229
230230 # Only change the state once past the point where connecting
231231 # can still fail, so a failed attempt leaves the connection
232232 # exactly as it was and free to retry via a plain connect().
233233 call = self ._open_stream ()
234- self ._state = GrpcConnectionState .INITIALIZED
234+ self ._state = CommandInterfaceState .INITIALIZED
235235
236236 self ._reader_thread = threading .Thread (
237237 target = self ._read_from_grpc , args = (call ,), daemon = True
238238 )
239239 self ._reader_thread .start ()
240- self ._state = GrpcConnectionState .CONNECTED
240+ self ._state = CommandInterfaceState .CONNECTED
241241 self ._logger .debug ("Connected to the Ankaios server over gRPC." )
242242
243243 def disconnect (self ) -> None :
244244 """
245245 Disconnect from the gRPC connection.
246246 """
247- if self ._state == GrpcConnectionState .TERMINATED :
247+ if self ._state == CommandInterfaceState .TERMINATED :
248248 self ._logger .debug ("Already disconnected." )
249249 return
250250
251251 self ._logger .debug ("Disconnecting.." )
252- self ._state = GrpcConnectionState .TERMINATED
252+ self ._state = CommandInterfaceState .TERMINATED
253253 if self ._call is not None :
254254 self ._call .cancel ()
255255 if self ._reader_thread is not None :
@@ -271,7 +271,7 @@ def write_request(self, request: Request) -> None:
271271
272272 :raises ConnectionException: If not connected.
273273 """
274- if self ._state != GrpcConnectionState .CONNECTED :
274+ if self ._state != CommandInterfaceState .CONNECTED :
275275 self ._logger .error (
276276 "Could not write to the gRPC connection, not connected."
277277 )
@@ -384,7 +384,7 @@ def _read_from_grpc(self, call) -> None:
384384 for from_server in call :
385385 self ._handle_from_server (from_server )
386386 except grpc .RpcError as e :
387- if self ._state == GrpcConnectionState .TERMINATED :
387+ if self ._state == CommandInterfaceState .TERMINATED :
388388 # disconnect() already cancelled the call itself;
389389 # this is the expected, self-inflicted result.
390390 self ._logger .debug (
@@ -397,10 +397,10 @@ def _read_from_grpc(self, call) -> None:
397397 e ,
398398 )
399399
400- if self ._state == GrpcConnectionState .TERMINATED :
400+ if self ._state == CommandInterfaceState .TERMINATED :
401401 return
402402
403- self ._state = GrpcConnectionState .RECONNECTING
403+ self ._state = CommandInterfaceState .RECONNECTING
404404 self ._logger .warning (
405405 "Lost connection to the Ankaios server, attempting to "
406406 "reconnect every %ss.." ,
@@ -421,14 +421,14 @@ def _reconnect(self):
421421 """
422422 while True :
423423 time .sleep (self .RECONNECT_INTERVAL )
424- if self ._state == GrpcConnectionState .TERMINATED :
424+ if self ._state == CommandInterfaceState .TERMINATED :
425425 return None
426426 try :
427427 call = self ._open_stream ()
428428 except ConnectionException as e :
429429 self ._logger .debug ("Reconnect attempt failed: '%s'" , e )
430430 continue
431- self ._state = GrpcConnectionState .CONNECTED
431+ self ._state = CommandInterfaceState .CONNECTED
432432 self ._logger .info ("Reconnected to the Ankaios server." )
433433 return call
434434
0 commit comments