diff options
-rw-r--r-- | ephraim.erl | 29 | ||||
-rw-r--r-- | ephraim_conn.erl | 3 | ||||
-rw-r--r-- | ephraim_conv.erl | 4 |
3 files changed, 30 insertions, 6 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}, diff --git a/ephraim_conn.erl b/ephraim_conn.erl index 211db4d..18edd6a 100644 --- a/ephraim_conn.erl +++ b/ephraim_conn.erl @@ -7,6 +7,7 @@ roster :: any() }). +-spec init() -> ok. init() -> application:start(exmpp), Session = exmpp_session:start(), @@ -15,11 +16,13 @@ init() -> exmpp_session:connect_TCP(Session, "jabber.ccc.de", 5222), session(#conn_state{session=Session}). +-spec session(#conn_state{}) -> ok. session(State) -> exmpp_session:login(State#conn_state.session), exmpp_session:send_packet(State#conn_state.session, exmpp_presence:set_status(exmpp_presence:available(), "Foo/Test\\Bar")), loop(State). +-spec loop(#conn_state{}) -> ok. loop(State) -> receive stop -> diff --git a/ephraim_conv.erl b/ephraim_conv.erl index d28cef8..99c7668 100644 --- a/ephraim_conv.erl +++ b/ephraim_conv.erl @@ -2,12 +2,14 @@ -compile([debug_info, export_all]). -record(conv_state, { - jid :: binary() + jid :: exmpp_jid:jid() }). +-spec init(exmpp_jid:jid()) -> ok. init(JID) -> loop(#conv_state{jid=JID}). +-spec loop(#conv_state{}) -> ok. loop(State) -> receive stop -> |