Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Almost have it ready with a sample page

  • Loading branch information...
commit 6a3d247e33bfdb9e9599a1e2ffe5b5b126d255ff 1 parent fe0c2c7
@nisbus authored
View
2  ebin/monterl_carlo_websocket.app
@@ -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]},
View
12 src/monterl_carlo_websocket.erl
@@ -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}]),
View
42 src/monterl_carlo_websocket_handler.erl
@@ -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) ->
@@ -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);
@@ -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),
@@ -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}};
@@ -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.
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.