1313# SPDX-License-Identifier: Apache-2.0
1414
1515"""
16- This script defines the Response and ResponseEvent classes,
16+ This script defines the Response and UpdateStateSuccess classes,
1717used for receiving messages from the control interface.
1818
1919Classes
2020--------
2121
2222- Response:
2323 Represents a response from the control interface.
24- - ResponseEvent:
25- Represents an event used to wait for a response.
2624- UpdateStateSuccess:
2725 Represents a response for a successful update state request.
2826
3129
3230- ResponseType:
3331 Enumeration for the different types of response. It includes
34- ERROR, COMPLETE_STATE, and UPDATE_STATE_SUCCESS.
32+ ERROR, COMPLETE_STATE, and UPDATE_STATE_SUCCESS and CONNECTION_CLOSED .
3533
3634Usage
3735------
5654 update_state_success.to_dict()
5755"""
5856
59- __all__ = ["Response" , "ResponseType" , "ResponseEvent" , " UpdateStateSuccess" ]
57+ __all__ = ["Response" , "ResponseType" , "UpdateStateSuccess" ]
6058
6159from typing import Union
62- from threading import Event
6360from enum import Enum
6461from .._protos import _control_api
65- from ..exceptions import ResponseException , ConnectionClosedException
62+ from ..exceptions import ResponseException
6663from ..utils import get_logger
6764from .complete_state import CompleteState
6865from .workload_state import WorkloadInstanceName
@@ -95,7 +92,6 @@ def __init__(self, message_buffer: bytes) -> None:
9592 self .content = None
9693
9794 self ._parse_response ()
98- self ._from_proto ()
9995
10096 def _parse_response (self ) -> None :
10197 """
@@ -115,12 +111,13 @@ def _parse_response(self) -> None:
115111 raise ResponseException (f"Parsing error: '{ e } '" ) from e
116112 if from_ankaios .HasField ("response" ):
117113 self ._response = from_ankaios .response
114+ self ._from_proto ()
115+ elif from_ankaios .HasField ("connectionClosed" ):
116+ self .content_type = ResponseType .CONNECTION_CLOSED
117+ self .content = from_ankaios .connectionClosed .reason
118118 else :
119- logger .error (
120- "Connection closed by the server."
121- )
122- raise ConnectionClosedException (
123- from_ankaios .connectionClosed .reason )
119+ raise ResponseException ( # pragma: no cover
120+ "Invalid response type." )
124121
125122 def _from_proto (self ) -> None :
126123 """
@@ -168,6 +165,8 @@ def get_request_id(self) -> str:
168165 Returns:
169166 str: The request id of the response.
170167 """
168+ if self .content_type == ResponseType .CONNECTION_CLOSED :
169+ return None
171170 return self ._response .requestId
172171
173172 def get_content (self ) -> \
@@ -193,6 +192,8 @@ class ResponseType(Enum):
193192 "(int): Got the complete state."
194193 UPDATE_STATE_SUCCESS = 3
195194 "(int): Got a successful update state response."
195+ CONNECTION_CLOSED = 4
196+ "(int): Connection closed by the server."
196197
197198 def __str__ (self ) -> str :
198199 """
@@ -204,60 +205,6 @@ def __str__(self) -> str:
204205 return self .name .lower ()
205206
206207
207- class ResponseEvent (Event ):
208- """
209- Represents an event that holds a Response object.
210- """
211- def __init__ (self , response : Response = None ) -> None :
212- """
213- Initializes the ResponseEvent with an optional Response object.
214-
215- Args:
216- response Optional(Response): The response to associate with
217- the event. Defaults to None.
218- """
219- super ().__init__ ()
220- self ._response = response
221-
222- def set_response (self , response : Response ) -> None :
223- """
224- Sets the response and triggers the event.
225-
226- Args:
227- response (Response): The response to set.
228- """
229- self ._response = response
230- self .set ()
231-
232- def get_response (self ) -> Response :
233- """
234- Gets the response associated with the event.
235-
236- Returns:
237- Response: The response associated with the event.
238- """
239- return self ._response
240-
241- def wait_for_response (self , timeout : int ) -> Response :
242- """
243- Waits for the response to be set, with a specified timeout.
244-
245- Args:
246- timeout (int): The maximum time to wait for the response,
247- in seconds.
248-
249- Returns:
250- Response: The response associated with the event.
251-
252- Raises:
253- TimeoutError: If the response is not set within the
254- specified timeout.
255- """
256- if not self .wait (timeout ):
257- raise TimeoutError ("Timeout while waiting for the response." )
258- return self .get_response ()
259-
260-
261208class UpdateStateSuccess :
262209 """
263210 Represents an object that holds the added and deleted workloads.
0 commit comments