aboutsummaryrefslogtreecommitdiff
path: root/shard
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-10-02 15:34:18 +0200
committerAlex Auvolat <alex@adnab.me>2018-10-02 15:34:18 +0200
commit486344f87f1e4c9b0cc391f12ce6bbf939be06ab (patch)
tree410175a4f8d3a9126f310766e0ea48acffc90307 /shard
parentfec60e26c0fc4bdc34c05bb52b12970987470254 (diff)
downloadshard-486344f87f1e4c9b0cc391f12ce6bbf939be06ab.tar.gz
shard-486344f87f1e4c9b0cc391f12ce6bbf939be06ab.zip
WIP
Diffstat (limited to 'shard')
-rw-r--r--shard/lib/net/chan.ex66
1 files changed, 66 insertions, 0 deletions
diff --git a/shard/lib/net/chan.ex b/shard/lib/net/chan.ex
new file mode 100644
index 0000000..aa5c186
--- /dev/null
+++ b/shard/lib/net/chan.ex
@@ -0,0 +1,66 @@
+defprotocol SNet.Chan do
+ @moduledoc"""
+ Abstract definition for a communication channel
+ """
+
+ @doc"""
+ Function to send a message to relevant peers.
+ """
+ def send(chan, msg)
+
+ @doc"""
+ Function to declare a handler for a channel
+ """
+ def handle(chan, func)
+end
+
+def SNet.PeerChan do
+ @moduledoc"""
+ Direct channel to a peer
+ """
+
+ defstruct [:peer_id, :shard_id, :path]
+
+ def new(peer_id, shard_id, path) do
+ %__MODULE__{peer_id: peer_id, shard_id: shard_id, path: path}
+ end
+
+ defimpl SNet.Chan do
+ def send(chan, msg) do
+ Shard.Manager.send(chan.peer_id, {chan.shard_id, chan.path, msg})
+ end
+
+ def handle(chan, func) do
+ # DO NOT USE THIS
+ assert false
+ end
+ end
+end
+
+def SNet.FloodChan do
+ @moduledoc"""
+ Channel that send a message to all know peers for shard (floods the network)
+ """
+
+ defstruct [:shard_id, :path]
+
+ def new(shard_id, path) do
+ %__MODULE__{shard_id: shard_id, path: path}
+ end
+
+ defimpl SNet.Chan do
+ def send(chan, msg) do
+ for x <- Shard.Manager.get_shard_peers(chan.shard_id) do
+ Shard.Manager.send(x, {chan.shard_id, chan.path, msg})
+ end
+ end
+
+ def handle(chan, func) do
+ # TODO
+ end
+ end
+end
+
+def SNet.CipherChan do
+ # TODO
+end