summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2010-10-05 04:06:52 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2010-10-05 04:06:52 +0200
commite6d184833a8c46d3ecd3a407c66c561bb0d47123 (patch)
treec4b1a36bdbc71a12add3bb5871a2c8e6fe7ec2f2
parent3d451b3dc69d91b04c1153f63436b63940287b13 (diff)
downloadephraim-e6d184833a8c46d3ecd3a407c66c561bb0d47123.tar
ephraim-e6d184833a8c46d3ecd3a407c66c561bb0d47123.zip
Don't connect before all event handlers are set up
-rw-r--r--src/core/ephraim_account.erl3
-rw-r--r--src/core/ephraim_conn.erl72
-rw-r--r--src/core/ephraim_roster.erl2
3 files changed, 45 insertions, 32 deletions
diff --git a/src/core/ephraim_account.erl b/src/core/ephraim_account.erl
index 2e95f88..b71cb08 100644
--- a/src/core/ephraim_account.erl
+++ b/src/core/ephraim_account.erl
@@ -13,6 +13,9 @@ start_link(Account) ->
{ok, Connection} = supervisor:start_child(Pid, {connection, {ephraim_conn, start_link, [EventManager]}, permanent, 10000, worker, [ephraim_conn]}),
{ok, Roster} = supervisor:start_child(Pid, {roster, {ephraim_roster, start_link, [EventManager, Connection]}, permanent, 1000, worker, [ephraim_roster]}),
{ok, _} = supervisor:start_child(Pid, {conv_man, {ephraim_conv_man, start_link, [EventManager, Connection, Roster]}, permanent, 1000, supervisor, [ephraim_conv_man]}),
+
+ ephraim_conn:connect(Connection),
+
{ok, Pid}.
init(_Args) ->
diff --git a/src/core/ephraim_conn.erl b/src/core/ephraim_conn.erl
index fa9344d..308f385 100644
--- a/src/core/ephraim_conn.erl
+++ b/src/core/ephraim_conn.erl
@@ -1,10 +1,11 @@
-module(ephraim_conn).
-include_lib("exmpp/include/exmpp_client.hrl").
-include_lib("exmpp/include/exmpp_xml.hrl").
--behaviour(gen_server).
+-behaviour(gen_fsm).
-export([start_link/1]).
--export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
--export([send_packet/2]).
+-export([init/1, handle_event/3, handle_sync_event/4, handle_info/3, terminate/3, code_change/4]).
+-export([connected/2, disconnected/2]).
+-export([connect/1, send_packet/2]).
-record(conn_state, {
event_manager :: pid(),
@@ -12,55 +13,66 @@
}).
start_link(EventManager) ->
- gen_server:start_link(?MODULE, EventManager, []).
+ gen_fsm:start_link(?MODULE, EventManager, []).
+
+connect(Connection) ->
+ gen_fsm:send_event(Connection, connect).
+send_packet(Connection, Packet) ->
+ gen_fsm:send_event(Connection, {send_packet, Packet}).
%-spec init() -> ok.
init(EventManager) ->
process_flag(trap_exit, true),
- Session = exmpp_session:start(),
-
+ {ok, disconnected, #conn_state{event_manager=EventManager}}.
+
+disconnected(connect, Data) ->
{jid, JID} = ephraim_config:get(jid),
{password, Password} = ephraim_config:get(password),
{server, Server, Port} = ephraim_config:get(server),
-
+
+ Session = exmpp_session:start(),
exmpp_session:auth_basic_digest(Session, exmpp_jid:parse(JID), Password),
io:format("Connecting...~n"),
exmpp_session:connect_TCP(Session, Server, Port),
io:format("Connected.~n"),
- session(Session),
- {ok, #conn_state{event_manager=EventManager,session=Session}}.
--spec session(any()) -> ok.
-session(Session) ->
io:format("Logging in...~n"),
exmpp_session:login(Session),
io:format("Getting profile...~n"),
exmpp_session:send_packet(Session, exmpp_iq:get('jabber:client', #xmlel{ns='vcard-temp',name='vCard'})),
+ io:format("Getting roster...~n"),
+ exmpp_session:send_packet(Session, exmpp_client_roster:get_roster("foo")),
io:format("Setting presence...~n"),
exmpp_session:send_packet(Session, exmpp_presence:set_status(exmpp_presence:available(), "Foo/Test\\Bar")),
- io:format("Ok.~n").
-
-send_packet(Connection, Packet) ->
- gen_server:cast(Connection, {send_packet, Packet}).
+ io:format("Ok.~n"),
+ {next_state,connected,Data#conn_state{session=Session}};
+disconnected({send_packet,_}, _Data) ->
+ erlang:error(disconnected).
-handle_call(_Msg, _From, State) ->
- {noreply, State}.
+connected(connect, Data) ->
+ {next_state,connected,Data};
+connected({send_packet, Packet}, Data) ->
+ exmpp_session:send_packet(Data#conn_state.session, Packet),
+ {next_state,connected,Data}.
-handle_cast({send_packet, Packet}, State) ->
- exmpp_session:send_packet(State#conn_state.session, Packet),
- {noreply, State}.
-
-handle_info(Msg = #received_packet{}, State) ->
- gen_event:notify(State#conn_state.event_manager, {received_packet,Msg}),
- {noreply, State};
-handle_info(Msg, State) ->
+handle_info(Msg = #received_packet{}, State, Data) ->
+ gen_event:notify(Data#conn_state.event_manager, {received_packet,Msg}),
+ {next_state, State, Data};
+handle_info(Msg, State, Data) ->
io:format("ephraim_conn: ~p~n", [Msg]),
- {noreply, State}.
+ {next_state, State, Data}.
+
+handle_event(_Event, _State, _Data) ->
+ erlang:error(handle_event).
+handle_sync_event(_Event, _From, _State, _Data) ->
+ erlang:error(handle_sync_event).
-terminate(_Reason, State) ->
+terminate(_Reason, connected, Data) ->
io:format("Disconnecting...~n"),
- exmpp_session:stop(State#conn_state.session).
+ exmpp_session:stop(Data#conn_state.session);
+terminate(_Reason, _State, _Data) ->
+ ok.
-code_change(_OldVersion, State, _Extra) ->
- {ok, State}.
+code_change(_OldVersion, State, Data, _Extra) ->
+ {ok, State, Data}.
diff --git a/src/core/ephraim_roster.erl b/src/core/ephraim_roster.erl
index 6c6a229..955b108 100644
--- a/src/core/ephraim_roster.erl
+++ b/src/core/ephraim_roster.erl
@@ -40,8 +40,6 @@ init({EventManager, Connection}) ->
process_flag(trap_exit, true),
ephraim_roster_handler:start(EventManager, self()),
- io:format("Getting roster...~n"),
- ephraim_conn:send_packet(Connection, exmpp_client_roster:get_roster("foo")),
{ok, #roster_state{event_manager=EventManager,connection=Connection,entries=dict:new()}}.
-spec updateResource(#roster_state{}, binary(), integer(), atom(), atom(), binary()) -> #roster_state{}.