Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 3.62 KB | None | 0 0
  1. -module(ws_bank_handler).
  2.  
  3. -behaviour(cowboy_http_handler).
  4. -behaviour(cowboy_websocket_handler).
  5.  
  6. -export([init/3, handle/2, terminate/3]).
  7. -export([websocket_init/3]).
  8. -export([websocket_handle/3]).
  9. -export([websocket_info/3]).
  10. -export([websocket_terminate/3]).
  11. -export([add/2, delete/2, find/2, qs_transl/1, kv_lst/1,
  12.          process_query/2, reply/2]).
  13.  
  14. init({tcp, http}, _Req, _Opts) ->
  15.   {upgrade, protocol, cowboy_websocket}.
  16.    
  17. handle(_Req, State) ->
  18.     {ok, Req2} = cowboy_http_req:reply(404, [{'Content-Type', <<"text/html">>}]),
  19.     {ok, Req2, State}.     
  20.  
  21. websocket_init(_, Req, _Opts) ->
  22.     {ok, Req, []}.
  23.  
  24. websocket_handle({text, Data}, Req, State) ->
  25.     try qs_transl(binary_to_list(Data)) of
  26.         Query ->
  27.             try process_query(Query, State) of     
  28.                 {Reply, NewState} ->
  29.                     {reply, {text, << Reply/binary >>}, Req, NewState, hibernate}
  30.             catch
  31.                 error:_ ->
  32.                     {reply, {text, << "Error in query." >>}, Req, State, hibernate}
  33.             end
  34.     catch
  35.         error:_ ->
  36.             {reply, {text, << "Query syntax error." >>}, Req, State, hibernate}
  37.     end;
  38. websocket_handle({binary, Data}, Req, State) ->
  39.     {reply, {binary, Data}, Req, State, hibernate};
  40. websocket_handle(_Frame, Req, State) ->
  41.     {ok, Req, State, hibernate}.
  42.  
  43. websocket_info(_Info, Req, State) ->
  44.     {ok, Req, State, hibernate}.
  45.  
  46. websocket_terminate(_Reason, _Req, _State) ->
  47.     ok.
  48.    
  49. terminate(_Reason, _Req, _State) ->
  50.     ok.
  51.  
  52. process_query(Query, State) ->
  53.     Who = list_to_atom(find(who,Query)),
  54.     case list_to_atom(find(op,Query)) of   
  55.        new ->
  56.           case find(Who, State) of
  57.              undefined ->
  58.                    { reply("Welcome, ~w!",[Who]),
  59.                      add({Who,0}, State) };
  60.              _ ->
  61.                    { reply("~w, you are already a customer!",[Who]),
  62.                      State }
  63.           end;
  64.        add ->
  65.           case find(Who, State) of
  66.              undefined ->
  67.                    { reply("~w is not a customer!",[Who]),
  68.                      State };
  69.              Balance ->
  70.                 NewBalance = Balance + erlang:list_to_integer(find(sum,Query)),
  71.                 { reply("Thanks, ~w, your balance is ~w!",[Who,NewBalance]),
  72.                   add({Who, NewBalance}, State) }
  73.           end;
  74.        withdraw ->
  75.           X = erlang:list_to_integer(find(sum,Query)),
  76.           case find(Who, State) of
  77.              undefined ->
  78.                 { reply("~w is not a customer!",[Who]),
  79.                   State };
  80.              Balance when X =< Balance ->
  81.                 NewBalance = Balance - X,
  82.                 { reply("Thanks, ~w, your balance is ~w!",[Who,NewBalance]),
  83.                   add({Who, NewBalance}, State) };
  84.             Balance ->
  85.                 { reply("Sorry, ~w, you only have ~w in the bank!",[Who,Balance]),
  86.                   State }
  87.           end;
  88.        close ->
  89.           case find(Who, State) of
  90.             undefined ->
  91.                 { reply("~w, account not exist!",[Who]),
  92.                   State };
  93.            
  94.             Balance ->
  95.                 if
  96.                     Balance < 0 ->
  97.                         { reply("~w, you cannot remove account with debit!",[Who]),
  98.                           State };
  99.                     true ->
  100.                         { reply("Removed account, ~w!",[Who]),
  101.                           delete(Who, State) }  
  102.                 end
  103.           end;
  104.        _ ->
  105.           { reply("Undefined operation.",[]),
  106.             State }
  107.     end.
  108.  
  109. qs_transl(String) ->
  110.     kv_lst(string:tokens(String,"&")).
  111.  
  112. kv_lst([]) -> [];
  113. kv_lst([Eq|Eqs]) ->
  114.     [Ks,Vs] = string:tokens(Eq,"="),
  115.     [{list_to_atom(Ks),Vs}|kv_lst(Eqs)].
  116.  
  117. add({Key,Value},[]) -> [{Key,Value}];
  118. add({Key,Value},[{Key,_}|T]) -> [{Key,Value}|T];
  119. add({Key,Value},[H|T]) -> [H|add({Key,Value},T)].
  120.  
  121. delete(_,[]) -> [];
  122. delete(Key,[{Key,_}|T]) -> T;
  123. delete(Key,[H|T]) -> [H|delete(Key,T)].
  124.  
  125. find(_,[]) -> undefined;
  126. find(Key,[{Key,Value}|_]) -> Value;
  127. find(Key,[_|T]) -> find(Key,T).
  128.  
  129. reply(Format, Args) ->
  130.     list_to_binary(io_lib:format(Format, Args)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement