Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Stopped the clients before storing the data

  • Loading branch information...
commit a3c41725cffe581ade7104b5b3dd1a2e332c9a14 1 parent bda9091
@ericmoritz ericmoritz authored
View
4 runtest
@@ -19,8 +19,10 @@ main([LogFile, SecondsStr, Server, PortStr, ClientsStr]) ->
wsdemo_stats:start_link(Server, Port, Clients),
timer:sleep(Seconds * 1000),
- io:format("Finished, syncing stats~n", []),
+ io:format("Finished, stopping clients~n", []),
+ wsdemo_stats:stop(),
+ io:format("Writing Stats~n", []),
wsdemo_logger:close(),
ok;
main([LogFile, SecondsStr, ClientsStr]) ->
View
7 src/websocket_client.erl
@@ -32,7 +32,7 @@
-export([behaviour_info/1]).
behaviour_info(callbacks) ->
- [{ws_init,0}, {ws_onopen,2}, {ws_onmessage,3}, {ws_info,3}, {ws_onclose,2}];
+ [{ws_init,0}, {ws_onopen,2}, {ws_onmessage,3}, {ws_info,3}, {ws_onclose,3}];
behaviour_info(_) ->
undefined.
@@ -68,7 +68,7 @@ handle_cast({send,Data}, State) ->
{noreply, State};
handle_cast(close,State) ->
Mod = State#state.callback,
- CBState = Mod:ws_onclose(State#state.callback_state),
+ CBState = Mod:ws_onclose(State, normal, State#state.callback_state),
gen_tcp:close(State#state.socket),
State1 = State#state{readystate=?CLOSED, callback_state=CBState},
{stop,normal,State1}.
@@ -140,7 +140,7 @@ handle_info({tcp, _Socket, Data}, #state{callback=Mod, sofar=SoFar} = State) ->
end;
handle_info({tcp_closed, _Socket},State) ->
Mod = State#state.callback,
- CBState = Mod:ws_onclose(State, State#state.callback_state),
+ CBState = Mod:ws_onclose(State, tcp_closed, State#state.callback_state),
{stop,normal,State#state{callback_state=CBState}};
handle_info({tcp_error, _Socket, _Reason},State) ->
{stop,tcp_error,State};
@@ -153,7 +153,6 @@ handle_call(_Request,_From,State) ->
{reply,ok,State}.
terminate(Reason, _State) ->
- error_logger:info_msg("Websocket Client Terminated ~p~n",[Reason]),
ok.
code_change(_OldVsn, State, _Extra) ->
View
10 src/wsdemo_client.erl
@@ -4,7 +4,7 @@
-export([start_link/0, start_link/2, start_link/3]).
--export([ws_init/0, ws_onopen/2, ws_onmessage/3, ws_info/3, ws_onclose/2]).
+-export([ws_init/0, ws_onopen/2, ws_onmessage/3, ws_info/3, ws_onclose/3]).
-record(state, {start_time}).
@@ -55,8 +55,12 @@ ws_info(Client, {timeout, _Ref, send_ping}, State) ->
erlang:start_timer(1000, self(), send_ping),
State.
-ws_onclose(Client, State) ->
- error_logger:info_msg("~p~n", [Client]),
+ws_onclose(_Client, tcp_closed, State) ->
+ % Server disconnected
wsdemo_logger:event({ws_onclose, self()}),
+ State;
+ws_onclose(_Client, normal, State) ->
+ % We told the client to close
State.
+
View
18 src/wsdemo_stats.erl
@@ -7,6 +7,7 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
+-record(state, {clients=[]}).
start_link(Clients) ->
start_link("localhost", 8000, Clients).
@@ -29,17 +30,21 @@ start_clients(Hostname, Port, Clients) ->
start_clients(Hostname, Port, Clients-1).
init([Hostname, Port, Clients]) ->
- process_flag(trap_exit, true),
+ process_flag(trap_exit, false),
spawn(fun() -> start_clients(Hostname, Port, Clients) end),
- {ok, no_state}.
+ {ok, #state{}}.
-handle_call(stop, _From, State) ->
+handle_call(stop, _From, State) ->
+ lists:foreach(fun(Pid) ->
+ websocket_client:close(Pid)
+ end, State#state.clients),
{stop, normal, ok, State}.
-handle_cast({start_client, Hostname, Port}, State) ->
- wsdemo_client:start_link(Hostname, Port),
- {noreply, State};
+handle_cast({start_client, Hostname, Port}, #state{clients=Pids} = State) ->
+ {ok, Pid} = wsdemo_client:start_link(Hostname, Port),
+
+ {noreply, State#state{clients=[Pid|Pids]}};
handle_cast(_Msg, State) ->
{noreply, State}.
@@ -53,4 +58,3 @@ code_change(_OldVsn, State, _Extra) ->
terminate(_Reason, _State) ->
ok.
-
Please sign in to comment.
Something went wrong with that request. Please try again.