aboutsummaryrefslogtreecommitdiff
path: root/shard/lib/app/chat.ex
diff options
context:
space:
mode:
Diffstat (limited to 'shard/lib/app/chat.ex')
-rw-r--r--shard/lib/app/chat.ex47
1 files changed, 16 insertions, 31 deletions
diff --git a/shard/lib/app/chat.ex b/shard/lib/app/chat.ex
index ff0c97d..b046fc3 100644
--- a/shard/lib/app/chat.ex
+++ b/shard/lib/app/chat.ex
@@ -12,11 +12,7 @@ defmodule SApp.Chat do
%SApp.Chat.PrivChat.Manifest{pk_list: ordered_list_of_authorized_pks}
Future improvements:
- - message signing
- - storage of the chatroom messages 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)
- partial synchronization only == data distributed over peers
"""
@@ -32,7 +28,9 @@ defmodule SApp.Chat do
defmodule Manifest do
@moduledoc"""
- Manifest for a public chat room defined by its name.
+ Manifest for a public chat room defined by its name. Example:
+
+ %SApp.Chat.Manifest{channel: "test"}
"""
defstruct [:channel]
@@ -73,19 +71,22 @@ defmodule SApp.Chat do
# ==========
defmodule State do
+ @moduledoc"""
+ Internal state struct of chat shard.
+ """
+
defstruct [:id, :netgroup, :manifest, :page_store, :mst, :subs, :read]
end
@doc """
- Start a process that connects to a given channel
+ Start a process that connects to a given channel. Don't call directly, use for instance:
+
+ Shard.Manager.find_or_start %SApp.Chat.Manifest{channel: "my_chan"}
"""
def start_link(manifest) do
GenServer.start_link(__MODULE__, manifest)
end
- @doc """
- Initialize channel process.
- """
def init(manifest) do
id = SData.term_hash manifest
@@ -103,7 +104,7 @@ defmodule SApp.Chat do
end
root = cond do
root == nil -> nil
- GenServer.call(page_store, {:have_rec, root}) -> root
+ SApp.PageStore.have_rec?(page_store, root) -> root
true ->
Logger.warn "Not all pages for saved root were saved, restarting from an empty state!"
nil
@@ -124,9 +125,6 @@ defmodule SApp.Chat do
}
end
- @doc """
- Implementation of the :manifest call that returns the chat room's manifest
- """
def handle_call(:manifest, _from, state) do
{:reply, state.manifest, state}
end
@@ -166,11 +164,6 @@ defmodule SApp.Chat do
{:noreply, state}
end
- @doc """
- Implementation of the :chat_send handler. This is the main handler that is used
- to send a message to the chat room. Puts the message in the store and syncs
- with all connected peers.
- """
def handle_cast({:chat_send, pk, msg}, state) do
next_ts = case MST.last(state.mst, nil, 1) do
[] -> System.os_time :seconds
@@ -204,10 +197,6 @@ defmodule SApp.Chat do
{:noreply, state}
end
- @doc """
- Implementation of the :interested handler, this is called when a peer we are
- connected to asks to recieve data for this channel.
- """
def handle_cast({:interested, conn_pid, auth}, state) do
if SNet.Group.in_group?(state.netgroup, conn_pid, auth) do
SNet.Manager.send_pid(conn_pid, {state.id, nil, {:root, state.mst.root, true}})
@@ -221,10 +210,6 @@ defmodule SApp.Chat do
{:noreply, %{ state | subs: new_subs }}
end
- @doc """
- Implementation of the :msg handler, which is the main handler for messages
- comming from other peers concerning this chat room.
- """
def handle_cast({:msg, conn_pid, auth, _shard_id, nil, msg}, state) do
if not SNet.Group.in_group?(state.netgroup, conn_pid, auth) do
# Ignore message
@@ -245,7 +230,7 @@ defmodule SApp.Chat do
mst2 = MST.insert(state.mst, msgitem)
if mst2.root == new_root do
state = %{state | mst: mst2}
- GenServer.cast(state.page_store, {:set_roots, [mst2.root]})
+ SApp.PageStore.set_roots(state.page_store, [mst2.root])
save_state(state)
msg_callback(state, msgitem)
SNet.Group.broadcast(state.netgroup, {state.id, nil, msg}, exclude_pid: [conn_pid])
@@ -311,7 +296,7 @@ defmodule SApp.Chat do
for x <- new do
msg_callback(state, x)
end
- GenServer.cast(state.page_store, {:set_roots, [mst.root]})
+ SApp.PageStore.set_roots(state.page_store, [mst.root])
state = %{state | mst: mst}
save_state(state)
if state.mst.root != old_root do
@@ -365,15 +350,15 @@ defmodule SApp.Chat do
The process calling this function will start recieving messages of the form:
- {:chat_recv, manifest, {pk, msgbin, sign}}
+ {:chat_recv, manifest, {pk, msgbin, sign}}
or
- {:chat_send, manifest, {pk, msgbin, sign}}
+ {:chat_send, manifest, {pk, msgbin, sign}}
msgbin can be used in the following way:
- {timestamp, message} = SData.term_unbin msgbin
+ {timestamp, message} = SData.term_unbin msgbin
"""
def subscribe(shard_pid) do
GenServer.cast(shard_pid, {:subscribe, self()})