Advertisement
Guest User

Untitled

a guest
Jan 13th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 3.45 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.     Model = list_to_atom(find(model,Query)),
  54.     case list_to_atom(find(op,Query)) of   
  55.        new ->
  56.           case find(Model, State) of
  57.              undefined ->
  58.                    { reply("Car: ~w added!",[Model]),
  59.                      add({Model,0}, State) };
  60.              _ ->
  61.                    { reply("~w: that car already exist!",[Model]),
  62.                      State }
  63.           end;
  64.        changeprice ->
  65.           case find(Model, State) of
  66.              undefined ->
  67.                    { reply("~w: that car not exist!",[Model]),
  68.                      State };
  69.              _ ->
  70.                 NewPrice = erlang:list_to_integer(find(price,Query)),
  71.                 { reply("New price of , ~w, is ~w",[Model,NewPrice]),
  72.                   add({Model, NewPrice}, State) }
  73.           end;
  74.        setsold ->
  75.           case find(Model, State) of
  76.              undefined ->
  77.                    { reply("~w: that car not exist!",[Model]),
  78.                      State };
  79.              _ ->
  80.                 NewPrice = -1,
  81.                 { reply("~w status has changed to sold",[Model]),
  82.                   add({Model, NewPrice}, State) }
  83.           end;
  84.        showall ->
  85.           case find(Model, State) of
  86.              _ ->
  87.                 { reply("~w", [State]), State }  
  88.           end;
  89.        remove ->
  90.           case find(Model, State) of
  91.             undefined ->
  92.                 { reply("~w: that car not exist!",[Model]),
  93.                   State };         
  94.             _ ->
  95.                 { reply("Removed car, ~w!",[Model]),
  96.                   delete(Model, State) }  
  97.           end;
  98.        _ ->
  99.           { reply("Undefined operation.",[]),
  100.             State }
  101.     end.
  102.  
  103. qs_transl(String) ->
  104.     kv_lst(string:tokens(String,"&")).
  105.  
  106. kv_lst([]) -> [];
  107. kv_lst([Eq|Eqs]) ->
  108.     [Ks,Vs] = string:tokens(Eq,"="),
  109.     [{list_to_atom(Ks),Vs}|kv_lst(Eqs)].
  110.  
  111. add({Key,Value},[]) -> [{Key,Value}];
  112. add({Key,Value},[{Key,_}|T]) -> [{Key,Value}|T];
  113. add({Key,Value},[H|T]) -> [H|add({Key,Value},T)].
  114.  
  115. delete(_,[]) -> [];
  116. delete(Key,[{Key,_}|T]) -> T;
  117. delete(Key,[H|T]) -> [H|delete(Key,T)].
  118.  
  119. find(_,[]) -> undefined;
  120. find(Key,[{Key,Value}|_]) -> Value;
  121. find(Key,[_|T]) -> find(Key,T).
  122.  
  123. reply(Format, Args) ->
  124.     list_to_binary(io_lib:format(Format, Args)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement