Skip to content

declearn.communication.build_server

Set up and return a NetworkServer communication endpoint.

Parameters:

Name Type Description Default
protocol str

Name of the communications protocol backend, based on which the Server subclass to instantiate will be retrieved.

required
host str

Host name (e.g. IP address) of the server.

required
port int

Communications port to use.

required
certificate Optional[str]

Path to the server certificate (publickey) to use SSL/TLS communications encryption. If provided, private_key must be set as well.

None
private_key Optional[str]

Path to the server private key to use SSL/TLS communications encryption. If provided, certificate must be set as well.

None
password Optional[str]

Optional password used to access private_key, or path to a file from which to read such a password. If None but a password is needed, an input will be prompted.

None
heartbeat float

Delay (in seconds) between verifications when checking for a message having beend received from or collected by a client.

1.0
logger Union[logging.Logger, str, None]

Logger to use, or name of a logger to set up with declearn.utils.get_logger. If None, use type(server).

None
**kwargs Any

Any valid additional keyword parameter may be passed as well. Refer to the target NetworkServer subclass for details.

{}

Returns:

Name Type Description
server NetworkServer

NetworkServer communication endpoint instance.

Source code in declearn/communication/utils/_build.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
def build_server(
    protocol: str,
    host: str,
    port: int,
    certificate: Optional[str] = None,
    private_key: Optional[str] = None,
    password: Optional[str] = None,
    heartbeat: float = 1.0,
    logger: Union[logging.Logger, str, None] = None,
    **kwargs: Any,
) -> NetworkServer:
    """Set up and return a NetworkServer communication endpoint.

    Parameters
    ----------
    protocol: str
        Name of the communications protocol backend, based on which
        the Server subclass to instantiate will be retrieved.
    host: str
        Host name (e.g. IP address) of the server.
    port: int
        Communications port to use.
    certificate: str or None, default=None
        Path to the server certificate (publickey) to use SSL/TLS
        communications encryption. If provided, `private_key` must
        be set as well.
    private_key: str or None, default=None
        Path to the server private key to use SSL/TLS communications
        encryption. If provided, `certificate` must be set as well.
    password: str or None, default=None
        Optional password used to access `private_key`, or path to a
        file from which to read such a password.
        If None but a password is needed, an input will be prompted.
    heartbeat: float, default=1.0
        Delay (in seconds) between verifications when checking for a
        message having beend received from or collected by a client.
    logger: logging.Logger or str or None, default=None,
        Logger to use, or name of a logger to set up with
        `declearn.utils.get_logger`. If None, use `type(server)`.
    **kwargs:
        Any valid additional keyword parameter may be passed as well.
        Refer to the target `NetworkServer` subclass for details.

    Returns
    -------
    server: NetworkServer
        NetworkServer communication endpoint instance.
    """
    # inherited signature; pylint: disable=too-many-arguments
    protocol = protocol.strip().lower()
    try:
        cls = access_registered(name=protocol, group="NetworkServer")
    except KeyError as exc:  # pragma: no cover
        raise_if_installable(protocol, exc)
        raise KeyError(
            "Failed to retrieve NetworkServer "
            f"class for protocol '{protocol}'."
        ) from exc
    assert issubclass(cls, NetworkServer)  # guaranteed by TypesRegistry
    return cls(
        host=host,
        port=port,
        certificate=certificate,
        private_key=private_key,
        password=password,
        heartbeat=heartbeat,
        logger=logger,
        **kwargs,
    )