# 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

> http://www.apache.org/licenses/LICENSE-2.0

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)

  Bases: `object`

  Socket and thread management for infrastructure layers

  `add_static_node_to_domain`(domain_id, node_id, ipv4, ipv6, port)

    Add static peer node for the domain

    Parameters: | domain_id – node_id – ipv4 – ipv6 – port –
    Returns: |

  `callback_route_failure`(query_entry)

    (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)

    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`(domain_id, transaction_id)

    disseminate transaction_id in the network (domain_global_0)

    Parameters: | domain_id – transaction_id –
    Returns: |

  `domain_ping`(query_entry)
  `forward_message`(query_entry)

    (internal use) forward message

    Parameters: | query_entry –
    Returns: |

  `get`(query_entry)

    (internal use) try to get resource data

    Parameters: | nonce – domain_id – resource_id – resource_type –
    Returns: |

  `get_my_socket_info`()

    Return waiting port and my IP address

    Returns: |

  `put`(domain_id=None, resource_id=None, resource_type=0, resource=None, asset_group_id=None)

    Put data in the DHT

    Parameters: | domain_id – resource_id – resource_type – resource – asset_group_id –
    Returns: |

  `receive_domain_ping`(ip4, from_addr, msg)

    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, user_id)

    Register user_id connecting directly to this node in the domain

    Parameters: | domain_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')

    Remove domain (remove DHT)

    Parameters: | domain_id –
    Returns: |

  `remove_user_id`(user_id)

    Remove user_id from the domain

    Parameters: | 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', dst_user_id=None, src_user_id=None, msg_to_send=None, payload_type=1)

    Find the destination host and send it

    Parameters: | domain_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`()

    Save all peer_lists in the config file

    Returns: |

  `send_domain_ping`(domain_id, ipv4, ipv6, port)

    (internal use) Send raw message to the specified node

    Parameters: | domain_id – ipv4 – ipv6 – port –
    Returns: |

  `send_message_in_network`(nodeinfo, payload_type, msg)

    Send message over a domain network

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

  `setup_tcp_server`()

    (internal use) start tcp server

    Returns: |

  `setup_udp_socket`()

    (internal use) Setup UDP socket

    Returns: |

  `tcpserver_loop`()

    (internal use) message loop for TCP socket

    Returns: |

  `udp_message_loop`()

    (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)

  Bases: `object`

  Base class of a domain

  `add_peer_node`(node_id, ip4, addr_info)

    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)

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

    Parameters: | node_id – ipv4 – ipv6 – port –
    Returns: |

  `alive_check`()

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

    Returns: |

  `domain_manager_loop`()

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

    Returns: |

  `get_neighbor_nodes`()

    Return neighbor nodes (for broadcasting message)

    Returns: |

  `get_resource`(query_entry)
  `leave_domain`()
  `make_message`(dst_node_id=None, nonce=None, msg_type=None)

    (internal use) create message with basic components

    Parameters: | dst_node_id – nonce – msg_type –
    Returns: |

  `make_peer_list`()

    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`()

    send ping to all neighbors :return:

  `ping_with_retry`(query_entry=None, node_id=None, retry_count=3)

    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`()

    Show peer list for debugging

    Returns: |

  `process_message`(ip4, from_addr, msg)

    (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)

    (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`(resource_id, resource_type, resource, asset_group_id)
  `random_send`(msg, count)
  `refresh_peer_list`(query_entry)

    (internal use) refresh peer_list by alive_check

    Parameters: | query_entry –
    Returns: |

  `register_user_id`(user_id)

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

    Parameters: | user_id –
    Returns: |

  `remove_peer_because_no_response`(query_entry)

    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')

    Remove node_info from the id_ip_mapping

    Parameters: | id –
    Returns: |

  `renew_peerlist`(peerlist)

    (internal use) renew peer_list

    Parameters: | peerlist –
    Returns: |

  `respond_ping`(target_id, nonce=None)
  `respond_store`(target_id, nonce)
  `send_message_to_peer`(msg, payload_type=1)

    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)
  `send_ping`(target_id, nonce=None)
  `send_start_refresh`(target_id)
  `send_store`(target_id, nonce, resource_id, resource, resource_type, asset_group_id=None)
  `set_refresh_timer`(interval=1800)

    (internal use) set refresh timer

    Parameters: | interval –
    Returns: |

  `start_domain_manager`()

    (internal use) start domain manager loop

    Returns: |

  `unregister_user_id`(user_id)

    (internal use) remove user_id from the list

    Parameters: | user_id –
    Returns: |

  `update_refresh_timer_random`(update_time)

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

`bbc_network.``check_my_IPaddresses`(target4='8.8.8.8', target6='2001:4860:4860::8888', port=80)

`bbc_network.``convert_to_string`(array)

`bbc_network.``send_data_by_tcp`(ipv4=None, ipv6=None, port=6641, msg=None)