Skip to content

Commit

Permalink
More wip
Browse files Browse the repository at this point in the history
  • Loading branch information
madtrick committed Apr 7, 2012
1 parent 74a6802 commit 5d1cb50
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
35 changes: 13 additions & 22 deletions src/wsecli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ start(Host, Port, Path)->

-spec stop() -> ok.
stop() ->
error_logger:info_msg("Calling stop on wsecli\n"),
%error_logger:info_msg("Calling stop on wsecli\n"),
gen_fsm:sync_send_all_state_event(wsecli, stop).

-spec send(Data::string()) -> ok;
Expand Down Expand Up @@ -57,7 +57,7 @@ on_close(Callback) ->
%
-spec init({Host::string(), Port::integer(), Resource::string()}) -> {ok, connecting, #data{}}.
init({Host, Port, Resource}) ->
error_logger:info_msg("Start wsecli \n"),
%error_logger:info_msg("Start wsecli \n"),
{ok, Socket} = gen_tcp:connect(Host, Port, [binary, {reuseaddr, true}, {packet, raw}] ),

Handshake = wsecli_handshake:build(Resource, Host, Port),
Expand Down Expand Up @@ -95,7 +95,7 @@ closing(Event, StateData) ->

-spec closed(Event::term(), StateData::#data{}) -> term().
closed(Event, StateData) ->
closed.
{stop, normal, StateData}.

%
% GEN_FSM behaviour callbacks
Expand All @@ -121,10 +121,7 @@ handle_sync_event(stop, _From, StateName, StateData) when StateName =:= closing
ok;

handle_sync_event(stop, _From, connecting, StateData) ->
gen_tcp:close(StateData#data.socket),
spawn(fun() -> (StateData#data.cb#callbacks.on_close)(undefined) end),
%{reply, {ok, closing}, closing, StateData};
{stop, normal, stop, StateData};
{reply, {ok, closing}, closed, StateData, 1};

handle_sync_event(stop, _From, open, StateData) ->
Message = wsecli_message:encode([], close),
Expand All @@ -151,39 +148,33 @@ handle_info({tcp, Socket, Data}, connecting, StateData) ->

handle_info({tcp, Socket, Data}, open, StateData) ->
%TODO: append previous fragmented message
error_logger:info_msg("Receiving in open state \n"),
%error_logger:info_msg("Receiving in open state \n"),
Messages = wsecli_message:decode(Data),
NewStateData = process_messages(Messages, StateData),
{next_state, open, NewStateData};

handle_info({tcp, Socket, Data}, closing, StateData) ->
error_logger:info_msg("Received msg in closing state \n"),
%error_logger:info_msg("Received msg in closing state \n"),
[Message] = wsecli_message:decode(Data),
case Message#message.type of
close ->
%TODO; Maybe move to closed state with timeout
%for tcp_close
spawn(fun() -> (StateData#data.cb#callbacks.on_close)(undefined) end),
{stop, normal, StateData};
{next_state, closed, StateData, 500};
_ ->
%Discard everything
{next_state, closing, StateData}
end;

%TODO: this should be on closed state, because if we receive a tcp_closed on
%closed state is an error on the comunication
handle_info({tcp_closed, _}, closing, StateData) ->
error_logger:info_msg("Received msg in tcp_closed closing \n"),
{stop, normal, StateData};
{next_state, closed, StateData, 1};

handle_info({tcp_closed, _}, StateName, StateData) ->
error_logger:info_msg("tcp closed in state ~w \n", [StateName]),
{stop, closed_socket, StateData}.
handle_info(_, closed, StateData) ->
{stop, normal, StateData}.

-spec terminate(Reason::atom(), StateName::atom(), #data{}) -> [].
terminate(_Reason, _StateName, StateData) ->
error_logger:error_msg("Closing wsecli socket \n"),
gen_tcp:close(StateData#data.socket).
%error_logger:error_msg("Closing wsecli socket \n"),
gen_tcp:close(StateData#data.socket),
spawn(fun() -> (StateData#data.cb#callbacks.on_close)(undefined) end).

code_change(OldVsn, StateName, StateData, Extra) ->
code_change.
Expand Down
11 changes: 4 additions & 7 deletions test/spec/wsecli_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ spec() ->
receive {mock_http_server, received_data} -> ok end,

assert_that(meck:called(gen_tcp, send, '_'), is(true)),
assert_that(meck:called(wsecli_message, encode, '_'), is(true)),
%assert_that(meck:called(wsecli_message, encode, '_'), is(true)),
cleanly_stop_wsecli(true),
meck:unload(wsecli_message)
end),
Expand Down Expand Up @@ -115,7 +115,7 @@ spec() ->
wsecli:start("localhost", 8080, "/"),
wsecli:on_message(fun(_Type,_Messae)-> Pid ! {Pid, on_message} end),
wsecli:on_open(fun() -> wsecli:send("Hello") end),

assert_that((fun() ->
receive
{Pid, on_message} ->
Expand Down Expand Up @@ -171,11 +171,8 @@ spec() ->

assert_that(meck:called(gen_tcp, close, '_') , is(true))
end)
end),
describe("on_close", fun() ->
it("should be called ")
end)
end).
end)
end).

cleanly_stop_wsecli(_) ->
Pid = self(),
Expand Down

0 comments on commit 5d1cb50

Please sign in to comment.