From c3354ad1fd4f843d80ebdd290c8dbaf81919ca38 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 15 Jun 2010 09:44:39 +0200 Subject: Added type specs --- ephraim.erl | 29 ++++++++++++++++++++++++----- ephraim_conn.erl | 3 +++ 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 -> -- cgit v1.2.3