Client¤
A Client sends requests to a Service and receives replies. It can also subscribe to messages that are published by the Service.
from msl.network import Client
def captured(data: bytes) -> None:
"""Handle image data that is published by the Service."""
...
with Client(host="192.168.1.25") as client: # (1)!
print(client.services()) # (2)!
camera = client.link("Camera") # (3)!
print(camera.signatures()) # (4)!
camera.subscribe(captured) # (5)!
camera.set_resolution(width=1280, height=720) # (6)!
resolution = camera.get_resolution() # (7)!
# do more stuff...
- Connect to a Broker. By using a context manager (i.e., a with statement), the connection is automatically closed after you are done using it. Otherwise, call disconnect.
- Get the names of the Services that are available to link with.
- Establish a Link with a Service. Tip: The Service does not actually need to be running in order to establish a Link. The Service only needs to be running when a request is sent in order to get a reply.
- Request what functions are available from the linked Service. This will return a mapping of the function name to the input/output parameters that the function requires/returns.
- Subscribe to captured images that are published by the Service.
- Send a request. No reply expected, but the Service still sends
Noneas a reply. - Send a request. Wait for reply.
Client
¤
Client(
*,
host: str = "127.0.0.1",
port: int = BROKER_PORT,
flag: Flag = PICKLE,
curve: AuthCurve | None = None,
plain: AuthPlain | None = None,
xpub_port: int | None = None
)
A Client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
The hostname (or IP address) that the Broker is running on. |
'127.0.0.1'
|
port
|
int
|
The network port that the Broker is running on. |
BROKER_PORT
|
flag
|
Flag
|
The serialisation and compression algorithms to apply to a request before sending the message. |
PICKLE
|
curve
|
AuthCurve | None
|
The CURVE authentication to use. |
None
|
plain
|
AuthPlain | None
|
The PLAIN authentication to use. |
None
|
xpub_port
|
int | None
|
The port on the Broker that is publishing messages.
Typically, this value is |
None
|
Source code in src/msl/network/client.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
flag
instance-attribute
¤
flag: Flag
The serialisation and compression algorithms to apply to a request before sending the message.
disconnect
¤
disconnect() -> None
Close the connection to the Broker.
Source code in src/msl/network/client.py
126 127 128 129 130 131 132 133 134 135 136 137 138 | |
flag_at
¤
Use as a context manager to temporarily change the flag value.
Example
from msl.network import Client, Flag
client = Client(flag=Flag.JSON) # (1)!
link = client.link("MyService")
# uses JSON to serialise the request
link.do_something()
with link.flag_at(Flag.ZSTD): # (2)!
# uses ZSTD to compress the image bytes in the request
link.send_picture(open("image.jpg", "rb").read())
# uses JSON to serialise the request
link.do_something_else(n=100)
client.disconnect()
- Use JSON to serialised each request (no compression).
- Temporarily change the serialisation and compression algorithms to send the request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flag
|
Flag
|
The temporary flag to use while within the context. Once the context exits, the value is reset to the original value. |
required |
Source code in src/msl/network/client.py
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 | |
link
¤
Link with a Service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service_name
|
str
|
required |
Returns:
| Type | Description |
|---|---|
Link
|
The Link instance. |
Source code in src/msl/network/client.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
services
¤
Request the names of the Services that are available from the Broker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
The maximum number of seconds to wait for the reply.
If |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
Source code in src/msl/network/client.py
203 204 205 206 207 208 209 210 211 212 213 | |