Skip to content

Commit

Permalink
Almost have it ready with a sample page
Browse files Browse the repository at this point in the history
  • Loading branch information
nisbus committed Nov 1, 2012
1 parent fe0c2c7 commit 6a3d247
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ebin/monterl_carlo_websocket.app
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[{description,[]},
{vsn,"1"},
{registered,[]},
{modules,[monterl_carlo_websocket,
{modules,[monterl_carlo_static_handler,monterl_carlo_websocket,
monterl_carlo_websocket_handler,
monterl_carlo_websocket_sup]},
{applications,[kernel,stdlib,inets,crypto,jsx,cowboy]},
Expand Down
12 changes: 10 additions & 2 deletions src/monterl_carlo_websocket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ start(_StartType, _StartArgs) ->
%% Dispatch the requests (whatever the host is) to
%% erws_handler, without any additional options.
Dispatch = [{'_', [
{'_', monterl_carlo_websocket_handler, []}
]}],
{[<<"index.htm">>,'...'], cowboy_static,
[ {directory, {priv_dir, monterl_carlo_websocket,[]}},
{file,<<"index.htm">>},
{mimetypes, [{<<".html">>, [<<"text/html">>]}]}]},
{[<<"flot.jquery.js">>,'...'],cowboy_static,
[ {directory, {priv_dir, monterl_carlo_websocket,[]}},
{file,<<"flot.jquery.js">>},
{mimetypes, [{<<".js">>, [<<"text/javascript">>]}]}]},
{'_', monterl_carlo_websocket_handler, []}
]}],
%% Name, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
%% Listen in 10100/tcp for http connections.
cowboy:start_http(http, 100, [{port, 8080}],[{dispatch, Dispatch}]),
Expand Down
42 changes: 34 additions & 8 deletions src/monterl_carlo_websocket_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ websocket_handle({text, Msg}, Req, State) ->
true ->
handle_message(Msg,Req,State);
false ->

{ok, Req, State}
end;

websocket_handle(_Any, Req, State) ->

{ok, Req, State}.

websocket_info({send,Message}, Req, State) ->
websocket_info({send,Message}, Req, State) ->
io:format("Sending: ~p~n",[Message]),
{reply, {text, Message}, Req, State};

websocket_info(_Info, Req, State) ->
Expand All @@ -59,6 +62,7 @@ websocket_terminate(_Reason, _Req, _State) ->
ok.

handle_message(Msg,Req,#state{callback = Callback} = State) ->
io:format("Received message ~p~n",[Msg]),
Props = jsx:json_to_term(Msg),
Type = case is_list(Props) of
true -> proplists:get_value(<<"type">>,Props,undefined);
Expand All @@ -67,7 +71,7 @@ handle_message(Msg,Req,#state{callback = Callback} = State) ->
case Type of
<<"graph">> ->
Symbol = proplists:get_value(<<"symbol">>,Props),
Points = proplists:get_value(<<"points">>,Props,50),
Points = to_int(proplists:get_value(<<"points">>,Props,50)),
GraphType = proplists:get_value(<<"graph_type">>,Props,<<"both">>),
Resp = monterl_carlo:graph(Symbol,Points,GraphType),
Callback(Resp),
Expand All @@ -76,13 +80,14 @@ handle_message(Msg,Req,#state{callback = Callback} = State) ->
Symbol = proplists:get_value(<<"symbol">>,Props),
monterl_carlo:subscribe(Symbol,Callback),
{ok, Req, State};
<<"start">> ->
<<"start">> ->
Symbol = proplists:get_value(<<"symbol">>,Props),
Px = proplists:get_value(<<"price">>,Props,100.50),
Precision = proplists:get_value(<<"precision">>,Props,5),
Annual_Vol = proplists:get_value(<<"annual_volatility">>,Props,0.1),
AnnualExpRet = proplists:get_value(<<"annual_expected_returns">>,Props,0.1),
Interval = proplists:get_value(<<"interval">>,Props,1000),
Px = to_float(proplists:get_value(<<"price">>,Props,100.50)),
Precision = to_int(proplists:get_value(<<"precision">>,Props,5)),
Annual_Vol = to_float(proplists:get_value(<<"annual_volatility">>,Props,0.1)),
AnnualExpRet = to_float(proplists:get_value(<<"annual_expected_returns">>,Props,0.1)),
Interval = to_int(proplists:get_value(<<"interval">>,Props,1000)),
io:format("Creating sim ~p, ~p, ~p, ~p, ~p, ~p~n",[Symbol,Px,Precision,Annual_Vol,AnnualExpRet,Interval]),
Resp = monterl_carlo:start_link(Symbol,Px,Precision,Annual_Vol,AnnualExpRet,Interval),
{ok, Pid} = Resp,
{ok, Req, State#state{sim_pid=Pid}};
Expand All @@ -98,3 +103,24 @@ handle_message(Msg,Req,#state{callback = Callback} = State) ->
io:format("Ignoring request~n"),
{ok, Req, State}
end.

to_int(Val) when is_binary(Val) ->
to_int(binary_to_list(Val));
to_int(Val) when is_list(Val) ->
list_to_integer(Val);
to_int(Val) when is_integer(Val) ->
Val;
to_int(_) ->
0.

to_float(Val) when is_binary(Val) ->
to_float(binary_to_list(Val));
to_float(Val) when is_list(Val) ->
list_to_float(Val);
to_float(Val) when is_float(Val) ->
Val;
to_float(_) ->
0.0.



0 comments on commit 6a3d247

Please sign in to comment.