1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
|