Source code for flwr.simulation.simulationio_connection
# 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,WARNINGfromtypingimportcastimportgrpcfromflwr.common.constantimportSERVERAPPIO_API_DEFAULT_CLIENT_ADDRESSfromflwr.common.loggerimportlogfromflwr.proto.serverappio_pb2_grpcimportServerAppIoStub# pylint: disable=E0611fromflwr.supercore.grpcimportcreate_channel,on_channel_state_changefromflwr.supercore.interceptorsimport(AppIoTokenClientInterceptor,RuntimeVersionClientInterceptor,)fromflwr.supercore.retryimportmake_simple_grpc_retry_invoker,wrap_stub
[docs]classSimulationIoConnection:"""`SimulationIoConnection` provides an interface to the ServerAppIo API. Parameters ---------- serverappio_api_address : str (default: "127.0.0.1:9091") The address (URL, IPv6, IPv4) of the SuperLink ServerAppIo 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, gRPC default root certificates are used. token : str Executor token attached to all outgoing RPCs via metadata. """def__init__(# pylint: disable=too-many-argumentsself,serverappio_api_address:str=SERVERAPPIO_API_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=serverappio_api_addressself._insecure=insecureself._cert=root_certificatesself._token=tokenself._grpc_stub:ServerAppIoStub|None=Noneself._channel:grpc.Channel|None=Noneself._retry_invoker=make_simple_grpc_retry_invoker()@propertydef_is_connected(self)->bool:"""Check if connected to the ServerAppIo API server."""returnself._channelisnotNone@propertydef_stub(self)->ServerAppIoStub:"""ServerAppIo stub."""ifnotself._is_connected:self._connect()returncast(ServerAppIoStub,self._grpc_stub)def_connect(self)->None:"""Connect to the ServerAppIo API."""ifself._is_connected:log(WARNING,"Already connected")returnself._channel=create_channel(server_address=self._addr,insecure=self._insecure,root_certificates=self._cert,interceptors=[RuntimeVersionClientInterceptor(component_name="flwr-simulation"),AppIoTokenClientInterceptor(token=self._token),],)self._channel.subscribe(on_channel_state_change)self._grpc_stub=ServerAppIoStub(self._channel)wrap_stub(self._grpc_stub,self._retry_invoker)log(DEBUG,"[ServerAppIO] Connected to %s",self._addr)def_disconnect(self)->None:"""Disconnect from the ServerAppIo API."""ifnotself._is_connected:log(DEBUG,"Already disconnected")returnchannel:grpc.Channel=self._channelself._channel=Noneself._grpc_stub=Nonechannel.close()log(DEBUG,"[ServerAppIO] Disconnected")