diff options
author | Alex Auvolat <alex@adnab.me> | 2018-10-10 15:04:11 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-10-10 15:04:11 +0200 |
commit | 1ee9c3fa6d4259d63685aea95d23b515f59a74cf (patch) | |
tree | 418decf98286040d67aa11533092f1bcf0a996e5 /shard/lib/net/chan.ex_ | |
parent | 3ba44d72a09d3da72d0dd3e495e5cdea07a0bdac (diff) | |
download | shard-1ee9c3fa6d4259d63685aea95d23b515f59a74cf.tar.gz shard-1ee9c3fa6d4259d63685aea95d23b515f59a74cf.zip |
BROKE EVERYTHING SORRY
Diffstat (limited to 'shard/lib/net/chan.ex_')
-rw-r--r-- | shard/lib/net/chan.ex_ | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/shard/lib/net/chan.ex_ b/shard/lib/net/chan.ex_ new file mode 100644 index 0000000..5aba960 --- /dev/null +++ b/shard/lib/net/chan.ex_ @@ -0,0 +1,67 @@ +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 + +defmodule 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 + raise :do_not_use_this + # assert false + end + end +end + +defmodule 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 + +defmodule SNet.CipherChan do + # TODO +end |