Permalink
Browse files

Work in progress on yaxs_core server for tracking sessions and routin…

…g stanzas...
  • Loading branch information...
1 parent 80eca15 commit 7a77500b15c39db125e414e1098e11fca2011399 @kaos committed Apr 29, 2009
View
@@ -29,6 +29,7 @@
}).
-record(stanza, {
+ kind,
type,
to,
from,
View
@@ -0,0 +1,110 @@
+%%%-------------------------------------------------------------------
+%%% File : yaxs_core.erl
+%%% Author : Andreas Stenius <kaos@astekk.se>
+%%% Description :
+%%%
+%%% Created : 29 Apr 2009 by Andreas Stenius <kaos@astekk.se>
+%%%-------------------------------------------------------------------
+-module(yaxs_core).
+
+-behaviour(gen_server).
+
+%% API
+-export([
+ start_link/0,
+
+ new_session/1,
+ route_stanza/2
+ ]).
+
+%% gen_server callbacks
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+-record(state, {}).
+
+-define(SERVER, ?MODULE).
+
+%%====================================================================
+%% API
+%%====================================================================
+%%--------------------------------------------------------------------
+%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
+%% Description: Starts the server
+%%--------------------------------------------------------------------
+start_link() ->
+ gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
+
+new_session(Jid) ->
+ error_logger:info_msg("new_session: ~s~n", [Jid]),
+ ok.
+
+route_stanza(Stanza, _Client) ->
+ error_logger:info_msg("route_stanza: ~p~n", [Stanza]),
+ ok.
+
+%%====================================================================
+%% gen_server callbacks
+%%====================================================================
+
+%%--------------------------------------------------------------------
+%% Function: init(Args) -> {ok, State} |
+%% {ok, State, Timeout} |
+%% ignore |
+%% {stop, Reason}
+%% Description: Initiates the server
+%%--------------------------------------------------------------------
+init([]) ->
+ {ok, #state{}}.
+
+%%--------------------------------------------------------------------
+%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
+%% {reply, Reply, State, Timeout} |
+%% {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, Reply, State} |
+%% {stop, Reason, State}
+%% Description: Handling call messages
+%%--------------------------------------------------------------------
+handle_call(_Request, _From, State) ->
+ Reply = ok,
+ {reply, Reply, State}.
+
+%%--------------------------------------------------------------------
+%% Function: handle_cast(Msg, State) -> {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, State}
+%% Description: Handling cast messages
+%%--------------------------------------------------------------------
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+%%--------------------------------------------------------------------
+%% Function: handle_info(Info, State) -> {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, State}
+%% Description: Handling all non call/cast messages
+%%--------------------------------------------------------------------
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+%%--------------------------------------------------------------------
+%% Function: terminate(Reason, State) -> void()
+%% Description: This function is called by a gen_server when it is about to
+%% terminate. It should be the opposite of Module:init/1 and do any necessary
+%% cleaning up. When it returns, the gen_server terminates with Reason.
+%% The return value is ignored.
+%%--------------------------------------------------------------------
+terminate(_Reason, _State) ->
+ ok.
+
+%%--------------------------------------------------------------------
+%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
+%% Description: Convert process state when code is changed
+%%--------------------------------------------------------------------
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%%--------------------------------------------------------------------
+%%% Internal functions
+%%--------------------------------------------------------------------
@@ -43,7 +43,7 @@ handle(#tag{ name="bind", body=Body },
#yaxs_client{ response=_R, tags=Tags } = Client ) ->
case proplists:get_value(bind, Tags) of
undefined ->
- bind_to_resource(Body, Client);
+ bind_resource(Body, Client);
Res ->
{tag, {already_bound, Res}}
end.
@@ -53,10 +53,22 @@ handle(#tag{ name="bind", body=Body },
%% Internal functions
%%====================================================================
-bind_to_resource([#tag{ name="resource",
+bind_resource([#tag{ name="resource",
body=[Resource] }],
- _Client) ->
- {tag, {bind, Resource}};
-bind_to_resource(_, _Client) ->
- {tag, {bind, "generated-resource-name"}}.
+ Client) ->
+ do_bind_resource(Resource, Client);
+bind_resource(_, Client) ->
+ do_bind_resource("generated-resource-name", Client).
+
+do_bind_resource(Resource, #yaxs_client{ tags = Tags }) ->
+ yaxs_core:new_session(
+ io_lib:format("~s@~s/~s",
+ [
+ proplists:get_value(user, Tags),
+ proplists:get_value(domain, Tags),
+ Resource
+ ])
+ ),
+
+ {tag, {bind, Resource}}.
@@ -16,6 +16,8 @@
handle/2
]).
+-import(yaxs, [get_attr/2, get_attr/3]).
+
%%====================================================================
%% API
%%====================================================================
@@ -30,14 +32,28 @@ init() ->
]).
handle(#tag{ namespace = "jabber:client",
- name = Type,
-% attrs = Attrs,
+ name = Kind,
+ attrs = Attrs,
body = Body },
#yaxs_client{ response=_R } = Client) ->
-
- yaxs_event:publish(#stanza{ type=Type, body=Body}, Client).
+
+ Stanza = #stanza{ kind = Kind,
+ type = get_attr("type", Attrs),
+ to = get_attr("to", Attrs, local),
+ from = get_attr("from", Attrs),
+ id = get_attr("id", Attrs),
+ xml_lang = get_attr("xml:lang", Attrs),
+ body = Body },
+
+ case Stanza#stanza.to of
+ local ->
+ yaxs_event:publish(Stanza, Client);
+ _ ->
+ yaxs_core:route_stanza(Stanza, Client)
+ end.
%%====================================================================
%% Internal functions
%%====================================================================
+
@@ -36,7 +36,7 @@ handle({'SASL/DIGEST-MD5', _Tag},
#yaxs_client{ response=R } = _Client) ->
R("challenge"),
R(reset_stream),
- {tag, {sasl, ok}}.
+ {tag, [{sasl, ok}, {user, "foo"}]}.
%%====================================================================
@@ -46,7 +46,8 @@ handle(#tag{ attrs=Attrs },
Res = yaxs_event:publish(stream_features, Client),
R("</stream:features>"),
- Res.
+ [{tag, {domain, yaxs:get_attr("to", Attrs)}}
+ |Res].
%%====================================================================
View
@@ -1,33 +1,27 @@
-module(yaxs).
-export([
start/0,
- stop/0
+ stop/0,
+
+ get_attr/2,
+ get_attr/3
- %% API
-% register_feature/1
]).
-%-include("xmpp.hrl").
+-include("yaxs.hrl").
start() ->
application:start(yaxs).
stop() ->
application:stop(yaxs).
-
-%% API
-
-%% register_feature(F) when is_record(F, feature) ->
-%% xmpp_core_stream_feature:register(F);
-%% register_feature(F) ->
-%% register_feature(
-%% #feature{ states = proplists:get_value(
-%% states, F, [bound]),
-%% module = proplists:get_value(
-%% module, F),
-%% namespace = proplists:get_value(
-%% namespace, F, 'jabber:client')
-%% }).
-
+get_attr(Key, Attrs) -> get_attr(Key, Attrs, undefined).
+get_attr(Key, Attrs, Default) ->
+ case [V || #attribute{ name=K, value=V } <- Attrs, K == Key] of
+ [Val] ->
+ Val;
+ _ ->
+ Default
+ end.
View
@@ -127,8 +127,13 @@ wait_for_stream({sax, {open, {"http://etherx.jabber.org/streams"=NS,
wait_for_stream({sax, _Event}, State) ->
{next_state, wait_for_stream, State}.
-setup_stream(reset_stream, State) ->
- {next_state, wait_for_stream, State#state{ sax=undefined, open_tags=[] }};
+setup_stream(reset_stream, #state{ client=#yaxs_client{ tags=Tags }=Client } = State) ->
+ {next_state, wait_for_stream,
+ State#state{ sax=undefined,
+ open_tags=[],
+ client=Client#yaxs_client{ tags=proplists:delete(domain, Tags) }
+ }
+ };
setup_stream({sax, {open, {NS, P, N, A}}}, #state{ open_tags = Tags } = State) ->
{next_state, setup_stream,

0 comments on commit 7a77500

Please sign in to comment.