pymasep.communication ===================== .. py:module:: pymasep.communication Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/pymasep/communication/base_threaded_communication/index /autoapi/pymasep/communication/client/index /autoapi/pymasep/communication/frame/index /autoapi/pymasep/communication/message/index /autoapi/pymasep/communication/server/index /autoapi/pymasep/communication/socket_handler/index /autoapi/pymasep/communication/socket_handler_queue/index Classes ------- .. autoapisummary:: pymasep.communication.Message pymasep.communication.Frame pymasep.communication.BaseThreadedCommunication pymasep.communication.Server pymasep.communication.Client pymasep.communication.SocketHandler pymasep.communication.SocketHandlerQueue Package Contents ---------------- .. py:class:: 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 :param msg_type: type of message. See above. :param param_dict: Parameters of the message. May be None. :param src_sub_app_id: SubApplication id that send the message. .. py:attribute:: MESSAGE_TYPE_DEFINE :value: 'define' I -> E : Init the initial state .. py:attribute:: MESSAGE_TYPE_OBSERVATION :value: 'observation' E -> I : Observation .. py:attribute:: MESSAGE_TYPE_ACTION :value: 'action' I -> E : Action .. py:attribute:: MESSAGE_TYPE_QUIT_GAME :value: 'quit' I -> E : Game is finished (by user) .. py:attribute:: MESSAGE_TYPE_REGISTER :value: 'register' I -> E : Register interface to engine .. py:attribute:: MESSAGE_TYPE_END_GAME :value: 'end' E -> I : Game is finished (by engine) .. py:attribute:: msg_type type of the message. See MESSAGE_TYPE_* .. py:attribute:: params parameter of the message .. py:attribute:: src_sub_app_id id of the sub application (interface or engine) source of the message .. py:method:: to_xml() Transform the Message to XML. :return: XML version of the Message .. py:method:: from_xml(xml_node) Transform an XML to a Message. The instance must be created before :param xml_node: The XML node containing the Message .. py:method:: __eq__(other) Check if two messages are equals :return: True if two messages are equals .. py:method:: __str__() Transform the message to string containing the XML version of the message :return: A string representing the XML version of the message .. py:method:: to_bytes() Transform the message to bytes :return: The message as bytes .. py:method:: from_bytes(b) Transform bytes to a message. :param b: Bytes string representing XML .. py:class:: Frame(msg) Frame sent at tcp level :param msg: bytes to send .. py:attribute:: size .. py:attribute:: message .. py:method:: __eq__(other) Check if two messages are equals :param other: The other message to compare :return: True if messages are equals .. py:method:: hello_frame(ident) :classmethod: First frame to send at connection .. py:method:: bye_frame() :classmethod: Frame that can be used to close the ReceivedHandler thread .. py:class:: BaseThreadedCommunication(ident, socket_handler_class, socket_handler_args, host, port, log_level) Base class for threaded communication for Client and Server :param ident: string id of the client or server. :param host: Host to listen or connect. :param port: Port to listen or connect. :param socket_handler_class: Class used to handle the socket (send, receive). :param socket_handler_args: Arguments as tuple to instantiate the SocketHandler class. :param log_level: Log level messages to log as str or int. Default is logging.WARNING. .. py:attribute:: id id of the communication thread .. py:attribute:: host host to listen or connect .. py:attribute:: port port to listen or connect .. py:attribute:: socket socket used to listen or connect .. py:attribute:: logger :type: logging.Logger communication logger .. py:attribute:: socket_handler_class class to handle message from/to socket .. py:attribute:: socket_handler_args socket handler arguments .. py:attribute:: _current_connections keep trace of current connections .. py:attribute:: _current_connections_lock lock to handle multiple connections at the same time .. py:method:: add_connection(ident, sh) Add a connection to the client or server :param ident: ID of the opposite BaseThreadCommunication connected to self. :param sh: Instance of the socket handler used to handle this connection .. py:method:: get_connection(ident) Get the socket handler of the connection :param ident: ID of the connection :return: the socket handler .. py:method:: is_connection_registered(ident) Check if a connection is registered in this BaseThreadCommunication. :param ident: ID of the connection :return: True if a socket handler is associated to this connection id .. py:method:: send_frame_to(ident, frame) Send a frame to a connection identified by ident :param ident: ID of the connection to send a frame. :param frame: Frame to send. .. py:class:: Server(ident, max_connection, socket_handler_class, socket_handler_args, host, port, wait_announcement = False, log_level = logging.WARNING) Bases: :py:obj:`pymasep.communication.BaseThreadedCommunication` Base class for Server :param ident: string id of the client. :param host: Host to connect. :param port: Port to connect. :param max_connection: Max number of connections for the server. :param socket_handler_class: Class used to handle the socket (send, receive). Type[ReceiveHandler] :param socket_handler_args: arguments to instantiate the SocketHandler class :param wait_announcement: True if the server is waiting for announcements through Multicast. :param log_level: Log level messages to log as str or int. Default is logging.WARNING. .. py:attribute:: max_connection max number of connection for the server .. py:attribute:: wait_connection_thread thread for handling new connection .. py:method:: __del__() Delete the instance. Close the socket and stop the thread that wait connection and the thread that wait the announcement .. py:method:: wait_connection() start the wait connection thread .. py:method:: stop_wait_connection() stop the wait connection thread .. py:method:: wait_announcement() start the wait announcement thread .. py:method:: stop_wait_announcement() stop the wait announcement thread .. py:class:: Client(ident, socket_handler_class, socket_handler_args, host = None, port = None, log_level = logging.WARNING) Bases: :py:obj:`pymasep.communication.BaseThreadedCommunication` Base class for Client :param ident: string id of the client. :param host: Host to connect. If None, the client will search a server to connect to. :param port: Port to connect. If None, the client will search a server to connect to. :param socket_handler_class: Class used to handle the socket (send, receive). :param socket_handler_args: Arguments to instantiate the SocketHandler class. :param log_level: Log level messages to log as str or int. Default is logging.WARNING. .. py:attribute:: server_id :value: None id of the server to connect to .. py:method:: __del__() Delete the instance. Close the socket .. py:method:: connect() Connect the client to a server. Create the handler and register the connection. .. py:method:: search_server() Search a server on the local net using multicast UPD .. py:class:: SocketHandler(conn, addr, logger) Bases: :py:obj:`threading.Thread` Thread used to handle received messages from conn, addr :param conn: connected socket :param addr: remote address :param logger: logger used to log send and receive message .. py:attribute:: lock lock to send/receive messages one at a time .. py:attribute:: logger logger of communication .. py:attribute:: socket socket to communicate .. py:attribute:: addr remote address .. py:attribute:: send_queue message queue for handling multithreading .. py:method:: __del__() delete the instance. close all sockets .. py:method:: send_frame(frame) send frame through socket :param frame: frame to send .. py:method:: receive_frame() wait for receiving frame from socket :return: received frame .. py:method:: 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 :param frame: Frame to send .. py:method:: on_receive(frame) Callback when a message is received (except for BYE frame). :param frame: Frame received .. py:method:: run() Main thread run that receives and send frames .. py:class:: SocketHandlerQueue(conn, addr, logger, queue) Bases: :py:obj:`pymasep.communication.SocketHandler` Thread used to handle received messages from conn, addr. Put a message in a queue when received :param conn: connected socket :param addr: remote address :param logger: logger used to log send and receive message :param queue: queue where the received messages are put .. py:attribute:: receive_queue the queue used to receive messages .. py:method:: on_receive(frame) Callback when a message is received (except for BYE frame). Put the frame message on the queue :param frame: Frame received