Mesh Module¶
-
js2p.mesh.
default_protocol
¶ A
protocol()
object which is used by default in the mesh module
-
class
js2p.mesh.
mesh_connection
(sock, server, outgoing)¶ This is the class for mesh connection abstractraction. It inherits from
js2p.base.base_connection()
Arguments: - sock – This is the raw socket object
- server (js2p.mesh.mesh_socket) – This is a link to the
mesh_socket()
parent - outgoing – This bool describes whether
server
initiated the connection
-
js2p.mesh.mesh_connection.
send
(msg_type, packs, id, time)¶ Sends a message through its connection.
Arguments: - msg_type – Message type, corresponds to the header in a
InternalMessage()
object - packs – A list of Buffer-like objects, which correspond to the packets to send to you
- id – The ID this message should appear to be sent from (default: your ID)
- time (number) – The time this message should appear to be sent from (default: now in UTC)
Returns: undefined
- msg_type – Message type, corresponds to the header in a
-
js2p.mesh.mesh_connection.
found_terminator
()¶ This method is called when the expected amount of data is received
Returns: undefined
-
js2p.mesh.mesh_connection.
handle_waterfall
(msg, packets)¶ This method determines whether this message has been previously received or not. If it has been previously received, this method returns
true
. If it is older than a preset limit, this method returnstrue
. Otherwise this method returnsundefined
, and forwards the message appropriately.Arguments: - msg (js2p.base.InternalMessage) – The message in question
- packets – The message’s packets
Returns: true
orundefined
-
js2p.mesh.mesh_connection.
onClose
()¶ This function is run when a connection is closed
-
js2p.mesh.mesh_connection.
onEnd
()¶ This function is run when a connection is ended
-
class
js2p.mesh.
mesh_socket
(addr, port[, protocol[, out_addr[, debug_level]]])¶ This is the class for mesh network socket abstraction. It inherits from
js2p.base.base_socket()
Arguments: - addr (string) – The address you’d like to bind to
- port (number) – The port you’d like to bind to
- 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.mesh.mesh_socket.
routing_table
¶ An object which contains
mesh_connection()
s keyed by their IDs
-
js2p.mesh.mesh_socket.
awaiting_ids
¶ An array which contains
mesh_connection()
s that are awaiting handshake information
-
js2p.mesh.mesh_socket.
recv
([num])¶ This function has two behaviors depending on whether num is truthy.
If num is truthy, it will return a list of
message()
objects up to length len.If num is not truthy, it will return either a single
message()
object, orundefined
Arguments: - num (number) – The maximum number of
message()
s you would like to pull
Returns: A list of
message()
s, an empty list, a singlemessage()
, orundefined
- num (number) – The maximum number of
-
js2p.mesh.mesh_socket.
_send_handshake_response
(handler)¶ Shortcut method to send a handshake response. This method is extracted from :js:meth:`~js2p.mesh.mesh_socket.__handle_handshake` in order to allow cleaner inheritence from
js2p.sync.sync_socket()
-
js2p.mesh.mesh_socket.
connect
(addr, port[, id])¶ 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:
> var conn = new 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 ... }
Arguments: - addr (string) – A string address
- port (number) – A positive, integral port
- id – A string-like object which represents the expected ID of this node
Note
While in the Python version there are more thorough checks on this, the Javascript implementation can connect to itself. There are checks to keep this from happening automatically, but it’s still trivial to override this via human intervention. Please do not try to connect to yourself.
-
js2p.mesh.mesh_socket.
disconnect
(handler)¶ Closes a given connection, and removes it from your routing tables
Arguments: - handler (js2p.mesh.mesh_connection) – The connection you wish to close
-
js2p.mesh.mesh_socket.
__get_peer_list
()¶ This function is used to generate a list-formatted group of your peers. It goes in format
[ [[addr, port], ID], ...]
Returns: An array in the above format
-
js2p.mesh.mesh_socket.
__handle_handshake
(msg, conn)¶ 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
Arguments: - msg (js2p.base.message) –
- conn (js2p.mesh.mesh_connection) –
Returns: Either
true
orundefined
-
js2p.mesh.mesh_socket.
__handle_peers
(msg, conn)¶ This callback is used to deal with peer signals. Its primary jobs is to connect to the given peers, if this does not exceed
js2p.mesh.max_outgoing
Arguments: - msg (js2p.base.message) –
- conn (js2p.mesh.mesh_connection) –
Returns: Either
true
orundefined
-
js2p.mesh.mesh_socket.
__handle_response
(msg, conn)¶ 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
Arguments: - msg (js2p.base.message) –
- conn (js2p.mesh.mesh_connection) –
Returns: Either
true
orundefined
-
js2p.mesh.mesh_socket.
__handle_request
(msg, conn)¶ This callback is used to deal with request signals. Its three primary jobs are:
- respond with a peers signal if packets[1] is
'*'
- if you know the ID requested, respond to it
- if you don’t, make a request with your peers
Arguments: - msg (js2p.base.message) –
- conn (js2p.mesh.mesh_connection) –
Returns: Either
true
orundefined
- respond with a peers signal if packets[1] is
-
js2p.mesh.mesh_socket.
send
(packets[, flag[, type]])¶ This sends a message to all of your peers. If you use default values it will send it to everyone on the network
Arguments: - packets – A list of strings or Buffer-like objects you want your peers to receive
- flag – A string or Buffer-like object which defines your flag. In other words, this defines packet 0.
- type – A string or Buffer-like object which defines your message type. Changing this from default can have adverse effects.
Warning
If you change the type attribute from default values, bad things could happen. It MUST be a value from
js2p.base.flags
, and more specifically, it MUST be eitherbroadcast
orwhisper
. The only other valid flags arewaterfall
andrenegotiate
, but these are RESERVED and must NOT be used.
-
js2p.mesh.mesh_socket.
__clean_waterfalls
()¶ This function cleans the list of recently relayed messages based on the following heurisitics:
- Delete all duplicates
- Delete all older than 60 seconds
-
js2p.mesh.mesh_socket.
waterfall
(msg)¶ This function handles message relays. Its return value is based on whether it took an action or not.
Arguments: - msg (js2p.base.message) – The message in question
Returns: true
if the message was then forwarded.false
if not.