atomic_p2p.mixin package

Submodules

atomic_p2p.mixin.commandable module

class atomic_p2p.mixin.commandable.CommandableMixin

Bases: object

CommandableMixin for anything which needs command ability. The class inherits this mixin, should contains a dict named to commands.

commands

dict variable to stroe / register commands.

onProcess(msg_arr, **kwargs)
Return type

str

register_command(command, force=False)

Register the command with it’s cmd to commands

Parameters
  • command (Command) – The command to be register.

  • force (bool) – If command is exists, whether override it or not.

Return type

bool

Returns

True if command been set, False is fail.

unregister_command(cmd)

Unregister a command in commands

Parameters

cmd (str) – Target command’s key cmd to unregister.

Return type

bool

Returns

True if remove success, False means not exists.

atomic_p2p.mixin.handleable module

class atomic_p2p.mixin.handleable.HandleableMixin

Bases: object

HandleableMixin for anything which needs handle ability. The class which needs communication through Peer class should inherits this

mixin, and contains a dict named pkt_handlers.

pkt_handlers

dict variable to stroe / register handles.

register_handler(handler, force=False)

Register the handler with it’s pkt_type to pkt_handlers

Parameters
  • handler (Handler) – The handler to be register.

  • force (bool) – If handler is exists, whether override it or not.

Return type

bool

Returns

True if handler been set, False is fail.

select_handler(pkt_type)

select a handler with given packet handler type. If current class’s pkt_handlers is not match, will iterate each va- riable which inherits HandleableMixin class. Also it will prevent circular interact in case a instance that passing parent instance.

Parameters

pkt_type (str) – Target handler’s pkt_type to get.

Return type

Union[None, Handler]

Returns

Handler which match given pkt_type or None if not found.

unregister_handler(pkt_type)

Unregister a handler in pkt_handlers

Parameters

pkt_type (str) – Target handler’s pkt_type to unregister.

Return type

bool

Returns

True if remove success, False means not exists.

Module contents

class atomic_p2p.mixin.CommandableMixin

Bases: object

CommandableMixin for anything which needs command ability. The class inherits this mixin, should contains a dict named to commands.

commands

dict variable to stroe / register commands.

onProcess(msg_arr, **kwargs)
Return type

str

register_command(command, force=False)

Register the command with it’s cmd to commands

Parameters
  • command (Command) – The command to be register.

  • force (bool) – If command is exists, whether override it or not.

Return type

bool

Returns

True if command been set, False is fail.

unregister_command(cmd)

Unregister a command in commands

Parameters

cmd (str) – Target command’s key cmd to unregister.

Return type

bool

Returns

True if remove success, False means not exists.

class atomic_p2p.mixin.HandleableMixin

Bases: object

HandleableMixin for anything which needs handle ability. The class which needs communication through Peer class should inherits this

mixin, and contains a dict named pkt_handlers.

pkt_handlers

dict variable to stroe / register handles.

register_handler(handler, force=False)

Register the handler with it’s pkt_type to pkt_handlers

Parameters
  • handler (Handler) – The handler to be register.

  • force (bool) – If handler is exists, whether override it or not.

Return type

bool

Returns

True if handler been set, False is fail.

select_handler(pkt_type)

select a handler with given packet handler type. If current class’s pkt_handlers is not match, will iterate each va- riable which inherits HandleableMixin class. Also it will prevent circular interact in case a instance that passing parent instance.

Parameters

pkt_type (str) – Target handler’s pkt_type to get.

Return type

Union[None, Handler]

Returns

Handler which match given pkt_type or None if not found.

unregister_handler(pkt_type)

Unregister a handler in pkt_handlers

Parameters

pkt_type (str) – Target handler’s pkt_type to unregister.

Return type

bool

Returns

True if remove success, False means not exists.

class atomic_p2p.mixin.DefaultAuthenticatorMixin

Bases: atomic_p2p.abc.authenticator.AuthenticatorABC

This class provide a peer’s ability to decide a packet is allow to be process

by higer layer in Peer or not.

