pymasep.communication
Submodules
Classes
Object send between thread or through network. |
|
Frame sent at tcp level |
|
Base class for threaded communication for Client and Server |
|
Base class for Server |
|
Base class for Client |
|
Thread used to handle received messages from conn, addr |
|
Thread used to handle received messages from conn, addr. |
Package Contents
- class pymasep.communication.Message(msg_type, param_dict, src_sub_app_id)
Object send between thread or through network.
DEFINE: Define the game used by the engine.
Param: ‘game’: name of the game class (doted path)
REGISTER: Register the interface.
Param: ‘id’: id of the interface, ‘queue_id’: id of the queue used by interface to receive messages, ‘role’: ‘InterfaceInfo.ROLE_OBSERVER’ interface is only observer, ‘InterfaceInfo.ROLE_ACTOR’ interface is actor
OBSERVATION: Observation sent by engine to interface,
Param: ‘observation’: State object, ‘additional_information’: dictionary containing additional information. Ex: ‘game_log’, ‘reward’
ACTION: Action sent by interface to engine
Param: ‘action’: Action object
END_GAME: Game is finished (by engine)
Param : None
QUI_GAME: Game is finished (by user)
Param : None
- Parameters:
msg_type (str) – type of message. See above.
param_dict (Optional[dict]) – Parameters of the message. May be None.
src_sub_app_id – SubApplication id that send the message.
- MESSAGE_TYPE_DEFINE = 'define'
I -> E : Init the initial state
- MESSAGE_TYPE_OBSERVATION = 'observation'
E -> I : Observation
- MESSAGE_TYPE_ACTION = 'action'
I -> E : Action
- MESSAGE_TYPE_QUIT_GAME = 'quit'
I -> E : Game is finished (by user)
- MESSAGE_TYPE_REGISTER = 'register'
I -> E : Register interface to engine
- MESSAGE_TYPE_END_GAME = 'end'
E -> I : Game is finished (by engine)
- msg_type
type of the message. See MESSAGE_TYPE_*
- params
parameter of the message
- src_sub_app_id
id of the sub application (interface or engine) source of the message
- to_xml()
Transform the Message to XML.
- Returns:
XML version of the Message
- Return type:
xml.etree.ElementTree.Element
- from_xml(xml_node)
Transform an XML to a Message. The instance must be created before
- Parameters:
xml_node (xml.etree.ElementTree.Element) – The XML node containing the Message
- Return type:
None
- __eq__(other)
Check if two messages are equals
- Returns:
True if two messages are equals
- Return type:
bool
- __str__()
Transform the message to string containing the XML version of the message
- Returns:
A string representing the XML version of the message
- Return type:
str
- to_bytes()
Transform the message to bytes
- Returns:
The message as bytes
- Return type:
bytes
- from_bytes(b)
Transform bytes to a message.
- Parameters:
b (bytes) – Bytes string representing XML
- Return type:
None
- class pymasep.communication.Frame(msg)
Frame sent at tcp level
- Parameters:
msg (bytes) – bytes to send
- size
- message
- __eq__(other)
Check if two messages are equals
- Parameters:
other – The other message to compare
- Returns:
True if messages are equals
- classmethod hello_frame(ident)
First frame to send at connection
- Parameters:
ident (str)
- classmethod bye_frame()
Frame that can be used to close the ReceivedHandler thread
- class pymasep.communication.BaseThreadedCommunication(ident, socket_handler_class, socket_handler_args, host, port, log_level)
Base class for threaded communication for Client and Server
- Parameters:
ident (str) – string id of the client or server.
host (str) – Host to listen or connect.
port (int) – Port to listen or connect.
socket_handler_class (Type[Any]) – Class used to handle the socket (send, receive).
socket_handler_args (tuple) – Arguments as tuple to instantiate the SocketHandler class.
log_level (Union[str, int]) – Log level messages to log as str or int. Default is logging.WARNING.
- id
id of the communication thread
- host
host to listen or connect
- port
port to listen or connect
- socket
socket used to listen or connect
- logger: logging.Logger
communication logger
- socket_handler_class
class to handle message from/to socket
- socket_handler_args
socket handler arguments
- _current_connections
keep trace of current connections
- _current_connections_lock
lock to handle multiple connections at the same time
- add_connection(ident, sh)
Add a connection to the client or server
- Parameters:
ident (str) – ID of the opposite BaseThreadCommunication connected to self.
sh – Instance of the socket handler used to handle this connection
- Return type:
None
- get_connection(ident)
Get the socket handler of the connection
- Parameters:
ident – ID of the connection
- Returns:
the socket handler
- is_connection_registered(ident)
Check if a connection is registered in this BaseThreadCommunication.
- Parameters:
ident – ID of the connection
- Returns:
True if a socket handler is associated to this connection id
- send_frame_to(ident, frame)
Send a frame to a connection identified by ident
- Parameters:
ident (str) – ID of the connection to send a frame.
frame – Frame to send.
- class pymasep.communication.Server(ident, max_connection, socket_handler_class, socket_handler_args, host, port, wait_announcement=False, log_level=logging.WARNING)
Bases:
pymasep.communication.BaseThreadedCommunicationBase class for Server
- Parameters:
ident (str) – string id of the client.
host (str) – Host to connect.
port (int) – Port to connect.
max_connection (int) – Max number of connections for the server.
socket_handler_class (Type[Any]) – Class used to handle the socket (send, receive). Type[ReceiveHandler]
socket_handler_args (tuple) – arguments to instantiate the SocketHandler class
wait_announcement (bool) – True if the server is waiting for announcements through Multicast.
log_level (Union[str, int]) – Log level messages to log as str or int. Default is logging.WARNING.
- max_connection
max number of connection for the server
- wait_connection_thread
thread for handling new connection
- __del__()
Delete the instance. Close the socket and stop the thread that wait connection and the thread that wait the announcement
- wait_connection()
start the wait connection thread
- stop_wait_connection()
stop the wait connection thread
- wait_announcement()
start the wait announcement thread
- stop_wait_announcement()
stop the wait announcement thread
- class pymasep.communication.Client(ident, socket_handler_class, socket_handler_args, host=None, port=None, log_level=logging.WARNING)
Bases:
pymasep.communication.BaseThreadedCommunicationBase class for Client
- Parameters:
ident (str) – string id of the client.
host (str) – Host to connect. If None, the client will search a server to connect to.
port (int) – Port to connect. If None, the client will search a server to connect to.
socket_handler_class (Type[Any]) – Class used to handle the socket (send, receive).
socket_handler_args (tuple) – Arguments to instantiate the SocketHandler class.
log_level (Union[str, int]) – Log level messages to log as str or int. Default is logging.WARNING.
- server_id = None
id of the server to connect to
- __del__()
Delete the instance. Close the socket
- connect()
Connect the client to a server. Create the handler and register the connection.
- search_server()
Search a server on the local net using multicast UPD
- class pymasep.communication.SocketHandler(conn, addr, logger)
Bases:
threading.ThreadThread used to handle received messages from conn, addr
- Parameters:
conn – connected socket
addr – remote address
logger (logging.Logger) – logger used to log send and receive message
- lock
lock to send/receive messages one at a time
- logger
logger of communication
- socket
socket to communicate
- addr
remote address
- send_queue
message queue for handling multithreading
- __del__()
delete the instance. close all sockets
- send_frame(frame)
send frame through socket
- Parameters:
frame – frame to send
- Return type:
None
- receive_frame()
wait for receiving frame from socket
- Returns:
received frame
- Return type:
- send_frame_async(frame)
Send a from asynchronously. Use the internal queue and socket as described here: https://stackoverflow.com/questions/51104534/python-socket-receive-send-multi-threading
- Parameters:
frame – Frame to send
- on_receive(frame)
Callback when a message is received (except for BYE frame).
- Parameters:
frame – Frame received
- run()
Main thread run that receives and send frames
- class pymasep.communication.SocketHandlerQueue(conn, addr, logger, queue)
Bases:
pymasep.communication.SocketHandlerThread used to handle received messages from conn, addr. Put a message in a queue when received
- Parameters:
conn – connected socket
addr – remote address
logger (logging.Logger) – logger used to log send and receive message
queue – queue where the received messages are put
- receive_queue
the queue used to receive messages
- on_receive(frame)
Callback when a message is received (except for BYE frame). Put the frame message on the queue
- Parameters:
frame – Frame received