wasp_general.network.beacon package

Submodules

wasp_general.network.beacon.beacon module

class wasp_general.network.beacon.beacon.WBeaconConfig(config=None, config_section=None)[source]

Bases: object

Abstract class that represent service discovery beacon configuration

config()[source]

Return beacon configuration

Returns:WConfig
class wasp_general.network.beacon.beacon.WBeaconHandler(config, io_handler, server_mode, transport=None)[source]

Bases: wasp_general.network.service.WNativeSocketHandler

Beacon’s loop-handler. Is capable to create required transport (that is specified in the beacon’s configuration) Depends on configuration value, the following classes is used:

‘broadcast’ (default) - WBroadcastBeaconTransport ‘multicast’ - WMulticastBeaconTransport ‘unicast_udp’ - not implemented yet ‘unicast_tcp’ - not implemented yet
setup_handler(io_loop)[source]

WIOLoopServiceHandler.setup_handler() implementation. When this object is in ‘non-server mode’ (client mode), then beacon message is sent

class wasp_general.network.beacon.beacon.WBeaconIOHandler(config, messenger=None, callback=None)[source]

Bases: wasp_general.network.service.WNativeSocketDirectIOHandler

Basic beacon io-handler.

callback()[source]

Return handler`s callback

Returns:WNetworkBeaconCallback (or None)
config()[source]

Return handler`s configuration

Returns:WConfig
max_size()[source]

Return maximum message size. (Minimum between this class ‘message_maxsize’ value and messengers ‘message_maxsize’ value)

Returns:int
message_maxsize = 1024

Network messages maximum size

messenger()[source]

Return beacon messenger

Returns:WBeaconMessenger
class wasp_general.network.beacon.beacon.WNetworkBeaconBase(config, io_handler, server_mode, transport, timeout=None)[source]

Bases: wasp_general.network.service.WIOLoopService

Represent service discovery beacon that works over the network. This beacon doesn’t interact with network services like zeroconf, but instead it does all the network discovery work itself. The real work is done in WNetworkServerBeacon and WNetworkClientBeacon classes.

This service automatically creates WBeaconHandler object with the specified io-handler.

class wasp_general.network.beacon.beacon.WNetworkBeaconCallback[source]

Bases: object

Abstract class that represent network beacon callback. Helps to interact with clients or servers. After beacons message received this is the place where all the beacon logic happens.

class WDataDescription[source]

Bases: enum.Enum

This enum defines beacons message source

invalid_request = 1

Beacons message is invalid request from client to server

request = 0

Beacons message is request from client to server

response = 2

Beacons message is response from server to client

class wasp_general.network.beacon.beacon.WNetworkClientBeacon(config=None, config_section=None, messenger=None, callback=None, transport=None)[source]

Bases: wasp_general.network.beacon.beacon.WBeaconConfig, wasp_general.network.beacon.beacon.WNetworkBeaconBase

Client beacon sends single request and waits for responses for a period of time. This period is specified in beacons configuration as ‘lookup_timeout’ option. Client waiting period can be interrupted by calling the WNetworkClientBeacon.stop() method.

class Handler(config, messenger=None, callback=None)[source]

Bases: wasp_general.network.beacon.beacon.WBeaconIOHandler

Client handler waits for responses and calls callback if it is available

handler_fn(fd, event)[source]

WNativeSocketIOHandler.handler_fn() method implementation.

class wasp_general.network.beacon.beacon.WNetworkServerBeacon(config=None, config_section=None, messenger=None, callback=None, process_any=False, transport=None)[source]

Bases: wasp_general.network.beacon.beacon.WBeaconConfig, wasp_general.network.beacon.beacon.WNetworkBeaconBase

Server beacon that is waiting to respond on a valid request and/or process this request with the specified callback.

class Handler(config, messenger=None, callback=None, process_any=False)[source]

Bases: wasp_general.network.beacon.beacon.WBeaconIOHandler

Server’s handler. Responds on a valid requests, if callback was specified, then it process client request. If server has received invalid request and ‘process_any’ flag was set, then callback (if available) will process this request as invalid

handler_fn(fd, event)[source]

