bbc_network module — BBc1 0.7 documentation

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

    disseminate transaction_id in the network (domain_global_0)

    Parameters: | transaction_id – asset_group_id –
    Returns: |

  `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, asset_group_id=None, resource_id=None, resource_type=0, resource=None)

    Put data in the DHT

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

  `raw_ping`(query_entry)
  `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, asset_group_id, user_id)

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

    Remove domain (remove DHT)

    Parameters: | domain_id –
    Returns: |

  `remove_user_id`(asset_group_id, user_id)

    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)

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

    Save all peer_lists in the config file

    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: |

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

    (internal use) Send raw message to the specified node

    Parameters: | domain_id – ipv4 – ipv6 – port –
    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)

    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)
  `is_registered_user`(asset_group_id, user_id)

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

    Parameters: | asset_group_id – user_id –
    Returns: |

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

    (internal use) refresh peer_list by alive_check

    Parameters: | query_entry –
    Returns: |

  `register_user_id`(asset_group_id, user_id)

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

    Remove node_info from the id_ip_mapping

    Parameters: | id –
    Returns: |

  `renew_peerlist`(peerlist)

    (internal use) send peer_list to renew those of others

    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, asset_group_id, resource_id, resource, resource_type)
  `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`(asset_group_id, user_id)

    (internal use) remove user_id from the list

    Parameters: | asset_group_id – user_id –
    Returns: |

class `bbc_network.` `InfraMessageTypeBase`

  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)

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