Mesh Module¶
-
js2p.mesh.
default_protocol
¶ A
Protocol()
object which is used by default in the mesh module
-
class
js2p.mesh.
MeshConnection
(sock, server, outgoing)¶ This is the class for mesh connection abstractraction. It inherits from
js2p.base.BaseConnection()
Arguments: - sock – This is the raw socket object
- server (js2p.mesh.MeshSocket) – This is a link to the
MeshSocket()
parent - outgoing – This bool describes whether
server
initiated the connection
-
js2p.mesh.MeshConnection.
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.MeshConnection.
found_terminator
()¶ This method is called when the expected amount of data is received
Returns: undefined
-
js2p.mesh.MeshConnection.
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.MeshConnection.
onClose
()¶ This function is run when a connection is closed
-
js2p.mesh.MeshConnection.
onEnd
()¶ This function is run when a connection is ended
-
class
js2p.mesh.
MeshSocket
(addr, port[, protocol[, out_addr[, debug_level]]])¶ This is the class for mesh network socket abstraction. It inherits from
js2p.base.BaseSocket()
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.
MeshSocket Event 'connect'
(conn)¶ This event is called whenever you have a new connection to the service network. In other words, whenever the length of your routing table is increased from zero to one.
If you call
on('connect')
, that will be executed on every connection to the network. So if you are suddenly disconnected, and manage to recover, that function will execute again.To avoid this, call
once('connect')
. That will usually be more correct.Arguments: - conn (js2p.mesh.MeshSocket) – A reference to this abstract socket
-
js2p.mesh.
MeshSocket Event 'message'
(conn)¶ This event is called whenever you receive a new message. A reference to the message is not passed to you. This is to prevent potential memory leaks.
If you want to register a “privileged” handler which does get a reference to the message, see
register_handler()
Arguments: - conn (js2p.mesh.MeshSocket) – A reference to this abstract socket
-
js2p.mesh.MeshSocket.
routing_table
¶ A
Map()
which containsMeshConnection()
s keyed by their IDs
-
js2p.mesh.MeshSocket.
awaiting_ids
¶ An array which contains
MeshConnection()
s that are awaiting handshake information
-
js2p.mesh.MeshSocket.
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.MeshSocket.
_send_peers
(handler)¶ Shortcut method to send a peerlist message. This method is extracted from
__handle_handshake()
in order to allow cleaner inheritence fromjs2p.sync.sync_socket()
-
js2p.mesh.MeshSocket.
_send_handshake
(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.mesh.MeshSocket.
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.MeshSocket('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.MeshSocket.
disconnect
(handler)¶ Closes a given connection, and removes it from your routing tables
Arguments: - handler (js2p.mesh.MeshConnection) – The connection you wish to close
-
js2p.mesh.MeshSocket.
__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.MeshSocket.
__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.MeshConnection) –
Returns: Either
true
orundefined
-
js2p.mesh.MeshSocket.
__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.MeshConnection) –
Returns: Either
true
orundefined
-
js2p.mesh.MeshSocket.
__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.MeshConnection) –
Returns: Either
true
orundefined
-
js2p.mesh.MeshSocket.
__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.MeshConnection) –
Returns: Either
true
orundefined
- respond with a peers signal if packets[1] is
-
js2p.mesh.MeshSocket.
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.MeshSocket.
__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.MeshSocket.
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.