sync_socket(addr, port, prot=<protocol object>, out_addr=None, debug_level=0, leasing=True)¶
This class is used to sync dictionaries between programs. It extends
Because of this inheritence, this can also be used as an alert network
This also implements and optional leasing system by default. This leasing system means that if node A sets a key, node B cannot overwrite the value at that key for an hour.
This may be turned off by adding
leasing=Falseto the constructor.
__init__(addr, port, prot=<protocol object>, out_addr=None, debug_level=0, leasing=True)¶
Shortcut method to send a handshake response. This method is extracted from
__handle_handshake()in order to allow cleaner inheritence from
Updates the value at a given key.
KeyError– If you do not have the lease for this slot. Lease is given automatically for one hour if the slot is open.
sync_socket.__setitem__()for each key/value pair in the given dictionary.
Parameters: update_dict – A
dict-like object to extract key/value pairs from. Key and value be a
Retrieves the value at a given key.
Parameters: key – The key that you wish to update. Must be a
Returns: The value at this key, or
retif there is none.
This function cleans the list of recently relayed messages based on the following heurisitics:
- Delete all duplicates
- Delete all older than 60 seconds
This function is used to generate a list-formatted group of your peers. It goes in format
[ ((addr, port), ID), ...]
This callback is used to deal with handshake signals. Its three primary jobs are:
- reject connections seeking a different network
- set connection state
- deal with connection conflicts
This callback is used to deal with peer signals. Its primary jobs is to connect to the given peers, if this does not exceed
This callback is used to deal with request signals. Its three primary jobs are:
- respond with a peers signal if packets is
- if you know the ID requested, respond to it
- if you don’t, make a request with your peers
- respond with a peers signal if packets is
This callback is used to deal with response signals. Its two primary jobs are:
- if it was your request, send the deferred message
- if it was someone else’s request, relay the information
Sometimes in trying to recover a network a race condition is created. This function applies a heuristic to try and organize the fallout from that race condition. While it isn’t perfect, it seems to have increased connection recovery rate from ~20% to ~75%. This statistic is from memory on past tests. Much improvement can be made here, but this statistic can likely never be brought to 100%.
In the failure condition, the overall network is unaffacted for large networks. In small networks this failure condition causes a fork, usually where an individual node is kicked out.
- handler – The handler with whom you have a connection conflict
- h_id – The id of this handler
Shortcut method for sending a handshake to a given handler
Parameters: handler – A
This callback is used to deal with data storage signals. Its two primary jobs are:
- store data in a given key
- delete data in a given key
_sync_socket__store(key, new_data, new_meta, error=True)¶
Private API method for storing data. You have permission to store something if:
- The network is not enforcing leases, or
- There is no value at that key, or
- The lease on that key has lapsed (not been set in the last hour), or
- You are the owner of that key
- key – The key you wish to store data at
- new_data – The data you wish to store in said key
- new_meta – The metadata associated with this storage
- error – A boolean which says whether to raise a
KeyErrorif you can’t store there
KeyError– If someone else has a lease at this value, and
If the socket is not closed, close the socket
RuntimeError– The socket was already closed
connect(addr, port, id=None)¶
This function connects you to a specific node in the overall network. Connecting to one node should connect you to the rest of the network, however if you connect to the wrong subnet, the handshake failure involved is silent. You can check this by looking at the truthiness of this objects routing table. Example:
>>> conn = mesh.mesh_socket('localhost', 4444) >>> conn.connect('localhost', 5555) >>> # do some other setup for your program >>> if (!conn.routing_table): ... conn.connect('localhost', 6666) # any fallback address
- addr – A string address
- port – A positive, integral port
- id – A string-like object which represents the expected ID of this node
Closes a given connection, and removes it from your routing tables
Parameters: handler – the connection you would like to close
Decides how to handle various message types, allowing some to be handled automatically
IDs of incoming connections
IDs of outgoing connections
This function has two behaviors depending on whether quantity is truthy.
If truthy is truthy, it will return a list of
messageobjects up to length len.
If truthy is not truthy, it will return either a single
Parameters: quantity – The maximum number of
messages you would like to pull
Returns: A list of
messages, an empty list, a single
Register a handler for incoming method.
Parameters: method – A function with two given arguments. Its signature should be of the form
handler(msg, handler), where msg is a
py2p.base.messageobject, and handler is a
py2p.base.base_connectionobject. It should return
Trueif it performed an action, to reduce the number of handlers checked.
ValueError– If the method signature doesn’t parse correctly
Requests your peers’ routing tables
This sends a message to all of your peers. If you use default values it will send it to everyone on the network
- *args – A list of strings or bytes-like objects you want your peers to receive
- **kargs – There are two keywords available:
- flag – A string or bytes-like object which defines your flag. In other words, this defines packet 0.
- type – A string or bytes-like object which defines your message type. Changing this from default can have adverse effects.
If you change the type attribute from default values, bad things could happen. It MUST be a value from
py2p.base.flags, and more specifically, it MUST be either
whisper. The only other valid flags are
renegotiate, but these are RESERVED and must NOT be used.
The status of the socket.
"Nominal"if all is going well, or a list of unexpected (Excpetion, traceback) tuples if not
This class is used to store metadata for a particular key