WNativeSocketIOHandler.handler_fn() method implementation.

process_any()[source]

Return ‘process_any’ flag, that is currently used

Returns:bool

wasp_general.network.beacon.messenger module

class wasp_general.network.beacon.messenger.WBeaconGouverneurMessenger(hello_message, invert_hello=False)[source]

Bases: wasp_general.network.beacon.messenger.WBeaconMessengerBase

Basic and real messenger implementation. Request and response are generated the same way, but have different meaning. Messages are generated the following way: [Message header]<:[Address<:TCP/UDP port>]> “Message header” is the first part of the message and it must be exactly the same for client or server. If server has got message with header that doesn’t match its own header, such messages will be omitted. Next parts are “Address” and “TCP/UDP port”. These parts are separated by the WBeaconGouverneurMessenger.__message_splitter__ separator. “TCP/UDP port” can’t be specified without an “Address” An “Address” field can be an IP address or a domain name.

For the client “Address” and “UDP Port” are treated as address where server must send the response. But for the server, they are treated as the address, that the server publish. For both type of messages “Address” and “TCP/UDP port” are generated from a configuration. Options are located in ‘wasp-general::network::beacon’ section. Configuration option “public_address” is used as “Address” value and option “public_port” is used as “UDP Port”

has_response(beacon_config, request, client_address)[source]

WBeaconMessengerBase.has_response() method implementation. This method compares request header with internal one.

hello_message(invert_hello=False)[source]

Return message header.

Parameters:invert_hello – whether to return the original header (in case of False value) or reversed one (in case of True value).
Returns:bytes
invert_hello()[source]

Return whether this messenger was constructed with ‘invert_hello’ or not.

Returns:bool
request(beacon_config)[source]

WBeaconMessengerBase.request() method implementation.

see WBeaconGouverneurMessenger

response(beacon_config, request, client_address)[source]

WBeaconMessengerBase.request() method implementation.

see WBeaconGouverneurMessenger

response_address(beacon_config, request, client_address)[source]

WBeaconMessengerBase.request() method implementation.

see WBeaconGouverneurMessenger

valid_response(beacon_config, response, server_address)[source]

WBeaconMessengerBase.valid_response() method implementation. Response when it has correct header. Response header must be reversed if this messenger was constructed with ‘invert_hello’ flag.

class wasp_general.network.beacon.messenger.WBeaconMessenger[source]

Bases: wasp_general.network.beacon.messenger.WBeaconMessengerBase

Simple. Demo/debug messenger. Server side just return original hello message to the sender.

has_response(beacon_config, request, client_address)[source]

WBeaconMessengerBase.has_response() method implementation. This class has a response only if the request wasn’t empty

request(beacon_config)[source]

WBeaconMessengerBase.request() method implementation. Sends WBeaconMessenger.__beacon_hello_msg__ to a server

response(beacon_config, request, client_address)[source]

WBeaconMessengerBase.response() method implementation. In response sends the same data as server has got

response_address(beacon_config, request, client_address)[source]

WBeaconMessengerBase.request() method implementation. Return the same address, that server detects (assume that clients address is correct)

valid_response(beacon_config, response, server_address)[source]

WBeaconMessengerBase.valid_response() method implementation. Response is valid if it has anything.

class wasp_general.network.beacon.messenger.WBeaconMessengerBase[source]

Bases: object

This is interface for classes, that implement communication (messaging) logic for beacons

see also: WNetworkBeacon

has_response(beacon_config, request, client_address)[source]

Whether this messenger has response or it must skip the request. Return True if there is a response, otherwise - False. If this method returns False, then calling a WBeaconMessengerBase.response() method treats as error.

Parameters:
  • beacon_config
  • request
  • client_address
Returns:

bool

message_maxsize = 512
request(beacon_config)[source]

Generate client request for beacon. It is calling from client side.

Parameters:beacon_config – client beacon configuration.
Returns:bytes
response(beacon_config, request, client_address)[source]

Generate server response for clients request. Obviously, it is calling from server side

Parameters:
  • beacon_config – server beacon configuration
  • request – client request message
  • client_address – client address
Returns:

bytes

