# Copyright 2025 Flower Labs GmbH. All Rights Reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# =============================================================================="""Flower simulation connection compatibility helper."""fromloggingimportDEBUG,WARNINGfromtypingimportcastfromflwr.supercoreimportlogfromflwr.supercore.constantimportSUPERLINK_DEFAULT_CLIENT_ADDRESSfromflwr.supercore.interceptorsimport(RuntimeTokenHttpInterceptor,RuntimeVersionHttpInterceptor,)fromflwr.supercore.retryimportmake_simple_http_retry_invokerfromflwr.supercore.runtimeimportRuntimeHttpClient
[문서]classSimulationIoConnection:"""`SimulationIoConnection` provides an interface to the Runtime API. Parameters ---------- runtime_api_address : str (default: "127.0.0.1:8000") The address (URL, IPv6, IPv4) of the SuperLink Runtime API service. insecure : bool (default: False) If True, use plaintext (TLS disabled). If False, use TLS. root_certificates : Optional[bytes] (default: None) The PEM-encoded root certificates as a byte string. Used only when `insecure` is False. If provided, these certificates are used to verify the server certificate. If None, HTTPX's default trusted CA bundle is used. token : str Executor token attached to all outgoing RPCs via metadata. """def__init__(# pylint: disable=too-many-argumentsself,runtime_api_address:str=SUPERLINK_DEFAULT_CLIENT_ADDRESS,insecure:bool=False,root_certificates:bytes|None=None,*,token:str,)->None:iftoken=="":raiseValueError("`token` must be a non-empty string")self._addr=runtime_api_addressself._insecure=insecureself._cert=root_certificatesself._token=tokenself._client:RuntimeHttpClient|None=Noneself._retry_invoker=make_simple_http_retry_invoker()@propertydef_is_connected(self)->bool:"""Check if connected to the Runtime API server."""returnself._clientisnotNone@propertydef_stub(self)->RuntimeHttpClient:"""Runtime API client."""ifnotself._is_connected:self._connect()returncast(RuntimeHttpClient,self._client)def_connect(self)->None:"""Connect to the Runtime API."""ifself._is_connected:log(WARNING,"Already connected")returnself._client=RuntimeHttpClient.from_server_address(server_address=self._addr,insecure=self._insecure,root_certificates=self._cert,interceptors=[RuntimeVersionHttpInterceptor(component_name="flwr-simulation"),RuntimeTokenHttpInterceptor(token=self._token),],retry_invoker=self._retry_invoker,)log(DEBUG,"[Runtime] Connected to %s",self._addr)def_disconnect(self)->None:"""Disconnect from the Runtime API."""ifnotself._is_connected:log(DEBUG,"Already disconnected")returnclient=cast(RuntimeHttpClient,self._client)self._client=Noneclient.close()log(DEBUG,"[Runtime] Disconnected")