Skip to content
Browse files

yaxs_mod stuff in place..

  • Loading branch information...
1 parent 39c1d36 commit 1181aeeb5cf3815d301f4627a84e3538db00a44a @kaos committed Apr 24, 2009
Showing with 172 additions and 29 deletions.
  1. +16 −4 ebin/yaxs.app
  2. +8 −1 include/yaxs.hrl
  3. +23 −4 src/core/yaxs_core_stream.erl
  4. +24 −0 src/test/yaxs_SUITE.erl
  5. +54 −13 src/yaxs_client.erl
  6. +2 −0 src/yaxs_con.erl
  7. +26 −3 src/yaxs_event.erl
  8. +19 −4 src/yaxs_sax.erl
View
20 ebin/yaxs.app
@@ -2,14 +2,26 @@
{ application, yaxs,
[{ description, "Yet Another XMPP Server" },
{ vsn, "0.1" },
- { modules, [ yaxs, yaxs_app, yaxs_sup, yaxs_con, yaxs_client ]},
- { registered, [ yaxs_sup, yaxs_con, yaxs_client_sup ]},
- { applications, [ kernel, stdlib, sasl, erlsom ]},
+ { modules, [
+ yaxs, yaxs_app, yaxs_sup, yaxs_con, yaxs_client,
+ yaxs_core_stream, yaxs_core_tls, yaxs_core_sasl,
+ yaxs_core_sasl_digest_md5
+ ]},
+ { registered, [
+ yaxs_sup, yaxs_con, yaxs_client_sup,
+ yaxs_event
+ ]},
+ { applications, [
+ kernel, stdlib, sasl, erlsom
+ ]},
{ mod, { yaxs_app, []}},
{ start_phases, []},
{ env, [
{mods, [
- yaxs_core_stream
+ yaxs_core_stream,
+ yaxs_core_tls,
+ yaxs_core_sasl,
+ yaxs_core_sasl_digest_md5
]}
]}
]
View
9 include/yaxs.hrl
@@ -9,5 +9,12 @@
sock,
addr,
jid,
- tags=[]
+ tags=[],
+ response
}).
+
+-record(tag, {
+ tag,
+ body=[]
+ }).
+
View
27 src/core/yaxs_core_stream.erl
@@ -7,6 +7,8 @@
%%%-------------------------------------------------------------------
-module(yaxs_core_stream).
+-include("yaxs.hrl").
+
%% API
-behaviour(yaxs_mod).
-export([
@@ -23,12 +25,29 @@
%%--------------------------------------------------------------------
init() ->
- error_logger:info_msg("core init"),
- yaxs_mod:register(?MODULE, [open_stream]).
+ yaxs_mod:register(?MODULE, [
+ "http://etherx.jabber.org/streams"
+ ]).
-handle({open_stream, Attrs}, Client) ->
+handle({"http://etherx.jabber.org/streams", "stream", "stream", Attrs},
+ #yaxs_client{ response=R } = Client) ->
error_logger:info_msg("core open_stream: ~p~n", [Attrs]),
- ok.
+ R("\
+<?xml version='1.0'?>\
+<stream:stream\
+ from='example.com'\
+ id='someid'\
+ xmlns='jabber:client'\
+ xmlns:stream='http://etherx.jabber.org/streams'\
+ version='1.0'>
+
+ <stream:features>"),
+
+ Tags = yaxs_event:publish(stream_features, Client),
+
+ R("</stream:features>"),
+ {tag, Tags}.
+
%%====================================================================
%% Internal functions
View
24 src/test/yaxs_SUITE.erl
@@ -185,5 +185,29 @@ test_stream(_Config) ->
xmlns:stream='http://etherx.jabber.org/streams'
version='1.0'>
"),
+
+%% Expect = "\
+%% <?xml version='1.0'?>\
+%% <stream:stream\
+%% from='example.com'\
+%% id='someid'\
+%% xmlns='jabber:client'\
+%% xmlns:stream='http://etherx.jabber.org/streams'\
+%% version='1.0'>",
+
+ {ok, Data} = gen_tcp:recv(Sock, 0, 1000),
+ ct:pal("Response:~n~s~n", [Data]),
+%% case Data of
+%% Expect -> ok;
+%% Else -> ct:fail("Expected~n~p~n~nGot~n~p~n", [Expect, Data])
+%% end,
+
+ ok = gen_tcp:send(Sock, "
+<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>"
+ ),
+
+ {ok, Auth} = gen_tcp:recv(Sock, 0, 1000),
+ ct:pal("Auth Response:~n~s~n", [Auth]),
+
{error, timeout} = gen_tcp:recv(Sock, 0, 500),
ok = gen_tcp:close(Sock).
View
67 src/yaxs_client.erl
@@ -26,6 +26,7 @@
-export([
wait_for_socket/2,
wait_for_stream/2,
+ setup_stream/2,
streaming/2
]).
@@ -35,7 +36,9 @@
-record(state, {
sax,
- client = #yaxs_client{}
+ client = #yaxs_client{},
+ response=[],
+ open_tags=[]
}).
%%====================================================================
@@ -69,7 +72,15 @@ sax_event(Pid, Event) ->
%% initialize.
%%--------------------------------------------------------------------
init([]) ->
- {ok, wait_for_socket, #state{ client=#yaxs_client{ pid=self() }}}.
+ Pid=self(),
+ {ok, wait_for_socket,
+ #state{
+ client=#yaxs_client{
+ pid=Pid,
+ response=fun(Data) -> gen_fsm:send_all_state_event(Pid, {response, Data}) end
+ }
+ }
+ }.
%%--------------------------------------------------------------------
%% Function:
@@ -87,7 +98,7 @@ wait_for_socket({socket_ready, Sock}, #state{ client=Client} = State) ->
inet:setopts(Sock, [{active, once}]),
{ok, {IP, Port}} = inet:peername(Sock),
Addr = io_lib:format("~s:~p", [inet_parse:ntoa(IP), Port]),
- error_logger:info_msg("Client connected: ~s", [Addr]),
+ error_logger:info_msg("Client connected: ~s~n", [Addr]),
{next_state, wait_for_stream,
State#state{
@@ -98,16 +109,35 @@ wait_for_socket({socket_ready, Sock}, #state{ client=Client} = State) ->
}
}.
-wait_for_stream({sax, {open_stream, _Attrs}=Event}, State) ->
- yaxs_event:publish(Event, State#state.client),
- {next_state, streaming, State}.
+wait_for_stream({sax, {open, {"http://etherx.jabber.org/streams",
+ "stream", "stream", _}=Tag}}, State) ->
+ State1 = publish(Tag, State#state{ open_tags = [#tag{ tag=Tag }] }),
+ gen_fsm:send_all_state_event(self(), send_response),
+ {next_state, setup_stream, State1};
-streaming({sax, {open_stream, _Attrs}}, State) ->
- {next_state, streaming, State};
+wait_for_stream({sax, _Event}, State) ->
+ {next_state, wait_for_stream, State}.
-streaming({sax, close}, State) ->
- {stop, normal, State}.
+setup_stream({sax, {open, Tag}}, #state{ open_tags = Tags } = State) ->
+ {next_state, setup_stream,
+ State#state{ open_tags = [#tag{ tag=Tag }|Tags] }
+ };
+
+setup_stream({sax, {close, _Tag}}, #state{ open_tags = [OTag|Tags] } = State) ->
+ State1 = publish(OTag, State#state{ open_tags = Tags }),
+ gen_fsm:send_all_state_event(self(), send_response),
+ {next_state, setup_stream, State1};
+
+setup_stream({sax, close}, #state{ client=Client } = State) ->
+ gen_tcp:send(Client#yaxs_client.sock, "</stream:stream>"),
+ {stop, normal, State};
+
+setup_stream({sax, _Event}, State) ->
+ {next_state, setup_stream, State}.
+streaming({sax, close}, #state{ client=Client } = State) ->
+ gen_tcp:send(Client#yaxs_client.sock, "</stream:stream>"),
+ {stop, normal, State}.
%%--------------------------------------------------------------------
%% Function:
@@ -139,6 +169,13 @@ streaming({sax, close}, State) ->
%% gen_fsm:send_all_state_event/2, this function is called to handle
%% the event.
%%--------------------------------------------------------------------
+handle_event(send_response, StateName, #state{ response=Response, client=Client } = State) ->
+ gen_tcp:send(Client#yaxs_client.sock, lists:reverse(Response)),
+ {next_state, StateName, State#state{ response=[] }};
+
+handle_event({response, Data}, StateName, #state{ response=Response } = State) ->
+ {next_state, StateName, State#state{ response=[Data|Response] }};
+
handle_event(_Event, StateName, State) ->
{next_state, StateName, State}.
@@ -188,7 +225,7 @@ handle_info({tcp, Sock, Data}, StateName, State) ->
handle_info({tcp_closed, _Sock}, _StateName, State) ->
error_logger:info_msg("Client disconnected: ~s~n", [(State#state.client)#yaxs_client.addr]),
- {stop, normal, #state{}}.
+ {stop, normal, State}.
%%--------------------------------------------------------------------
%% Function: terminate(Reason, StateName, State) -> void()
@@ -198,8 +235,8 @@ handle_info({tcp_closed, _Sock}, _StateName, State) ->
%% Reason. The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _StateName, State) ->
- error_logger:info_msg("Terminate yaxs_client in state ~p.~nReason: ~p~n",
- [_StateName, _Reason]),
+ error_logger:info_msg("Terminate yaxs_client in state ~p.~nReason: ~p~nState:~p~n",
+ [_StateName, _Reason, State]),
case (State#state.client)#yaxs_client.sock of
undefined ->
ok;
@@ -219,3 +256,7 @@ code_change(_OldVsn, StateName, State, _Extra) ->
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
+
+publish(Event, #state{ client=#yaxs_client{ tags=Tags } = Client } = State) ->
+ NewTags = yaxs_event:publish(Event, Client),
+ State#state{ client = Client#yaxs_client{ tags=lists:flatten(NewTags) ++ Tags }}.
View
2 src/yaxs_con.erl
@@ -133,6 +133,8 @@ handle_info(_Info, State) ->
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, State) ->
+ error_logger:info_msg("Terminate yaxs_con.~nReason: ~p~n",
+ [_Reason]),
gen_tcp:close(State#state.sock),
ok.
View
29 src/yaxs_event.erl
@@ -21,6 +21,8 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
+-include("yaxs.hrl").
+
-record(state, {
% mods = [],
events = []
@@ -41,9 +43,15 @@ start_link() ->
register(Module, Events) ->
gen_server:cast(?SERVER, {register, Module, Events}).
-publish(Event, Client) ->
- Mods = gen_server:call(?SERVER, {list_mods, element(1, Event)}),
- [Mod:handle(Event, Client) || Mod <- Mods].
+publish(Event, Client) when is_record(Event, tag) ->
+ Mods = list_mods(element(1, Event#tag.tag)),
+ do_publish(Event, Client, Mods);
+publish(Event, Client) when is_tuple(Event) ->
+ Mods = list_mods(element(1, Event)),
+ do_publish(Event, Client, Mods);
+publish(Event, Client) when is_atom(Event) ->
+ Mods = list_mods(Event),
+ do_publish(Event, Client, Mods).
%%====================================================================
@@ -111,6 +119,8 @@ handle_info(_Info, State) ->
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
+ error_logger:info_msg("Terminate yaxs_event.~nReason: ~p~n",
+ [_Reason]),
ok.
%%--------------------------------------------------------------------
@@ -123,3 +133,16 @@ code_change(_OldVsn, State, _Extra) ->
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
+
+list_mods(Event) ->
+ gen_server:call(?SERVER, {list_mods, Event}).
+
+do_publish(Event, Client, Mods) ->
+ error_logger:info_msg("do publish:~nEvent:~p~nClient:~p~nMods:~p~n", [Event, Client, Mods]),
+ [element(2, Tag) || Tag <- lists:flatten(
+ [Mod:handle(Event, Client) || Mod <- Mods]
+ ),
+ is_tuple(Tag),
+ tuple_size(Tag) == 2,
+ element(1, Tag) == tag
+ ].
View
23 src/yaxs_sax.erl
@@ -50,15 +50,30 @@ parse(_, _, Sax) ->
%%====================================================================
%% stream tag
-sax_event({startElement, "http://etherx.jabber.org/streams",
- "stream", "stream", Attrs}, Fun) ->
- Fun({open_stream, Attrs}),
+
+%% sax_event({startElement, "http://etherx.jabber.org/streams",
+%% "stream", "stream", Attrs}, Fun) ->
+%% Fun({open_stream, Attrs}),
+%% Fun;
+
+%% sax_event({startElement, "urn:ietf:params:xlm:ns:xmpp-sasl",
+%% "auth", [], Attrs}, Fun) ->
+%% Fun({auth}),
+%% Fun;
+
+sax_event({startElement, Uri, Name, Prefix, Attrs}, Fun) ->
+ Fun({open, {Uri, Prefix, Name, Attrs}}),
+ Fun;
+
+sax_event({endElement, Uri, Name, Prefix}, Fun) ->
+ Fun({close, {Uri, Prefix, Name}}),
Fun;
sax_event(endDocument, Fun) ->
Fun(close),
Fun;
%% other
-sax_event(_Event, Fun) ->
+sax_event(Event, Fun) ->
+ Fun(Event),
Fun.

0 comments on commit 1181aee

Please sign in to comment.
Something went wrong with that request. Please try again.