aboutsummaryrefslogtreecommitdiff
path: root/shard/lib
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-10-12 11:10:17 +0200
committerAlex Auvolat <alex@adnab.me>2018-10-12 11:10:17 +0200
commitd15d5fbfc5133a9d0f0d99dbbfc023849f61cc37 (patch)
treedaedfd26af6978301b2633a338a137a874876d1f /shard/lib
parent6dcc2eefc3c8db0cadd7300536527dbd1905fa48 (diff)
downloadshard-d15d5fbfc5133a9d0f0d99dbbfc023849f61cc37.tar.gz
shard-d15d5fbfc5133a9d0f0d99dbbfc023849f61cc37.zip
Update TODO, refactor a bit
Diffstat (limited to 'shard/lib')
-rw-r--r--shard/lib/app/chat.ex6
-rw-r--r--shard/lib/app/identity.ex56
-rw-r--r--shard/lib/cli/cli.ex35
-rw-r--r--shard/lib/manager.ex16
4 files changed, 55 insertions, 58 deletions
diff --git a/shard/lib/app/chat.ex b/shard/lib/app/chat.ex
index 35ecdbf..ea210d8 100644
--- a/shard/lib/app/chat.ex
+++ b/shard/lib/app/chat.ex
@@ -108,12 +108,6 @@ defmodule SApp.Chat do
end
end
- def find_proc(chan) do
- manifest = %Manifest{channel: chan}
- id = SData.term_hash manifest
- Shard.Manager.find_proc id
- end
-
@doc """
Implementation of the :manifest call that returns the chat room's manifest
"""
diff --git a/shard/lib/app/identity.ex b/shard/lib/app/identity.ex
index 06bc225..02e8eb9 100644
--- a/shard/lib/app/identity.ex
+++ b/shard/lib/app/identity.ex
@@ -46,30 +46,6 @@ defmodule SApp.Identity do
end
end
- def default_nick(pk) do
- nick_suffix = Shard.Keys.pk_display pk
- "Anon" <> nick_suffix
- end
-
- def find_proc(pk) do
- manifest = %Manifest{pk: pk}
- id = SData.term_hash manifest
- case Shard.Manager.find_proc id do
- nil ->
- case Shard.Manifest.start manifest do
- {:ok, pid} -> pid
- {:error, :redundant} -> find_proc(pk)
- end
- pid -> pid
- end
- end
-
- def get_nick(pk) do
- pid = find_proc pk
- info = GenServer.call(pid, :get_info)
- info.nick
- end
-
def handle_call(:manifest, _from, state) do
{:reply, state.manifest, state}
end
@@ -136,8 +112,38 @@ defmodule SApp.Identity do
{:noreply, state}
end
- def bcast_state(state, _exclude \\ []) do
+ defp bcast_state(state, _exclude \\ []) do
# TODO: effectively apply exclude list
SNet.Group.broadcast(state.netgroup, {state.id, nil, {:update, SData.SignRev.signed(state.state), false}})
end
+
+ # ================
+ # PUBLIC INTERFACE
+ # ================
+
+ @doc"""
+ Return the default nickname associated to a pk,
+ in the form "Anonxxxxxxxx" with some bytes of the pk in hex.
+ """
+ def default_nick(pk) do
+ nick_suffix = Shard.Keys.pk_display pk
+ "Anon" <> nick_suffix
+ end
+
+ @doc"""
+ Find the shard process for an identity. Launches such a process
+ if necessary.
+ """
+ def find_proc(pk) do
+ Shard.Manager.find_or_start %Manifest{pk: pk}
+ end
+
+ @doc"""
+ Get a user's nickname from his pk
+ """
+ def get_nick(pk) do
+ pid = find_proc pk
+ info = GenServer.call(pid, :get_info)
+ info.nick
+ end
end
diff --git a/shard/lib/cli/cli.ex b/shard/lib/cli/cli.ex
index 85fa3fc..3b52fa5 100644
--- a/shard/lib/cli/cli.ex
+++ b/shard/lib/cli/cli.ex
@@ -113,18 +113,9 @@ defmodule SCLI do
end
defp handle_command(state, ["join", qchan]) do
- pid = SApp.Chat.find_proc qchan
- case pid do
- nil ->
- {:ok, pid} = Shard.Manifest.start %SApp.Chat.Manifest{channel: qchan}
- GenServer.cast(pid, {:subscribe, self()})
- IO.puts "Joining ##{qchan} (new shard)"
- %{state | room_pid: pid}
- pid ->
- GenServer.cast(pid, {:subscribe, self()})
- IO.puts "Switching to ##{qchan}"
- %{state | room_pid: pid}
- end
+ pid = Shard.Manager.find_or_start %SApp.Chat.Manifest{channel: qchan}
+ IO.puts "Switching to ##{qchan}"
+ %{state | room_pid: pid}
end
defp handle_command(state, ["pm" | people_list]) do
@@ -151,22 +142,12 @@ defmodule SCLI do
end
end
if Enum.all?(pk_list, &(&1 != :error)) do
- pk_list = [state.pk | pk_list]
- manifest = SApp.Chat.PrivChat.Manifest.new(pk_list)
- id = SData.term_hash manifest
- case Shard.Manager.find_proc id do
- nil ->
- {:ok, pid} = Shard.Manifest.start manifest
- GenServer.cast(pid, {:subscribe, self()})
- IO.puts "Joining private conversation (new shard)."
- %{state | room_pid: pid}
- pid ->
- GenServer.cast(pid, {:subscribe, self()})
- IO.puts "Switching to private conversation."
- %{state | room_pid: pid}
- end
+ manifest = SApp.Chat.PrivChat.Manifest.new([state.pk | pk_list])
+ pid = Shard.Manager.find_or_start manifest
+ IO.puts "Switching to private conversation."
+ %{state | room_pid: pid}
else
- state
+ state
end
end
diff --git a/shard/lib/manager.ex b/shard/lib/manager.ex
index 9def229..d6b493b 100644
--- a/shard/lib/manager.ex
+++ b/shard/lib/manager.ex
@@ -213,6 +213,22 @@ defmodule Shard.Manager do
end
@doc"""
+ Returns the pid for a shard defined by its manifest.
+ Start it if it doesn't exist.
+ """
+ def find_or_start(manifest) do
+ id = SData.term_hash manifest
+ case find_proc id do
+ nil ->
+ case Shard.Manifest.start manifest do
+ {:ok, pid} -> pid
+ {:error, :redundant} -> find_proc id
+ end
+ pid -> pid
+ end
+ end
+
+ @doc"""
Return the list of all shards.
"""
def list_shards() do