The class which inherit this mixin should contain some fileds and methods:
  1. fields: (1) program_hash: a hash str for recognize packet is safe or not.

    Usually a hash of whole program can prevent Man-in-Middle attack.

    1. logger: logger for everything.

  2. methods: (1) _on_packet: The method to keep process packet to higher level when

    authenticate success.

    1. pend_packet: The method to queue a socket to process schedule of a Peer.

class atomic_p2p.mixin.LanTopologyMixin

Bases: atomic_p2p.abc.topology.TopologyABC

add_peer_in_net(peer_info, **kwargs)

Add given PeerInfo into current net’s peer_pool.

Parameters

peer_info (PeerInfo) – A PeerInfo object to be add.

Raises

AssertionError – If given peer_info variable is not in proper PeerInfo type.

Return type

None

del_peer_in_net(peer_info, **kwargs)

Delete given PeerInfo if exists in current net’s peer_pool

Parameters

peer_info (PeerInfo) – A PeerInfo object to be delete.

Return type

bool

Returns

True is success, or False.

Raises

ValueError – If peer_info object type is not PeerInfo.

get_peer_info_by_conn(conn, **kwargs)

The function get peer info by given socket.

Return type

Union[None, _Forwardref]

get_peer_info_by_host(host, **kwargs)

Get PeerInfo object from current net’s peer_pool if exists.

Parameters

host (Tuple[str, int]) – A tuple(str, int) object represents host in net.

Return type

Union[None, PeerInfo]

Returns

A PeerInfo object get from peer_pool if exists or None.

Raises

ValueError – If a given host is not tuple(str, int) object.

handler_broadcast_packet(host, pkt_type, **kwargs)

Exported function for pending broadcast pkt to specific peers. This function is for anyother instance to make a safer packet send with given role currently in peer_pool to broadcast.

Args:from atomic_p2p.utils.logging import getLogger

host: First argument in tuple will not be used. Only use second one

which represents target role to recive the pkt.

pkt_type: Packet’s unique identity str. kwargs: Any addtional arguments need by Handler.

Return type

None

handler_unicast_packet(host, pkt_type, **kwargs)

Exported function for pending unicast pkt with specific host. This function is for anyother instance to make a safer packet send with specific host currently in peer_pool.

Parameters
  • host (Tuple[str, int]) – Destination to recieve packet. This host should be currently in .

  • pkt_type (str) – Packet’s unique identity str.

  • kwargs – Any addtional arguments need by Handler.

Raises

AssertionError – If given host variable is not in proper Tuple[str, int] type.

Return type

None

is_peer_in_net(info, **kwargs)

Return if in current net pool

Parameters

peer_info – A PeerInfo object or a tuple with (str, int) type represents host.

Return type

bool

Returns

true if in net, or False.

Raises

ValueError – If peer_info’s type is not PeerInfo or tuple (str, int)

join_net(host, **kwargs)

Join into a net with known host. This method is use to join a net with known host while current peer is

not in any net yet.

Parameters

host (Tuple[str, int]) – A tuple with address in str at position 0 and port in int at positon 1.

Return type

None

join_net_by_DNS(domain, ns=None, **kwargs)

Join into a net with known domain. This method is use to join a net with known domain while current peer

is not in any net yet.

The domain can point to single or multiple exists host in net.

Parameters
  • domain (str) – The domain point to any host currently in net.

  • ns (Optional[List[str]]) – A list with str, specified which DNS server to query.

Raises

ValueError – No any valid record after given domain.

Return type

None

leave_net(**kwargs)
Return type

None

on_packet_to_route(sock, pkt, **kwargs)

Call by AuthenticatorABC after a packet passed the authentication. The content should be like:

handler = self.select_handler(pkt_type=pkt._type) if handler is None:

self.logger.info(“Unknown packet type: {}”.format(pkt._type))

else:

return handler.on_recv(src=pkt.src, pkt=pkt, sock=sock)

Notice that select_handler method is from HandleableMixin.

Return type

None

topology_register_handler()

Call by a peer class to register essentials handlers. The content should be like:

installing_handlers = [

… AnyHandlerYouNeed, …

] for each in installing_handlers:

self.register_handler(handler=each)

Notice that register_handler method is from HandleableMixin & each element inside installing_handlers should be a Handler object.