summaryrefslogtreecommitdiffstats
path: root/src/core/ephraim_conv.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/ephraim_conv.erl')
-rw-r--r--src/core/ephraim_conv.erl51
1 files changed, 42 insertions, 9 deletions
diff --git a/src/core/ephraim_conv.erl b/src/core/ephraim_conv.erl
index 3342bc9..d9431b2 100644
--- a/src/core/ephraim_conv.erl
+++ b/src/core/ephraim_conv.erl
@@ -1,18 +1,51 @@
-module(ephraim_conv).
--compile([debug_info, export_all]).
+-behaviour(gen_server).
+
+-export([start_link/2]).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+-export([received_message/4]).
-record(conv_state, {
- my_jid :: binary(),
- jid :: binary()
+ conv_ref :: reference(),
+ event_manager :: pid(),
+ my_jid :: binary(),
+ jid :: binary()
}).
--spec init(binary()) -> ok.
-init(JID) ->
- io:format("Starting a conversation with ~p~n", [JID]),
-
+-spec start_link(pid(), binary()) -> ok.
+start_link(EventManager, JID) ->
+ ConvRef = make_ref(),
+ io:format("Starting conversation ~p with ~p~n", [ConvRef, JID]),
+
+ gen_event:notify(EventManager, {view_update, {conversation, ConvRef, {new, JID}}}),
+
{jid,MyJID} = ephraim_config:get(jid),
- loop(#conv_state{my_jid=list_to_binary(MyJID),jid=JID}),
- io:format("Stopping a conversation with ~p~n", [JID]).
+ gen_server:start_link(?MODULE, #conv_state{conv_ref=ConvRef,event_manager=EventManager,my_jid=list_to_binary(MyJID),jid=JID}, []).
+
+init(State) ->
+ process_flag(trap_exit, true),
+ {ok, State}.
+
+received_message(Conv, From, Type, Body) ->
+ gen_server:cast(Conv, {received_message, From, Type, Body}).
+
+handle_call(_Msg, _From, State) ->
+ {noreply, State}.
+
+handle_cast({received_message, From, Type, Body}, State) ->
+ gen_event:notify(State#conv_state.event_manager, {view_update, {conversation, State#conv_state.conv_ref, {message, From, Type, Body}}}),
+ {noreply, State};
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+handle_info(_Msg, State) ->
+ {ok, State}.
+terminate(_Reason, State) ->
+ io:format("Stopping conversation ~p with ~p~n", [State#conv_state.conv_ref, State#conv_state.jid]),
+ ok.
+code_change(_OldVersion, State, _Extra) ->
+ {ok, State}.
+
-spec loop(#conv_state{}) -> ok.
loop(State) ->