Chord Module

js2p.chord.get_hashes(key)

Returns the (adjusted) hashes for a given key. This is in the order of:

  • SHA1 (shifted 224 bits left)
  • SHA224 (shifted 160 bits left)
  • SHA256 (shifted 128 bits left)
  • SHA384 (unadjusted)
  • SHA512 (unadjusted)

The adjustment is made to allow better load balancing between nodes, which assign responisbility for a value based on their SHA384-assigned ID.

class js2p.chord.ChordConnection(sock, server, outgoing)

This is the class for chord connection abstractraction. It inherits from js2p.mesh.MeshConnection()

Arguments:
  • sock – This is the raw socket object
  • server (js2p.chord.ChordSocket) – This is a link to the ChordSocket() parent
  • outgoing – This bool describes whether server initiated the connection
class js2p.chord.ChordSocket(addr, port[, protocol[, out_addr[, debug_level]]])

This is the class for chord network socket abstraction. It inherits from js2p.mesh.MeshSocket()

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.chord.ChordSocket Event 'add'(conn, key)

This event is triggered when a key is added to the distributed dictionary. Because value information is not transmitted in this message, you must specifically request it.

Arguments:
  • conn (js2p.chord.ChordSocket) – A reference to this abstract socket
  • key (Buffer) – The key which has a new value
js2p.chord.ChordSocket Event 'delete'(conn, key)

This event is triggered when a key is deleted from your distributed dictionary.

Arguments:
  • conn (js2p.chord.ChordSocket) – A reference to this abstract socket
  • key (Buffer) – The key which has a new value
js2p.chord.ChordSocket.id_10

This socket’s ID as a big-integer()

js2p.chord.ChordSocket.__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.chord.max_outgoing

Arguments:
Returns:

Either true or undefined

js2p.chord.ChordSocket.get(key[, fallback[, timeout]])

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
  • timeout – The maximum time (in seconds) to wait before returning fallback
Returns:

A Promise() for the value at the given key, or fallback.

Raises TypeError:
 

If the key could not be transformed into a Buffer()

js2p.chord.ChordSocket.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 value you wish to store
Raises TypeError:
 

If a key could not be transformed into a Buffer()

Raises:

See __store()

js2p.chord.ChordSocket.update(update_dict)

For each key/value pair in the given object, calls set()

Arguments:
  • update_dict (Object) – An object with keys and values which can be transformed into a Buffer()
Raises:

See set()

js2p.chord.ChordSocket.del(key)

Clears the value at a given key

Arguments:
  • key – The key you wish to look up (must be transformable into a Buffer() )
Raises TypeError:
 

If a key or value could not be transformed into a Buffer()

Raises:

See set()

js2p.chord.ChordSocket.keys()

Returns a generator for all keys presently in the dictionary

Because this data is changed asynchronously, the key is only garunteed to be present at the time of generation.

Returns:A generator which yields Buffer() s
js2p.chord.ChordSocket.values()

Returns a generator for all values presently in the dictionary

Because this data is changed asynchronously, the value is only garunteed to be accurate at the time of generation.

Returns:A generator which yields Promise() s. These Promise() s will yield a Buffer() , on success.
js2p.chord.ChordSocket.items()

Returns a generator for all associations presently in the dictionary

Because this data is changed asynchronously, the association is only garunteed to be present at the time of generation.

Returns:A generator which yields pairs of Buffer() s and Promise() s. The Promise() s will yield a Buffer() , on success.
js2p.chord.ChordSocket.pop(key[, fallback])

Returns the value at a given key. As a side effect, it it deletes that association.

Returns:A Promise() , similar to the js2p.chord.ChordSocket.get() method.
js2p.chord.ChordSocket.popitem()

Returns the association at a key. As a side effect, it it deletes that association.

Returns:A pair of Buffer() and Promise() , similar to an item in js2p.chord.ChordSocket.items()
js2p.chord.ChordSocket.copy()

Returns the Promise() of an Object() , with the same associations as this DHT.

Note

This is potentially a very slow operation. It is probably significantly faster, and likely to be more accurate, if you iterate over js2p.chord.ChordSocket.items()

Returns:The Promise() of an Object() , with the same associations as this DHT.