Use maps for headers

This commit is contained in:
Vincent Ambo 2016-02-13 15:30:29 +01:00
parent 763bae8a61
commit ad0d127ab3
No known key found for this signature in database
GPG key ID: 66F505681DB8F43B
3 changed files with 28 additions and 28 deletions

View file

@ -1,6 +1,6 @@
PROJECT = stomp.erl PROJECT = stomp
PROJECT_DESCRIPTION = New project PROJECT_DESCRIPTION = STOMP client for Erlang
PROJECT_VERSION = 0.0.1 PROJECT_VERSION = 0.1.0
# Whitespace to be used when creating files from templates. # Whitespace to be used when creating files from templates.
SP = 4 SP = 4

View file

@ -4,4 +4,4 @@
{registered, [stomp_worker]}, {registered, [stomp_worker]},
{env, []}, {env, []},
{applications, [kernel, stdlib]}, {applications, [kernel, stdlib]},
{mod, {stomp_app, []}}]. {mod, {stomp_app, []}}]}.

View file

@ -12,6 +12,9 @@
-export([terminate/2]). -export([terminate/2]).
-export([code_change/3]). -export([code_change/3]).
%% Testing
-compile(export_all).
-include("stomp.hrl"). -include("stomp.hrl").
%% State of a stomp_worker %% State of a stomp_worker
@ -69,7 +72,7 @@ handle_call({subscribe, Dest, Ack}, From, State) ->
handle_call(_Req, _From, State) -> handle_call(_Req, _From, State) ->
{reply, ignored, State}. {reply, ignored, State}.
handle_info({tcp, Conn, Frame}, State#state{connection = Conn}) -> handle_info({tcp, Conn, Frame}, State) when Conn =:= State#state.connection ->
handle_frame(Frame, State); handle_frame(Frame, State);
handle_info(_Msg, State) -> handle_info(_Msg, State) ->
{noreply, State}. {noreply, State}.
@ -110,10 +113,7 @@ subscribe(Socket, Id, Queue, Ack) ->
%%% Parsing STOMP frames %%% Parsing STOMP frames
handle_frame(<<"MESSAGE", "\n", Frame/binary>>, handle_frame(<<"MESSAGE", "\n", _Frame/binary>>, State) ->
#state{subscribers = Subscribers,
subscriptions = Subscriptions}) ->
{noreply, State}; {noreply, State};
handle_frame(Frame, State) -> handle_frame(Frame, State) ->
io:format("Received unknown frame ~p", [Frame]), io:format("Received unknown frame ~p", [Frame]),
@ -134,15 +134,15 @@ parse_headers(HeadersBin) ->
%% Format a header %% Format a header
-spec format_header({binary(), binary()}) -> binary(). -spec format_header({binary(), binary()}) -> binary().
format_header({Key, Val}) -> format_header({Key, Val}) ->
<<Key, ":", Val, "\n">>. <<Key/binary, ":", Val/binary, "\n">>.
%% Build a single STOMP frame %% Build a single STOMP frame
-spec make_frame(binary(), -spec make_frame(binary(),
list({binary(), binary()}), headers(),
binary()) binary())
-> {ok, iolist()}. -> {ok, iolist()}.
make_frame(Command, HeaderMap, Body) -> make_frame(Command, HeaderMap, Body) ->
Headers = lists:map(fun format_header/1, HeaderMap), Headers = lists:map(fun format_header/1, maps:to_list(HeaderMap)),
Frame = [Command, <<"\n">>, Headers, <<"\n">>, Body, <<0>>], Frame = [Command, <<"\n">>, Headers, <<"\n">>, Body, <<0>>],
{ok, Frame}. {ok, Frame}.
@ -151,31 +151,31 @@ make_frame(Command, HeaderMap, Body) ->
-spec connect_frame(list(), any(), any()) -> iolist(). -spec connect_frame(list(), any(), any()) -> iolist().
connect_frame(Host, {ok, Login}, {ok, Pass}) -> connect_frame(Host, {ok, Login}, {ok, Pass}) ->
make_frame(<<"CONNECT">>, make_frame(<<"CONNECT">>,
[{"accept-version", "1.2"}, #{<<"accept-version">> => <<"1.2">>,
{"host", Host}, <<"host">> => Host,
{"login", Login}, <<"login">> => Login,
{"passcode", Pass}, <<"passcode">> => Pass,
{"heart-beat", "0,5000"}], <<"heart-beat">> => <<"0,5000">>},
[]); []);
connect_frame(Host, _Login, _Pass) -> connect_frame(Host, _Login, _Pass) ->
make_frame(<<"CONNECT">>, make_frame(<<"CONNECT">>,
[{"accept-version", "1.2"}, #{<<"accept-version">> => <<"1.2">>,
{"host", Host}, <<"host">> => Host,
%% Expect a server heartbeat every 5 seconds, let the server %% Expect a server heartbeat every 5 seconds, let the server
%% expect one every 10. We don't actually check this and just %% expect one every 10. We don't actually check this and just
%% echo server heartbeats. %% echo server heartbeats.
%% TODO: For now the server is told not to expect replies due to %% TODO: For now the server is told not to expect replies due to
%% a weird behaviour. %% a weird behaviour.
{"heart-beat", "0,5000"}], <<"heart-beat">> => <<"0,5000">>},
[]). []).
-spec subscribe_frame(sub_id(), destination(), ack_mode()) -> iolist(). -spec subscribe_frame(sub_id(), destination(), ack_mode()) -> iolist().
subscribe_frame(Id, Queue, Ack) -> subscribe_frame(Id, Queue, Ack) ->
make_frame(<<"SUBSCRIBE">>, make_frame(<<"SUBSCRIBE">>,
[{"id", integer_to_binary(Id)}, #{<<"id">> => integer_to_binary(Id),
{"destination", Queue}, <<"destination">> => Queue,
{"ack", ack_mode_to_binary(Ack)}], <<"ack">> => ack_mode_to_binary(Ack)},
[]). []).
-spec ack_mode_to_binary(ack_mode()) -> binary(). -spec ack_mode_to_binary(ack_mode()) -> binary().