Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(ws_bank_handler).
- -behaviour(cowboy_http_handler).
- -behaviour(cowboy_websocket_handler).
- -export([init/3, handle/2, terminate/3]).
- -export([websocket_init/3]).
- -export([websocket_handle/3]).
- -export([websocket_info/3]).
- -export([websocket_terminate/3]).
- -export([add/2, delete/2, find/2, qs_transl/1, kv_lst/1,
- process_query/2, reply/2]).
- init({tcp, http}, _Req, _Opts) ->
- {upgrade, protocol, cowboy_websocket}.
- handle(_Req, State) ->
- {ok, Req2} = cowboy_http_req:reply(404, [{'Content-Type', <<"text/html">>}]),
- {ok, Req2, State}.
- websocket_init(_, Req, _Opts) ->
- {ok, Req, []}.
- websocket_handle({text, Data}, Req, State) ->
- try qs_transl(binary_to_list(Data)) of
- Query ->
- try process_query(Query, State) of
- {Reply, NewState} ->
- {reply, {text, << Reply/binary >>}, Req, NewState, hibernate}
- catch
- error:_ ->
- {reply, {text, << "Error in query." >>}, Req, State, hibernate}
- end
- catch
- error:_ ->
- {reply, {text, << "Query syntax error." >>}, Req, State, hibernate}
- end;
- websocket_handle({binary, Data}, Req, State) ->
- {reply, {binary, Data}, Req, State, hibernate};
- websocket_handle(_Frame, Req, State) ->
- {ok, Req, State, hibernate}.
- websocket_info(_Info, Req, State) ->
- {ok, Req, State, hibernate}.
- websocket_terminate(_Reason, _Req, _State) ->
- ok.
- terminate(_Reason, _Req, _State) ->
- ok.
- process_query(Query, State) ->
- Who = list_to_atom(find(who,Query)),
- case list_to_atom(find(op,Query)) of
- new ->
- case find(Who, State) of
- undefined ->
- { reply("Welcome, ~w!",[Who]),
- add({Who,0}, State) };
- _ ->
- { reply("~w, you are already a customer!",[Who]),
- State }
- end;
- add ->
- case find(Who, State) of
- undefined ->
- { reply("~w is not a customer!",[Who]),
- State };
- Balance ->
- NewBalance = Balance + erlang:list_to_integer(find(sum,Query)),
- { reply("Thanks, ~w, your balance is ~w!",[Who,NewBalance]),
- add({Who, NewBalance}, State) }
- end;
- withdraw ->
- X = erlang:list_to_integer(find(sum,Query)),
- case find(Who, State) of
- undefined ->
- { reply("~w is not a customer!",[Who]),
- State };
- Balance when X =< Balance ->
- NewBalance = Balance - X,
- { reply("Thanks, ~w, your balance is ~w!",[Who,NewBalance]),
- add({Who, NewBalance}, State) };
- Balance ->
- { reply("Sorry, ~w, you only have ~w in the bank!",[Who,Balance]),
- State }
- end;
- close ->
- case find(Who, State) of
- undefined ->
- { reply("~w, account not exist!",[Who]),
- State };
- Balance ->
- if
- Balance < 0 ->
- { reply("~w, you cannot remove account with debit!",[Who]),
- State };
- true ->
- { reply("Removed account, ~w!",[Who]),
- delete(Who, State) }
- end
- end;
- _ ->
- { reply("Undefined operation.",[]),
- State }
- end.
- qs_transl(String) ->
- kv_lst(string:tokens(String,"&")).
- kv_lst([]) -> [];
- kv_lst([Eq|Eqs]) ->
- [Ks,Vs] = string:tokens(Eq,"="),
- [{list_to_atom(Ks),Vs}|kv_lst(Eqs)].
- add({Key,Value},[]) -> [{Key,Value}];
- add({Key,Value},[{Key,_}|T]) -> [{Key,Value}|T];
- add({Key,Value},[H|T]) -> [H|add({Key,Value},T)].
- delete(_,[]) -> [];
- delete(Key,[{Key,_}|T]) -> T;
- delete(Key,[H|T]) -> [H|delete(Key,T)].
- find(_,[]) -> undefined;
- find(Key,[{Key,Value}|_]) -> Value;
- find(Key,[_|T]) -> find(Key,T).
- reply(Format, Args) ->
- list_to_binary(io_lib:format(Format, Args)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement