4. Communication module¶
The communication module handles the low level communication between the server and the clients.
It provides functions to send and receive a request with arguments to a given socket.
It can start a connection with the server and close a socket. It also keep track of sockets to close them automatically at the end of the program.
4.1. Examples¶
from fand.communication import *
s = connect('myserver.example.com', 9999)
send(s, Request.GET_PWM, 'myshelf1')
req, args = recv(s)
if req == Request.SET_PWM:
print("The fan speed of", args[0], "is", args[1])
else:
print("The server did not answer the expected request")
4.2. Python API¶
4.2.1. Request¶
-
class
fand.communication.Request[source]¶ Bases:
enum.EnumEnumeration of known requests
-
DISCONNECT= 'disconnect'¶ Notification of disconnection
-
GET_PWM= 'get_pwm'¶ Request for a
Request.SET_PWMto get current PWM
-
GET_RPM= 'get_rpm'¶ Request for a
Request.SET_RPMto get current RPM
-
PING= 'ping'¶ Request for a
Request.ACK
-
SET_PWM= 'set_pwm'¶ Give the current PWM
-
SET_PWM_EXPIRE= 'set_pwm_expire'¶ Set the expiration date of the PWM override
-
SET_PWM_OVERRIDE= 'set_pwm_override'¶ Override the PWM value
-
SET_RPM= 'set_rpm'¶ Give the current RPM
-
4.2.2. add_socket¶
-
fand.communication.add_socket(sock: socket.socket) → None[source]¶ Add sock to the set of managed sockets
It can be removed with
reset_connection()and will automatically be whenfand.util.terminate()is called.Parameters: sock – Socket to add Raises: TerminatingError – Trying to add a socket but fand.util.terminating()is True
4.2.3. is_socket_open¶
-
fand.communication.is_socket_open(sock: socket.socket) → bool[source]¶ Returns True if sock is currently managed by this module
This will be False after a socket has been closed with
reset_connection().Parameters: sock – Socket to test
4.2.4. send¶
-
fand.communication.send(sock: socket.socket, request: fand.communication.Request, *args) → None[source]¶ Send a request to a remote socket
Parameters: - sock – Socket to send the request to
- request – Request to send
- args – Request arguments
Raises: - UnpicklableError – Given data cannot be pickled by
pickle - FandTimeoutError – Connection timed out
- SendReceiveError – Error while sending the data
4.2.5. recv¶
-
fand.communication.recv(sock: socket.socket) → Tuple[fand.communication.Request, Tuple][source]¶ Receive a request from a remote socket, returns (request, args)
Parameters: sock – Socket to receive the request and its arguments from
Raises: - FandTimeoutError – Connection timed out
- SendReceiveError – Error while receiving the data
- FandConnectionResetError – No data received or
Request.DISCONNECTreceived - CorruptedDataError – Invalid data received
4.2.6. connect¶
-
fand.communication.connect(address: str, port: int) → socket.socket[source]¶ Connect to server and returns socket
Parameters: - address – Server address
- port – Server port
Raises: - FandTimeoutError – Connection timed out
- ConnectionFailedError – Failed to connect to remote socket