response_address(beacon_config, request, client_address)[source]

Return client address where server must send response. It is Possible, that address where server must send response is different then the origin address. In that case, client address is encoded in request message.

Parameters:
  • beacon_config – server configuration
  • request – client request message
  • client_address – original client address
Returns:

WIPV4SocketInfo

valid_response(beacon_config, response, server_address)[source]

Return True if server response isn’t junk.

Parameters:
  • beacon_config – client beacon configuration
  • response – server response
  • server_address – original server address
Returns:

bool

class wasp_general.network.beacon.messenger.WHostgroupBeaconMessenger(hello_message, *hostgroup_names, invert_hello=False)[source]

Bases: wasp_general.network.beacon.messenger.WBeaconGouverneurMessenger

This messenger is based on WBeaconGouverneurMessenger class. This messenger extends WBeaconGouverneurMessenger functionality by working with host group names. Also, this class doubles maximum message size defined in WBeaconMessengerBase. In cases where no host group name are specified this class works as its basic class WBeaconGouverneurMessenger.

Messages format is updated from this:

‘[Message header]<:[Address<:TCP/UDP port>]>’

to this:

‘[Message header]<:[Address<:TCP/UDP port>]><#<[Hostgroup name]>,<[Hostgroup name]…>’>

‘Message header’, ‘Address’ and ‘TCP/UDP port’ work the same way as they specified in WBeaconGouverneurMessenger class. At the end of the original message new optional separator is appended. This separator is defined at WHostgroupBeaconMessenger.__message_groups_splitter__. After this separator host group names can be defined. Host group names can contain latin letters and numbers only. Host group names are separated by WHostgroupBeaconMessenger.__group_splitter__

If this messenger is constructed with at least one host group name, then it won’not response to requests, that do not have at least one host group name, that this messenger was constructed with.

has_response(beacon_config, request, client_address)[source]

WBeaconMessengerBase.has_response() method implementation. This method compares request headers as WBeaconGouverneurMessenger.has_response() do and compares specified group names with internal names.

hostgroups()[source]

Return list of host group names

Returns:list of str
message_maxsize = 1024

Doubled message size for list of names

re_hostgroup_name = re.compile('^[a-zA-Z0-9]+$')

Regular expression for host group name validation

response_address(beacon_config, request, client_address)[source]

WBeaconMessengerBase.response_address() method implementation. It just removes host group names part and return WBeaconMessengerBase.response_address() result

wasp_general.network.beacon.task module

class wasp_general.network.beacon.task.WNetworkBeaconTask(config=None, config_section=None, thread_name=None, messenger=None)[source]

Bases: wasp_general.network.beacon.beacon.WNetworkServerBeacon, wasp_general.task.thread.WThreadTask

Convenient way to start WNetworkBeacon server side

see WNetworkServerBeacon

start()[source]
stop()[source]
thread_started()[source]
thread_stopped()[source]

wasp_general.network.beacon.transport module

class wasp_general.network.beacon.transport.WBroadcastBeaconTransport[source]

Bases: wasp_general.network.transport.WBroadcastNetworkTransport

Network beacon transport, that uses IPv4 broadcast communication

class wasp_general.network.beacon.transport.WMulticastBeaconTransport[source]

Bases: wasp_general.network.transport.WMulticastNetworkTransport

Network beacon transport, that uses IPv4 multicast communication

wasp_general.network.beacon.transport.client_configuration = <wasp_general.network.transport.WNetworkNativeTransportSocketConfig object>

Mainly. ‘address’ and ‘port’ options are used on client side for destination definition (for server destination). This options can be used for address validation on server side also. For example, in WMulticastBeaconTransport class ‘address’ checks if it is a valid multicast address, in WBroadcastBeaconTransport class ‘address’ checks if it is a IPv4 address.

wasp_general.network.beacon.transport.server_configuration = <wasp_general.network.transport.WNetworkNativeTransportSocketConfig object>

Server side use ‘bind_address’ options to set up listening socket. Server will be able to receive requests only at this address. Default is ‘’ (empty string) that works as ‘0.0.0.0’ address, in this case server will be able to receive requests at any address defined in the system.

Module contents