diff options
author | Alex Auvolat <alex@adnab.me> | 2018-09-11 15:39:09 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-09-11 15:39:09 +0200 |
commit | a033c82a3c656a8f53feb60b5b149680771ac247 (patch) | |
tree | bc3cb9a6954aebcfd1a0c5f61d367e1083802c3e /shard/lib/app | |
parent | e92969db3f0a2093da16eb7db18c9db49225a719 (diff) | |
download | shard-a033c82a3c656a8f53feb60b5b149680771ac247.tar.gz shard-a033c82a3c656a8f53feb60b5b149680771ac247.zip |
Use DETS to store shard & peer list to disk
Diffstat (limited to 'shard/lib/app')
-rw-r--r-- | shard/lib/app/blockstore.ex | 4 | ||||
-rw-r--r-- | shard/lib/app/chat.ex | 19 |
2 files changed, 17 insertions, 6 deletions
diff --git a/shard/lib/app/blockstore.ex b/shard/lib/app/blockstore.ex index 8e4fddc..5e93135 100644 --- a/shard/lib/app/blockstore.ex +++ b/shard/lib/app/blockstore.ex @@ -109,10 +109,10 @@ defmodule SApp.BlockStore do end def ask_random_peers(state, key) do - peers = :ets.lookup(:shard_peer_db, state.shard_id) + peers = Shard.Manager.get_shard_peers(state.shard_id) |> Enum.shuffle |> Enum.take(3) - for {_, peer} <- peers do + for peer <- peers do Shard.Manager.send(peer, {state.shard_id, state.path, {:get, key}}) end end diff --git a/shard/lib/app/chat.ex b/shard/lib/app/chat.ex index 051fab6..471d8f7 100644 --- a/shard/lib/app/chat.ex +++ b/shard/lib/app/chat.ex @@ -10,7 +10,6 @@ defmodule SApp.Chat do Future improvements: - message signing - storage of the chatroom messages to disk - - storage of the known peers that have this channel to disk - use a DHT to find peers that are interested in this channel - epidemic broadcast (carefull not to be too costly, maybe by limiting the number of peers we talk to) @@ -22,6 +21,18 @@ defmodule SApp.Chat do require Logger alias SData.MerkleSearchTree, as: MST + + defmodule Manifest do + defstruct [:channel] + end + + defimpl Shard.Manifest, for: Manifest do + def start(m) do + SApp.Chat.start_link(m.channel) + end + end + + @doc """ Start a process that connects to a given channel """ @@ -33,7 +44,7 @@ defmodule SApp.Chat do Initialize channel process. """ def init(channel) do - manifest = {:chat, channel} + manifest = %Manifest{channel: channel} id = SData.term_hash manifest case Shard.Manager.register(id, manifest, self()) do @@ -75,7 +86,7 @@ defmodule SApp.Chat do send data for this channel if they have some. """ def handle_cast(:init_pull, state) do - for {_, pid, _, _} <- :ets.tab2list(:peer_db) do + for {_, pid, _, _} <- Shard.Manager.list_peers do GenServer.cast(pid, {:send_msg, {:interested, [state.id]}}) end {:noreply, state} @@ -101,7 +112,7 @@ defmodule SApp.Chat do end notif = {:append, prev_root, msgitem, mst.root} - for {_, peer_id} <- :ets.lookup(:shard_peer_db, state.id) do + for peer_id <- Shard.Manager.get_shard_peers(state.id) do Shard.Manager.send(peer_id, {state.id, nil, notif}) end |