bbc_network module

Copyright (c) 2017 beyond-blockchain.org.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

BBcNetwork(config[, core, p2p_port, …]) Socket and thread management for infrastructure layers
DomainBase([network, config, domain_id, …]) Base class of a domain
class bbc_network.BBcNetwork(config, core=None, p2p_port=None, use_global=True, external_ip4addr=None, external_ip6addr=None, loglevel='all', logname=None)[source]

Bases: object

Socket and thread management for infrastructure layers

add_static_node_to_domain(domain_id, node_id, ipv4, ipv6, port)[source]

Add static peer node for the domain

Parameters:
  • domain_id
  • node_id
  • ipv4
  • ipv6
  • port
Returns:

callback_route_failure(query_entry)[source]

(internal use) Called after several “route_message” trial

Parameters:query_entry
Returns:
create_domain(domain_id=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', network_module=None, get_new_node_id=False)[source]

Create domain and register user in the domain

Parameters:
  • domain_id
  • network_module – string of module script file
  • get_new_node_id – If True, the node_id is newly created again
Returns:

disseminate_cross_ref(transaction_id, asset_group_id)[source]

disseminate transaction_id in the network (domain_global_0)

Parameters:
  • transaction_id
  • asset_group_id
Returns:

domain_ping(query_entry)[source]
forward_message(query_entry)[source]

(internal use) forward message

Parameters:query_entry
Returns:
get(query_entry)[source]

(internal use) try to get resource data

Parameters:
  • nonce
  • domain_id
  • resource_id
  • resource_type
Returns:

get_my_socket_info()[source]

Return waiting port and my IP address

Returns:
put(domain_id=None, asset_group_id=None, resource_id=None, resource_type=0, resource=None)[source]

Put data in the DHT

Parameters:
  • domain_id
  • asset_group_id
  • resource_id
  • resource_type
  • resource
Returns:

receive_domain_ping(ip4, from_addr, msg)[source]

Process received domain_ping. If KeyType.domain_ping value is 1, the sender of the ping is registered as static

Parameters:
  • ip4 – True (from IPv4) / False (from IPv6)
  • from_addr – sender address and port (None if TCP)
  • msg – the message body (already deserialized)
  • payload_type – PayloadType value of msg
Returns:

register_user_id(domain_id, asset_group_id, user_id)[source]

Register user_id connecting directly to this node in the domain

Parameters:
  • domain_id
  • asset_group_id
  • user_id
Returns:

remove_domain(domain_id=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')[source]

Remove domain (remove DHT)

Parameters:domain_id
Returns:
remove_user_id(asset_group_id, user_id)[source]

Remove user_id from the domain

Parameters:
  • asset_group_id
  • user_id
Returns:

route_message(domain_id=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', asset_group_id=None, dst_user_id=None, src_user_id=None, msg_to_send=None, payload_type=1)[source]

Find the destination host and send it

Parameters:
  • domain_id
  • asset_group_id
  • src_user_id – source user
  • dst_user_id – destination user
  • msg_to_send – content to send
  • payload_type – PayloadType value
Returns:

save_all_peer_lists()[source]

Save all peer_lists in the config file

Returns:
send_domain_ping(domain_id, ipv4, ipv6, port)[source]

(internal use) Send raw message to the specified node

Parameters:
  • domain_id
  • ipv4
  • ipv6
  • port
Returns:

send_message_in_network(nodeinfo, payload_type, msg)[source]

Send message over a domain network

Parameters:
  • nodeinfo – NodeInfo object
  • payload_type – PayloadType value
  • msg – data body
Returns:

setup_tcp_server()[source]

(internal use) start tcp server

Returns:
setup_udp_socket()[source]

(internal use) Setup UDP socket

Returns:
tcpserver_loop()[source]

(internal use) message loop for TCP socket

Returns:
udp_message_loop()[source]

(internal use) message loop for UDP socket

Returns:
class bbc_network.DomainBase(network=None, config=None, domain_id=None, node_id=None, loglevel='all', logname=None)[source]

Bases: object

Base class of a domain

add_peer_node(node_id, ip4, addr_info)[source]

Add as a peer node

Parameters:
  • node_id
  • ip4 – True (IPv4)/False (IPv6)
  • addr_info – tuple of (address, port)
Returns:

add_peer_node_ip46(node_id, ipv4, ipv6, port, need_ping=False)[source]

Add as a peer node (with ipv4 and ipv6 address)

Parameters:
  • node_id
  • ipv4
  • ipv6
  • port
Returns:

alive_check()[source]

Check whether alive or not to update node list and to broadcast the list to others

Returns:
domain_manager_loop()[source]

(internal use) maintain the domain (e.g., updating peer list and topology)

Returns:
get_neighbor_nodes()[source]

Return neighbor nodes (for broadcasting message)

Returns:
get_resource(query_entry)[source]
is_registered_user(asset_group_id, user_id)[source]

(internal use) check if the user_id is registered in the asset_group

Parameters:
  • asset_group_id
  • user_id
Returns:

leave_domain()[source]
make_message(dst_node_id=None, nonce=None, msg_type=None)[source]

(internal use) create message with basic components

Parameters:
  • dst_node_id
  • nonce
  • msg_type
Returns:

make_peer_list()[source]

Make binary peer_list (the first entry of the returned result always include the info of the node itself)

Returns:binary data of count,[node_id,ipv4,ipv6,port],[node_id,ipv4,ipv6,port],[node_id,ipv4,ipv6,port],,,,
ping_to_all_neighbors()[source]

send ping to all neighbors :return:

ping_with_retry(query_entry=None, node_id=None, retry_count=3)[source]

Retry ping if response is not received within a given time

Parameters:
  • query_entry
  • node_id – target node_id (need for first trial)
  • retry_count
Returns:

print_peerlist()[source]

Show peer list for debugging

Returns:
process_message(ip4, from_addr, msg)[source]

(internal use) process received message for the network module (need to override)

Parameters:
  • ip4 – True (from IPv4) / False (from IPv6)
  • from_addr – sender address and port (None if TCP)
  • msg – the message body (already deserialized)
Returns:

process_message_base(ip4, from_addr, msg, payload_type)[source]

(internal use) process received message (common process for any kind of network module)

Parameters:
  • ip4 – True (from IPv4) / False (from IPv6)
  • from_addr – sender address and port (None if TCP)
  • msg – the message body (already deserialized)
  • payload_type – PayloadType value of msg
Returns:

put_resource(asset_group_id, resource_id, resource_type, resource)[source]
random_send(msg, count)[source]
refresh_peer_list(query_entry)[source]

(internal use) refresh peer_list by alive_check

Parameters:query_entry
Returns:
register_user_id(asset_group_id, user_id)[source]

Register user_id that connect directly to this core node in the list

Parameters:
  • asset_group_id
  • user_id
Returns:

remove_peer_because_no_response(query_entry)[source]

Remove the peer that does not respond :param query_entry: :return:

remove_peer_node(node_id=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')[source]

Remove node_info from the id_ip_mapping

Parameters:id
Returns:
renew_peerlist(peerlist)[source]

(internal use) renew peer_list

Parameters:peerlist
Returns:
respond_ping(target_id, nonce=None)[source]
respond_store(target_id, nonce)[source]
send_message_to_peer(msg, payload_type=1)[source]

Resolve socket for the target_id and call message send method in BBcNetwork

Parameters:
  • msg
  • payload_type – PayloadType value
Returns:

send_p2p_message(query_entry)[source]
send_ping(target_id, nonce=None)[source]
send_start_refresh(target_id)[source]
send_store(target_id, nonce, asset_group_id, resource_id, resource, resource_type)[source]
set_refresh_timer(interval=1800)[source]

(internal use) set refresh timer

Parameters:interval
Returns:
start_domain_manager()[source]

(internal use) start domain manager loop

Returns:
unregister_user_id(asset_group_id, user_id)[source]

(internal use) remove user_id from the list

Parameters:
  • asset_group_id
  • user_id
Returns:

update_refresh_timer_random(update_time)[source]

update refresh timer with random time (0.5*update_timer - 1.5*update_time) :param update_time: :return:

class bbc_network.InfraMessageTypeBase[source]

Bases: object

ADVERTISE_ASSET_GROUP = b'\x00\x11'
DOMAIN_PING = b'\x00\x00'
MESSAGE_TO_USER = b'\x00G'
NOTIFY_CROSS_REF = b'\x00\x10'
NOTIFY_LEAVE = b'\x00\x01'
NOTIFY_PEERLIST = b'\x00\x02'
REQUEST_FIND_USER = b'\x00C'
REQUEST_FIND_VALUE = b'\x00E'
REQUEST_PING = b'\x00\x04'
REQUEST_STORE = b'\x00@'
RESPONSE_FIND_USER = b'\x00D'
RESPONSE_FIND_VALUE = b'\x00F'
RESPONSE_PING = b'\x00\x05'
RESPONSE_STORE = b'\x00A'
RESPONSE_STORE_COPY = b'\x00B'
START_TO_REFRESH = b'\x00\x03'
bbc_network.check_my_IPaddresses(target4='8.8.8.8', target6='2001:4860:4860::8888', port=80)[source]
bbc_network.convert_to_string(array)[source]
bbc_network.send_data_by_tcp(ipv4=None, ipv6=None, port=6641, msg=None)[source]