msl.network.manager module
The Network Manager.
- class msl.network.manager.Manager(port, password, login, hostnames, connections_table, users_table, hostnames_table, loop)[source]
Bases:
NetworkThe Network
Manager.Attention
Not to be instantiated directly. Start the Network
Managerfrom the command line. Runmsl-network start --helpfrom a terminal for more information.- async acquire_lock(writer, uid, service, shared)[source]
A request from a
Clientto lock aService.New in version 1.0.
- async new_connection(reader, writer)[source]
Receive a new connection request.
To accept the new connection request, the following checks must be successful:
The correct authentication reply is received.
A correct
identityis received, i.e., is the connection from aClientorService?
- Parameters:
reader (
asyncio.StreamReader) – The stream reader.writer (
asyncio.StreamWriter) – The stream writer.
- async check_user(reader, writer)[source]
Check the login credentials of a user.
- Parameters:
reader (
asyncio.StreamReader) – The stream reader.writer (
asyncio.StreamWriter) – The stream writer.
- Returns:
bool– Whether the login credentials are valid.
- async check_manager_password(reader, writer)[source]
Check the
Manager's password from the connected device.- Parameters:
reader (
asyncio.StreamReader) – The stream reader.writer (
asyncio.StreamWriter) – The stream writer.
- Returns:
bool– Whether the correct password was received.
- async check_identity(reader, writer)[source]
Check the
identityof the connected device.- Parameters:
reader (
asyncio.StreamReader) – The stream reader.writer (
asyncio.StreamWriter) – The stream writer.
- Returns:
strorNone– If the identity check was successful then returns the connection type, either'client'or'service', otherwise returnsNone.
- async get_handshake_data(reader)[source]
Used by
check_manager_password(),check_identity()andcheck_user().- Parameters:
reader (
asyncio.StreamReader) – The stream reader.- Returns:
- async handler(reader, writer)[source]
Handles requests from the connected
Clients and replies or notifications from the connectedServices.- Parameters:
reader (
asyncio.StreamReader) – The stream reader.writer (
asyncio.StreamWriter) – The stream writer.
- async release_lock(writer, uid, service)[source]
A request from a
Clientto unlock aService.New in version 1.0.
- async remove_peer(id_type, writer)[source]
Remove this peer from the registry of connected peers.
- Parameters:
id_type (
str) – The type of the connection, either'client'or'service'.writer (
asyncio.StreamWriter) – The stream writer of the peer.
- async close_writer(writer)[source]
Close the connection to the
asyncio.StreamWriter.Log that the connection is closing, drains the writer and then closes the connection.
- Parameters:
writer (
asyncio.StreamWriter) – The stream writer to close.
- async shutdown_manager()[source]
Disconnect all
Services andClients from theManagerand then shut down theManager.
- async unlink(writer, uid, service)[source]
A request from a
Clientto unlink it from aService.New in version 0.5.
- async write_request(writer, attribute, *args, **kwargs)[source]
Write a request to a
Clientor to aService.- Parameters:
writer (
asyncio.StreamWriter) – The stream writer of theClientorService.attribute (
str) – The name of the attribute to request.args – The arguments that attribute requires.
kwargs – The key-value pairs that attribute requires.
- class msl.network.manager.Peer(writer)[source]
Bases:
objectMetadata about a peer that is connected to the Network
Manager.Attention
Not to be called directly. To be called when the Network
Managerreceives anew_connection()request.- Parameters:
writer (
asyncio.StreamWriter) – The stream writer for the peer.
- msl.network.manager.run_forever(*, host=None, port=1875, auth_hostname=False, auth_login=False, auth_password=None, database=None, disable_tls=False, cert_file=None, key_file=None, key_file_password=None, log_level='INFO', log_file=None)[source]
Start the event loop for the Network
Manager.This is a blocking function. It will not return until the event loop of the
Managerhas stopped.New in version 0.4.
Changed in version 1.0: Renamed certfile to cert_file. Renamed keyfile to key_file. Renamed keyfile_password to key_file_password. Renamed logfile to log_file. Removed the debug keyword argument. Added the log_level keyword argument. Added the host keyword argument.
- Parameters:
host (
str, optional) – The hostname or IP address to run the NetworkManageron. If unspecified then all network interfaces are used.port (
int, optional) – The port number to run the NetworkManageron.auth_hostname (
bool, optional) – IfTruethen only connections from trusted hosts are allowed. If enabling auth_hostname then do not specify an auth_password and do not enable auth_login. Runmsl-network hostname --helpfor more details.auth_login (
bool, optional) – IfTruethen checks a users login credentials (the username and password) before aClientorServicesuccessfully connects. If enabling auth_login then do not specify an auth_password and do not enable auth_hostname. Runmsl-network user --helpfor more details.auth_password (
str, optional) – The password of the NetworkManager. Essentially, this can be a thought of as a single password that allClients andServices need to specify before the connection to the NetworkManageris successful. Can be a path to a file that contains the password on the first line in the file (WARNING!! if the path does not exist then the value of the path becomes the password). If using an auth_password then do not enable auth_login nor auth_hostname.database (
str, optional) – The path to the sqlite3 database that contains the records for the following tables –ConnectionsTable,HostnamesTable,UsersTable. IfNonethen loads the default database.disable_tls (
bool, optional) – Whether to use TLS for the communication protocol.cert_file (
str, optional) – The path to the TLS certificate file. Seeload_cert_chain()for more details. Only required if using TLS.key_file (
str, optional) – The path to the TLS key file. Seeload_cert_chain()for more details.key_file_password (
str, optional) – The password to decrypt the key_file. Seeload_cert_chain()for more details. Can be a path to a file that contains the password on the first line in the file (WARNING!! if the path does not exist then the value of the path becomes the password).log_level (
strorint, optional) – The logging level to initially use. Can also be changed via anadmin_request().log_file (
str, optional) – The file path to write logging messages to. IfNonethen uses the default file path.
- msl.network.manager.run_services(*services, **kwargs)[source]
This function starts the Network
Managerand then starts the specifiedServices.This is a convenience function for running the Network
Manageronly when the specifiedServices are all connected to theManager. Once allServices disconnect from theManagerthen theManagershuts down.This is a blocking call. It will not return until the event loop of the
Managerhas stopped.New in version 0.4.
- Parameters:
services – The
Services to run on theManager. EachServicemust be instantiated but not started. Thisrun_services()function will start eachService.kwargs – Keyword arguments are passed to
run_forever()and tostart(). The keyword arguments that are passed torun_forever()andstart()that are not valid for that function are silently ignored.
Examples
If you want to allow a
Clientto be able to shut down aServicethen implement a publicshutdown_service()method on theService. For example, the followingshutdownable_example.pyis a script that starts a NetworkManagerand twoServices# shutdownable_example.py from msl.network import Service, run_services class AddService(Service): def add(self, a, b): return a + b def shutdown_service(self, *args, **kwargs): # do whatever you need to do before the AddService shuts down # return whatever you want return True class SubtractService(Service): def subtract(self, a, b): return a - b def shutdown_service(self, *args, **kwargs): # do whatever you need to do before the SubtractService shuts down # return whatever you want return 'Success!' run_services(AddService(), SubtractService())
Then the
Clientscript could befrom msl.network import connect cxn = connect() a = cxn.link('AddService') s = cxn.link('SubtractService') assert a.add(1, 2) == 3 assert s.subtract(1, 2) == -1 a.shutdown_service() s.shutdown_service()
When both
Services have shut down then the NetworkManagerwill also shut down and therun_services()function will no longer be blocking the execution ofshutdownable_example.py.
- msl.network.manager.filter_run_forever_kwargs(**kwargs)[source]
From the specified keyword arguments only return those that are valid for
run_forever().New in version 0.4.
- Parameters:
kwargs – All keyword arguments that are not part of the function signature for
run_forever()are silently ignored and are not included in the output.- Returns:
dict– Valid keyword arguments that can be passed torun_forever().