README.md

Exkad
=====

### kademlia in elixir


#### Building a network

```elixir 

# This is really the only module you need to interact with
alias Exkad.Node.Gateway

# args are id, seed, protocol, and port
# Passing a nil id will cause the id to be created for you
# A nil seed means this node will not connect to anyone, ie: it's the first in the net.
# This node could then be the seed for other nodes
# This will return a {node_pid, node_descriptor}. The node_pid is used to communicate 
# with the Gateway, which is a GenServer. 
# This line will build a node listening on port 8080 for requests from other nodes
{node_pid, node_descriptor} = Gateway.build_node(nil, nil, :http, "http://localhost:8080")

# A node descriptor is a struct defined in the Node module that has the id and location 
# of the node. It is what you need to give to other nodes so that they can join the network
%Exkad.Node.Descriptor{id: id, loc: location} = node_descriptor
location # in this case would be http://localhost:8080
id # some hash of a uuid

# Adding 10 new nodes to the network, which will join the network through the seed node
Enum.map(9000..9010, fn port -> 
  bind_to = "http://locahost:#{port}"
  Gateway.build_node(nil, node_descriptor, :http, bind_to)
end)

# build_node/4 gives back a {node_id, node_descriptor} tuple
# node_id is used to communicate with the gateway, which is the interface
# to the underlying node process, which handles the actuall communication with other
# nodes. This means to put a value into the network, you just do..

{:ok, key} = Gateway.put(node_id, "hello world")


# Similarly, to get a value..
{:value, {:ok, value}} = Gateway.get(node_id, key)



```