diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2010-06-14 21:26:33 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2010-06-14 21:26:33 +0200 |
commit | 3fe9171137838ede69a3a3e56dbc4b10049d9693 (patch) | |
tree | 3e8a8f70a986ce05229d85bb31048f7d4396b783 /ephraim.erl | |
parent | fcf338a9c6642dc95b123834977428187b2bb310 (diff) | |
download | ephraim-3fe9171137838ede69a3a3e56dbc4b10049d9693.tar ephraim-3fe9171137838ede69a3a3e56dbc4b10049d9693.zip |
Create process for every conversation
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. |