summaryrefslogtreecommitdiffstats
path: root/ephraim.erl
diff options
context:
space:
mode:
Diffstat (limited to 'ephraim.erl')
-rw-r--r--ephraim.erl29
1 files changed, 24 insertions, 5 deletions
diff --git a/ephraim.erl b/ephraim.erl
index 11ef8a8..60902da 100644
--- a/ephraim.erl
+++ b/ephraim.erl
@@ -3,20 +3,29 @@
-record(state, {
conn :: pid(),
- convs :: dict:dictionary()
+ convs :: dict(),
+ guis :: set()
}).
+-spec start() -> ok.
start() ->
Pid = spawn(?MODULE, init, []),
- register(ephraim, Pid).
+ case register(ephraim, Pid) of
+ true ->
+ ok
+ end.
+-spec stop() -> ok.
stop() ->
- ephraim ! stop.
+ ephraim ! stop,
+ ok.
+-spec init() -> ok.
init() ->
Conn = spawn(ephraim_conn, init, []),
- loop(#state{conn=Conn,convs=dict:new()}).
+ loop(#state{conn=Conn,convs=dict:new(),guis=sets:new()}).
+-spec get_conv(#state{},exmpp_jid:jid()) -> {#state{},pid()}.
get_conv(State, JID) ->
case dict:find(JID, State#state.convs) of
{ok, Conv} ->
@@ -27,11 +36,21 @@ get_conv(State, JID) ->
{State#state{convs=Dict}, Conv}
end.
+-spec loop(#state{}) -> ok.
loop(State) ->
receive
stop ->
dict:fold(fun(_,Conv,Msg) -> Conv ! Msg end, stop, State#state.convs),
- State#state.conn ! stop;
+ State#state.conn ! stop,
+ ok;
+ {register_gui, Pid} ->
+ GUIs = State#state.guis,
+ State2 = State#state{guis=sets:add_element(Pid,GUIs)},
+ loop(State2);
+ {unregister_gui, Pid} ->
+ GUIs = State#state.guis,
+ State2 = State#state{guis=sets:del_element(Pid,GUIs)},
+ loop(State2);
{receive_message, From, Packet} ->
{State2, Conv} = get_conv(State, From),
Conv ! {receive_message, Packet},