diff options
Diffstat (limited to 'ephraim.erl')
-rw-r--r-- | ephraim.erl | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/ephraim.erl b/ephraim.erl index 0aff011..11ef8a8 100644 --- a/ephraim.erl +++ b/ephraim.erl @@ -1,6 +1,11 @@ -module(ephraim). -compile([debug_info, export_all]). +-record(state, { + conn :: pid(), + convs :: dict:dictionary() + }). + start() -> Pid = spawn(?MODULE, init, []), register(ephraim, Pid). @@ -10,13 +15,28 @@ stop() -> init() -> Conn = spawn(ephraim_conn, init, []), - loop(Conn). + loop(#state{conn=Conn,convs=dict:new()}). + +get_conv(State, JID) -> + case dict:find(JID, State#state.convs) of + {ok, Conv} -> + {State, Conv}; + error -> + Conv = spawn(ephraim_conv, init, [JID]), + Dict = dict:store(JID, Conv, State#state.convs), + {State#state{convs=Dict}, Conv} + end. -loop(Conn) -> +loop(State) -> receive stop -> - Conn ! stop; + dict:fold(fun(_,Conv,Msg) -> Conv ! Msg end, stop, State#state.convs), + State#state.conn ! stop; + {receive_message, From, Packet} -> + {State2, Conv} = get_conv(State, From), + Conv ! {receive_message, Packet}, + loop(State2); Msg -> io:format("ephraim: ~p~n", [Msg]), - loop(Conn) + loop(State) end. |