Usage
Using MSL-Network requires a sequence of 3 steps:
Start the Network Manager
The first thing to do is to start the Network Manager
.
There are 3 ways to do this.
From a terminal run:
msl-network start
Running this command will automatically perform the following default actions:
create a private 2048-bit, RSA key
create a self-signed certificate using the private key
create an SQLite database to store information that is used by the Network
Manager
start the Network
Manager
on the default port using the TLS protocolno authentication is required for
Client
's andService
's to connect to theManager
You can override the default actions, for example, use Elliptic-Curve Cryptography rather than RSA or only allow certain users to be able to connect to the
Manager
. For more details refer to the help that is available from the command linemsl-network --help msl-network start --help
Call
run_forever()
in a script.Call
run_services()
in a script. This method also starts theService
's immediately after theManager
starts.
Start a Service on the Network Manager
In order to create a new Service just create a class that is a subclass of
Service
and call the
start()
method.
BasicMath Service
For example, the BasicMath Service is a simple (and terribly inefficient)
Service
that performs some basic math operations
and it is included with MSL-Network.
To start the BasicMath Service on the Manager
that is running on the same computer, run the following
command in a terminal
python -c "from msl.examples.network import BasicMath; BasicMath().start()"
Note
The reason for adding the time.sleep()
functions in the BasicMath Service
will become evident when discussing Asynchronous Programming.
Connect to the Network Manager as a Client
Now that there is a BasicMath Service running on the Network Manager
(which are both running on the same computer that the Client
will be), we can
connect()
to the Network Manager
>>> from msl.network import connect
>>> cxn = connect(name='MyClient')
establish a link with the BasicMath Service
>>> bm = cxn.link('BasicMath')
and send a request to the BasicMath Service
>>> bm.add(1, 2)
3
See the Asynchronous Programming section for an example on how to send requests asynchronously.
To find out what devices are currently connected to the Manager
, execute
>>> print(cxn.identities(as_string=True))
Manager[localhost:1875]
attributes:
identity() -> dict
link(service: str) -> bool
language: Python 3.9.7
os: Windows 10 AMD64
Clients [1]:
MyClient[localhost:63818]
language: Python 3.9.7
os: Windows 10 AMD64
Services [1]:
BasicMath[localhost:63815]
attributes:
add(x: Union[int, float], y: Union[int, float]) -> Union[int, float]
divide(x: Union[int, float], y: Union[int, float]) -> Union[int, float]
ensure_positive(x: Union[int, float]) -> bool
euler() -> 2.718281828459045
multiply(x: Union[int, float], y: Union[int, float]) -> Union[int, float]
pi() -> 3.141592653589793
power(x: Union[int, float], n=2) -> Union[int, float]
set_logging_level(level: Union[str, int]) -> bool
subtract(x: Union[int, float], y: Union[int, float]) -> Union[int, float]
language: Python 3.9.7
max_clients: -1
os: Windows 10 AMD64
If as_string=False
, which is the default boolean value, then the returned value would be a
dict
, rather than a str
, containing the same information.
To disconnect from the Manager
, execute
>>> cxn.disconnect()
If you only wanted to connect to the BasicMath Service (and no other
Service
s on the Manager
)
then you could create a LinkedClient
>>> from msl.network import LinkedClient
>>> bm = LinkedClient('BasicMath')
>>> bm.add(1, 2)
3
>>> bm.disconnect()