aboutsummaryrefslogtreecommitdiff
path: root/shard
diff options
context:
space:
mode:
Diffstat (limited to 'shard')
-rw-r--r--shard/lib/app/chat.ex5
-rw-r--r--shard/lib/app/directory.ex164
-rw-r--r--shard/lib/app/identity.ex14
-rw-r--r--shard/lib/manager.ex8
4 files changed, 135 insertions, 56 deletions
diff --git a/shard/lib/app/chat.ex b/shard/lib/app/chat.ex
index 2a3872b..405210b 100644
--- a/shard/lib/app/chat.ex
+++ b/shard/lib/app/chat.ex
@@ -39,6 +39,7 @@ defmodule SApp.Chat do
defimpl Shard.Manifest do
def module(_m), do: SApp.Chat
+ def is_valid?(_m), do: true
end
end
@@ -60,6 +61,10 @@ defmodule SApp.Chat do
defimpl Shard.Manifest do
def module(_m), do: SApp.Chat
+ def is_valid?(m) do
+ Enum.all?(m.pk_list, &(byte_size(&1)==32))
+ and m.pk_list == m.pk_list |> Enum.sort |> Enum.uniq
+ end
end
end
diff --git a/shard/lib/app/directory.ex b/shard/lib/app/directory.ex
index f4b5a0b..96e3a2b 100644
--- a/shard/lib/app/directory.ex
+++ b/shard/lib/app/directory.ex
@@ -1,8 +1,9 @@
defmodule SApp.Directory do
@moduledoc"""
Shard application for a directory of other shards.
+ Items can be stored (they become dependencies) or just links for possible reference.
- TODO: use MST for file list instead of plain list
+ TODO: use MST for item list instead of plain list
"""
use GenServer
@@ -19,11 +20,16 @@ defmodule SApp.Directory do
defimpl Shard.Manifest do
def module(_m), do: SApp.Directory
+ def is_valid?(m) do
+ is_boolean(m.public)
+ and is_binary(m.name)
+ and byte_size(m.owner) == 32
+ end
end
end
defmodule State do
- defstruct [:owner, :public, :name, :manifest, :id, :netgroup, :files, :revfiles]
+ defstruct [:owner, :public, :name, :manifest, :id, :netgroup, :items, :revitems]
end
def start_link(manifest) do
@@ -35,7 +41,7 @@ defmodule SApp.Directory do
id = SData.term_hash manifest
Shard.Manager.dispatch_to(id, nil, self())
- files = case Shard.Manager.load_state(id) do
+ items = case Shard.Manager.load_state(id) do
nil ->
SData.SignRev.new %{}
st -> st
@@ -46,37 +52,37 @@ defmodule SApp.Directory do
end
SNet.Group.init_lookup(netgroup, self())
- revfiles = for {n, m} <- SData.SignRev.get(files), into: %{}, do: {m, n}
+ revitems = for {n, m} <- SData.SignRev.get(items), into: %{}, do: {m, n}
{:ok, %State{
owner: owner, public: public, name: name,
manifest: manifest, id: id, netgroup: netgroup,
- files: files, revfiles: revfiles}}
+ items: items, revitems: revitems}}
end
def handle_call(:manifest, _from, state) do
{:reply, state.manifest, state}
end
- def handle_call(:get_files, _from, state) do
- {:reply, SData.SignRev.get(state.files), state}
+ def handle_call(:get_items, _from, state) do
+ {:reply, SData.SignRev.get(state.items), state}
end
- def handle_call({:add_file, name, manifest}, _from, state) do
+ def handle_call({:add_item, name, manifest, stored}, _from, state) do
if Shard.Keys.have_sk?(state.owner) do
- dict = SData.SignRev.get(state.files)
- if dict[name] != nil and dict[name] != manifest do
+ dict = SData.SignRev.get(state.items)
+ if dict[name] != nil and elem(dict[name], 0) != manifest do
{:reply, :exists_already, state}
else
- dict = Map.put(dict, name, manifest)
- GenServer.cast(Shard.Manager, {:dep_list, state.id, Map.values(dict)})
- {:ok, st2} = SData.SignRev.set(state.files, dict, state.owner)
+ dict = Map.put(dict, name, {manifest, stored})
+ {:ok, st2} = SData.SignRev.set(state.items, dict, state.owner)
Shard.Manager.save_state(state.id, st2)
state = %{state |
- files: st2,
- revfiles: Map.put(state.revfiles, manifest, name)
+ items: st2,
+ revitems: Map.put(state.revitems, manifest, name)
}
bcast_state(state)
+ send_deps(state)
{:reply, :ok, state}
end
else
@@ -84,20 +90,41 @@ defmodule SApp.Directory do
end
end
- def handle_call({:rm_file, name}, _from, state) do
+ def handle_call({:rm_item, item}, _from, state) do
if Shard.Keys.have_sk?(state.owner) do
- dict = SData.SignRev.get(state.files)
- if dict[name] == nil do
- {:reply, :not_found, state}
- else
- state = put_in(state.revfiles, Map.delete(state.revfiles, dict[name]))
- dict = Map.delete(dict, name)
- GenServer.cast(Shard.Manager, {:dep_list, state.id, Map.values(dict)})
- {:ok, st2} = SData.SignRev.set(state.files, dict, state.owner)
- Shard.Manager.save_state(state.id, st2)
- state = put_in(state.files, st2)
- bcast_state(state)
- {:reply, :ok, state}
+ dict = SData.SignRev.get(state.items)
+ case find(state, dict, item) do
+ {name, manifest} ->
+ state = put_in(state.revitems, Map.delete(state.revitems, manifest))
+ dict = Map.delete(dict, name)
+ {:ok, st2} = SData.SignRev.set(state.items, dict, state.owner)
+ Shard.Manager.save_state(state.id, st2)
+ state = put_in(state.items, st2)
+ bcast_state(state)
+ send_deps(state)
+ {:reply, :ok, state}
+ nil ->
+ {:reply, :not_found, state}
+ end
+ else
+ {:reply, :impossible, state}
+ end
+ end
+
+ def handle_call({:set_stored, item, stored}, _from, state) do
+ if Shard.Keys.have_sk?(state.owner) do
+ dict = SData.SignRev.get(state.items)
+ case find(state, dict, item) do
+ {name, manifest} ->
+ dict = Map.put(dict, name, {manifest, stored})
+ {:ok, st2} = SData.SignRev.set(state.items, dict, state.owner)
+ Shard.Manager.save_state(state.id, st2)
+ state = put_in(state.items, st2)
+ bcast_state(state)
+ send_deps(state)
+ {:reply, :ok, state}
+ nil ->
+ {:reply, :not_found, state}
end
else
{:reply, :impossible, state}
@@ -105,23 +132,22 @@ defmodule SApp.Directory do
end
def handle_call({:read, name}, _from, state) do
- dict = SData.SignRev.get(state.files)
+ dict = SData.SignRev.get(state.items)
{:reply, dict[name], state}
end
def handle_call({:find, manifest}, _from, state) do
- {:reply, state.revfiles[manifest], state}
+ {:reply, state.revitems[manifest], state}
end
def handle_cast(:send_deps, state) do
- dict = SData.SignRev.get(state.files)
- GenServer.cast(Shard.Manager, {:dep_list, state.id, Map.values(dict)})
+ send_deps(state)
{:noreply, state}
end
def handle_cast({:interested, peer_pid, auth}, state) do
if SNet.Group.in_group?(state.netgroup, peer_pid, auth) do
- SNet.Manager.send_pid(peer_pid, {state.id, nil, {:update, SData.SignRev.signed(state.files), true}})
+ SNet.Manager.send_pid(peer_pid, {state.id, nil, {:update, SData.SignRev.signed(state.items), true}})
end
{:noreply, state}
end
@@ -132,17 +158,17 @@ defmodule SApp.Directory do
else
state = case msg do
{:update, signed, ask_reply} when signed != nil ->
- state = case SData.SignRev.merge(state.files, signed, state.pk) do
- {true, newfiles} ->
- Shard.Manager.save_state(state.id, newfiles)
- state = put_in(state.files, newfiles)
+ state = case SData.SignRev.merge(state.items, signed, state.pk) do
+ {true, newitems} ->
+ Shard.Manager.save_state(state.id, newitems)
+ state = put_in(state.items, newitems)
bcast_state(state, [conn_pid])
state
{false, _} ->
state
end
if ask_reply do
- SNet.Manager.send_pid(conn_pid, {state.id, nil, {:update, SData.SignRev.signed(state.files), false}})
+ SNet.Manager.send_pid(conn_pid, {state.id, nil, {:update, SData.SignRev.signed(state.items), false}})
end
state
_ -> state
@@ -151,53 +177,83 @@ defmodule SApp.Directory do
end
end
+ defp find(state, dict, item) do
+ cond do
+ dict[item] != nil ->
+ {manifest, _} = dict[item]
+ {item, manifest}
+ state.revitems[item] != nil ->
+ name = state.revitems[item]
+ {name, item}
+ true ->
+ nil
+ end
+ end
+
defp bcast_state(state, exclude \\ []) do
- msg = {state.id, nil, {:update, SData.SignRev.signed(state.files), false}}
+ msg = {state.id, nil, {:update, SData.SignRev.signed(state.items), false}}
SNet.Group.broadcast(state.netgroup, msg, exclude_pid: exclude)
end
+ defp send_deps(state) do
+ dict = SData.SignRev.get(state.items)
+ IO.puts("items: #{inspect dict}")
+ deps = for {_, {m, stored}} <- dict, stored, do: m
+ IO.puts("stored: #{inspect deps}")
+ GenServer.cast(Shard.Manager, {:dep_list, state.id, deps})
+ end
+
# ================
# PUBLIC INTERFACE
# ================
@doc"""
- Return list of files stored in this directory.
+ Return list of items stored in this directory.
- Returns a dictionnary of %{name => manifest}.
+ Returns a dictionnary of %{name => {manifest, stored?}}.
"""
- def get_files(pid) do
- GenServer.call(pid, :get_files)
+ def get_items(pid) do
+ GenServer.call(pid, :get_items)
end
@doc"""
- Return the manifest of file with a given name in directory, or nil if not found.
+ Return the manifest of item with a given name in directory, or nil if not found.
- Equivalent to get_files(pid)[name] but better.
+ Equivalent to get_items(pid)[name] but better.
"""
def read(pid, name) do
GenServer.call(pid, {:read, name})
end
@doc"""
- Find a file in the directory by its manifest. Returns name if found or nil if not found.
+ Find an item in the directory by its manifest. Returns name if found or nil if not found.
"""
def find(pid, manifest) do
GenServer.call(pid, {:find, manifest})
end
@doc"""
- Add a file to this directory. A file is a name for a shard manifest.
- A file added to a directory becomes a dependency of the directory, i.e.
- if the directory is pinned then all files inside are pinned as well.
+ Add an item to this directory. An item is a name for a shard manifest.
+ An item added to a directory becomes a dependency of the directory, i.e.
+ if the directory is pinned then all items inside are pinned as well.
+ """
+ def add_item(pid, name, manifest, stored \\ true) do
+ GenServer.call(pid, {:add_item, name, manifest, stored})
+ end
+
+ @doc"""
+ Remove a named item from this directory.
+ Argument can be either a manifest or the name of an item.
"""
- def add_file(pid, name, manifest) do
- GenServer.call(pid, {:add_file, name, manifest})
+ def rm_item(pid, item) do
+ GenServer.call(pid, {:rm_item, item})
end
@doc"""
- Remove a named file from this directory.
+ Set an item as stored or not stored.
+ Argument can be either a manifest or the name of an item.
"""
- def rm_file(pid, name) do
- GenServer.call(pid, {:rm_file, name})
+ def set_stored(pid, item, stored) do
+ GenServer.call(pid, {:set_stored, item, stored})
end
end
diff --git a/shard/lib/app/identity.ex b/shard/lib/app/identity.ex
index 69bed93..59a4b90 100644
--- a/shard/lib/app/identity.ex
+++ b/shard/lib/app/identity.ex
@@ -27,6 +27,9 @@ defmodule SApp.Identity do
defimpl Shard.Manifest do
def module(_m), do: SApp.Identity
+ def is_valid?(m) do
+ byte_size(m.pk) == 32
+ end
end
end
@@ -79,9 +82,16 @@ defmodule SApp.Identity do
end
def handle_cast(:send_deps, state) do
- # TODO: collections
+ deps = if Shard.Keys.have_sk?(state.pk) do
+ [
+ %SApp.Directory.Manifest{owner: state.pk, public: true, name: "collection"},
+ %SApp.Directory.Manifest{owner: state.pk, public: false, name: "collection"}
+ ]
+ else
+ []
+ end
- GenServer.cast(Shard.Manager, {:dep_list, state.id, []})
+ GenServer.cast(Shard.Manager, {:dep_list, state.id, deps})
{:noreply, state}
end
diff --git a/shard/lib/manager.ex b/shard/lib/manager.ex
index 990bcea..c178e2f 100644
--- a/shard/lib/manager.ex
+++ b/shard/lib/manager.ex
@@ -17,6 +17,11 @@ defprotocol Shard.Manifest do
Get the module in question.
"""
def module(manifest)
+
+ @doc"""
+ Check if manifest is valid
+ """
+ def is_valid?(manifest)
end
defmodule Shard.Manager do
@@ -189,6 +194,8 @@ defmodule Shard.Manager do
end
defp find_or_start(state, shard_id, manifest) do
+ true = Shard.Manifest.is_valid?(manifest)
+
case :dets.lookup(@shard_db, shard_id) do
[] ->
:dets.insert(@shard_db, {shard_id, manifest, cached(), nil})
@@ -200,6 +207,7 @@ defmodule Shard.Manager do
case :ets.lookup(:shard_procs, {shard_id, nil}) do
[] ->
{:ok, pid} = apply(Shard.Manifest.module(manifest), :start_link, [manifest])
+ GenServer.cast(pid, :send_deps)
:ets.insert(:shard_procs, {{shard_id, nil}, pid})
state = Map.put(state, pid, {shard_id, nil})
{pid, state}