diff options
author | Alex Auvolat <alex@adnab.me> | 2018-10-11 17:25:31 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-10-11 17:25:31 +0200 |
commit | e5a7330d0526efb592e200ab96c3f33585ae8d02 (patch) | |
tree | 906651f53e17002b32e3db3d77bca2918bf62c47 /shard/lib/app/identity.ex | |
parent | 1646bc57eae9880fd408d23ca692364dc6fd6442 (diff) | |
download | shard-e5a7330d0526efb592e200ab96c3f33585ae8d02.tar.gz shard-e5a7330d0526efb592e200ab96c3f33585ae8d02.zip |
Initial support for private conversations
Diffstat (limited to 'shard/lib/app/identity.ex')
-rw-r--r-- | shard/lib/app/identity.ex | 62 |
1 files changed, 36 insertions, 26 deletions
diff --git a/shard/lib/app/identity.ex b/shard/lib/app/identity.ex index 391d37e..255bebb 100644 --- a/shard/lib/app/identity.ex +++ b/shard/lib/app/identity.ex @@ -35,8 +35,12 @@ defmodule SApp.Identity do st -> st end - GenServer.cast(self(), :init_pull) - {:ok, %{pk: pk, id: id, state: state}} + netgroup = %SNet.PubShardGroup{id: id} + SNet.Group.init_lookup(netgroup, self()) + if Shard.Keys.have_sk? pk do + GenServer.cast(self(), :update_peer_info) + end + {:ok, %{pk: pk, id: id, state: state, netgroup: netgroup}} :redundant -> exit(:redundant) end @@ -52,7 +56,8 @@ defmodule SApp.Identity do id = SData.term_hash manifest case Shard.Manager.find_proc id do nil -> - Shard.Manifest.start manifest + {:ok, pid} = Shard.Manifest.start manifest + pid pid -> pid end end @@ -64,7 +69,7 @@ defmodule SApp.Identity do end def handle_call(:manifest, _from, state) do - {:replyl, state.manifest, state} + {:reply, state.manifest, state} end def handle_call(:get_info, _from, state) do @@ -72,22 +77,15 @@ defmodule SApp.Identity do end def handle_call({:set_info, new_info}, _from, state) do - case SData.SignRev.set(state.state, new_info, state.pk) do - {:ok, st2} -> - Shard.Manager.save_state(state.id, st2) - state = put_in(state.state, st2) - bcast_state(state) - {:reply, :ok, state} - err -> - {:reply, err, state} - end - end - - def handle_cast(:init_pull, state) do - for {_, pid, _} <- SNet.Manager.list_connections do - GenServer.cast(pid, {:send_msg, {:interested, [state.id]}}) + if Shard.Keys.have_sk?(state.pk) do + {:ok, st2} = SData.SignRev.set(state.state, new_info, state.pk) + Shard.Manager.save_state(state.id, st2) + state = put_in(state.state, st2) + bcast_state(state) + {:reply, :ok, state} + else + {:reply, :impossible, state} end - {:noreply, state} end def handle_cast({:interested, peer_pid, _auth}, state) do @@ -102,7 +100,7 @@ defmodule SApp.Identity do {true, st2} -> Shard.Manager.save_state(state.id, st2) state = put_in(state.state, st2) - bcast_state(state, [GenServer.call(conn_pid, :get_peer_info)]) + bcast_state(state, [conn_pid]) state {false, _} -> state @@ -112,11 +110,23 @@ defmodule SApp.Identity do {:noreply, state} end - def bcast_state(state, exclude \\ []) do - for peer_id <- Shard.Manager.get_shard_peers(state.id) do - if not Enum.member? exclude, peer_id do - SNet.Manager.send(peer_id, {state.id, nil, {:update, SData.SignRev.signed(state.state)}}) - end - end + def handle_cast(:update_peer_info, state) do + peer_info = SNet.Addr.get_all_inet4() + |> Enum.map(&({:inet, &1, Application.get_env(:shard, :port)})) + + prev_info = SData.SignRev.get(state.state) + # TODO multi peer info + new_info = Map.put(prev_info, :peer_info, peer_info) + {:ok, st2} = SData.SignRev.set(state.state, new_info, state.pk) + + Shard.Manager.save_state(state.id, st2) + state = put_in(state.state, st2) + bcast_state(state) + {:noreply, state} + end + + def bcast_state(state, _exclude \\ []) do + # TODO: effectively apply exclude list + SNet.Group.broadcast(state.netgroup, {state.id, nil, {:update, SData.SignRev.signed(state.state)}}) end end |