Sync Module¶
-
class
js2p.sync.
metatuple
(owner, timestamp)¶ This class is used to store metadata for a particular key
-
class
js2p.sync.
sync_socket
(addr, port[, leasing[, protocol[, out_addr[, debug_level]]]])¶ This is the class for mesh network socket abstraction. It inherits from
js2p.mesh.mesh_socket()
. 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 setting
leasing
tofalse
to the constructor.Arguments: - addr (string) – The address you’d like to bind to
- port (number) – The port you’d like to bind to
- leasing (boolean) – Whether this class’s leasing system should be enabled (default:
true
) - protocol (js2p.base.protocol) – The subnet you’re looking to connect to
- out_addr (array) – Your outward-facing address
- debug_level (number) – The verbosity of debug prints
-
js2p.sync.sync_socket.
__store
(key, new_data, new_meta, error)¶ Private API method for storing data
Arguments: - 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
KeyError
if you can’t store there
Raises Error: If someone else has a lease at this value, and
error
is notfalse
-
js2p.sync.sync_socket.
_send_handshake_response
(handler)¶ Shortcut method to send a handshake response. This method is extracted from
__handle_handshake()
in order to allow cleaner inheritence fromjs2p.sync.sync_socket()
-
js2p.sync.sync_socket.
__handle_store
()¶ 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
param msg: A message()
param handler: A mesh_connection()
returns: Either true
orundefined
-
js2p.sync.sync_socket.
get
(key[, fallback])¶ Retrieves the value at a given key
Arguments: - key – The key you wish to look up (must be transformable into a
Buffer()
) - fallback – The value it should return when the key has no data
Returns: The value at the given key, or
fallback
.Raises TypeError: If the key could not be transformed into a
Buffer()
- key – The key you wish to look up (must be transformable into a
-
js2p.sync.sync_socket.
set
(key, value)¶ Sets the value at a given key
Arguments: - key – The key you wish to look up (must be transformable into a
Buffer()
) - value – The key you wish to store (must be transformable into a
Buffer()
)
Raises TypeError: If a key or value could not be transformed into a
Buffer()
Raises: See
__store()
- key – The key you wish to look up (must be transformable into a