summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2010-06-14 21:26:33 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2010-06-14 21:26:33 +0200
commit3fe9171137838ede69a3a3e56dbc4b10049d9693 (patch)
tree3e8a8f70a986ce05229d85bb31048f7d4396b783
parentfcf338a9c6642dc95b123834977428187b2bb310 (diff)
downloadephraim-3fe9171137838ede69a3a3e56dbc4b10049d9693.tar
ephraim-3fe9171137838ede69a3a3e56dbc4b10049d9693.zip
Create process for every conversation
-rw-r--r--ephraim.erl28
-rw-r--r--ephraim_conn.erl13
-rw-r--r--ephraim_conv.erl5
3 files changed, 31 insertions, 15 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.
diff --git a/ephraim_conn.erl b/ephraim_conn.erl
index 009d38d..211db4d 100644
--- a/ephraim_conn.erl
+++ b/ephraim_conn.erl
@@ -33,16 +33,9 @@ loop(State) ->
loop(State);
#received_packet{packet_type=message, raw_packet=Packet} ->
- case exmpp_message:get_type(Packet) of
- chat ->
- From = exmpp_xml:get_attribute(Packet, from, <<"unknown">>),
- Body = exmpp_message:get_body(Packet),
- io:format("~ts: ~ts~n", [From, Body]);
- _ ->
- ok
- end,
- loop(State);
-
+ From = exmpp_xml:get_attribute(Packet, from, <<"unknown">>),
+ ephraim ! {receive_message, From, Packet},
+ loop(State);
Msg ->
io:format("ephraim_conn: ~p~n", [Msg]),
loop(State)
diff --git a/ephraim_conv.erl b/ephraim_conv.erl
index c6243fa..d28cef8 100644
--- a/ephraim_conv.erl
+++ b/ephraim_conv.erl
@@ -12,7 +12,10 @@ loop(State) ->
receive
stop ->
ok;
+ {receive_message, Packet} ->
+ io:format("Received packet from ~p: ~p~n", [State#conv_state.jid, Packet]),
+ loop(State);
Msg ->
- io:format("ephraim_conv: ~p~n", [Msg]),
+ io:format("ephraim_conv (~p): ~p~n", [State#conv_state.jid, Msg]),
loop(State)
end.