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, 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:
-
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:
-
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:
-
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:
-
-
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)[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:
-
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:
-
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_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:
-
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:
-
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_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) send peer_list to renew those of others
Parameters: peerlist – Returns:
-
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:
-
-
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'¶
-