socktools.meta_sock - one socket to rule them all¶
This module provides one socket class to rule them all, one socket class to bring them all and in the meta socket bind them.
Tolkien aside, a meta socket is just a socket class that can add other sockets and respond to messages from all the added sockets in one place.
Each individual socket will also respond to the individual messages unless the meta socket is in override mode.
If you’re feeling crazy you can also nest meta sockets inside other meta sockets.
The intended application of this module is enabling things like UDP+TCP at the same time, or websocket+IRC etc.
-
class
socktools.meta_sock.MetaSock(override_mode=False, child_sockets=[], **kwargs)[source]¶ Bases:
socktools.base_sock.BaseSockOne socket to rule them all
A meta socket is a socket object that has a collection of child sockets from which it receives messages in pre-parsed format.
Keyword Arguments: - override_mode (bool) – if True, the child sockets will not process messages and instead defer to the meta socket
- child_sockets (list) – a list of child sockets to add at startup
-
add_msg(child_sock, from_addr, msg_type, msg_data)[source]¶ Add a message to in_q
The child sockets call this to add messages to the meta socket’s in_q for handling.
Parameters: - child_sock (base_sock.BaseSock) – the child socket this message came from - useful if we need to reply to the message
- from_addr (tuple) – the TCP/IP endpoint as seen by the child socket
- msg_type – the message type
- msg_data – the actual message data
-
get_default_child_sockets()[source]¶ Return a list of default child sockets
This method is provided for the sake of a uniform API, in most cases you can do what you need to without subclassing.
In the default implementation this returns a 0-length list.
-
handler_thread()[source]¶ Async invoke handlers - used internally
This is near identical to the implementation in base_sock.BaseSock() except that handlers accept a child_sock param, so we need to account for that.
- Todo:
- refactor this so we’re not duplicating code
-
handler_wrapper(handler, child_sock, addr, msg_type, msg_data)[source]¶ Invokes the specified handler while catching exceptions
Copied straight from base_sock.BaseSock, does the same except for adding the child_sock param
Parameters: - handler (function) – a function accepting params (addr,msg_type,msg_data)
- child_sock (base_sock.BaseSock) – the child socket this message originated from
- addr (tuple) – TCP/IP endpoint for the peer that originated the message
- msg_type – the message type - this depends on the application but usually an int
- msg_data – the message data - this depends on the application but usually a tuple or dict
-
send_msg(msg_type, msg_data, to_peer=None)[source]¶ Broadcasts a message to all child sockets
It is up to the child socket classes to properly encode the message.
The to_peer param is ignored and present only for compatiblity reasons.
-
send_raw(data, to_peer=None)[source]¶ Broadcast a raw message to all child sockets
As with send_msg(), to_peer is for compatiblity reasons only and is ignored.
Warning
It is advised to use send_msg() instead of this method due to the possiblity of underlying protocol differences