SHARE
TWEET

Untitled

a guest Dec 11th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -module(m).
  2. -export([createController/3, createUser/1]).
  3. -export([handleController/1, handleUser/1]).
  4.  
  5. createUser(Name) ->
  6.     register(Name, spawn(?MODULE, handleUser, [Name])).
  7.  
  8. handleUser(Name) ->
  9.     {ok, Pid} = trade_fsm:start_link(atom_to_list(Name)),
  10.     io:format("Spawned fsm server: ~p~n", [Pid]),
  11.     register(getFsmName(Name), Pid),
  12.     monitor(process, Pid),
  13.     handleUserMessage(Pid).
  14.  
  15. handleUserMessage(FsmPid) ->
  16.     receive
  17.         {trade, OtherName} ->
  18.             OtherFsmName = getFsmName(OtherName),
  19.             trade_fsm:trade(FsmPid, whereis(OtherFsmName)),
  20.             handleUserMessage(FsmPid);
  21.         accept_trade ->
  22.             trade_fsm:accept_trade(FsmPid), handleUserMessage(FsmPid);
  23.         {make_offer, Item} ->
  24.             trade_fsm:make_offer(FsmPid, Item), handleUserMessage(FsmPid);
  25.         {retract_offer, Item} ->
  26.             trade_fsm:retract_offer(FsmPid, Item), handleUserMessage(FsmPid);
  27.         ready ->
  28.             trade_fsm:ready(FsmPid), handleUserMessage(FsmPid);
  29.         cancel ->
  30.             trade_fsm:cancel(FsmPid), handleUserMessage(FsmPid);
  31.         {'DOWN', _, process, _, _} -> erlang:display({self(), user_stopped});
  32.         _ -> handleUserMessage(FsmPid)
  33.     end.
  34.  
  35. getFsmName(UserName) ->
  36.     list_to_atom(atom_to_list(UserName) ++ "_fsm").
  37.  
  38. getFsmPid(UserName) ->
  39.     FsmName = getFsmName(UserName),
  40.     whereis(FsmName).
  41.  
  42. % TO jest to mojego zdania tylko
  43. % controller functions
  44.  
  45. createController(Name, FirstUserName, SecUserName) ->
  46.     register(Name, spawn(?MODULE, handleController, [[FirstUserName, SecUserName]])).
  47.  
  48. handleController(Users) ->
  49.     receive
  50.         {remove_own, UserName, Items} ->
  51.             removeItems(remove_own, getFsmPid(UserName), Items),
  52.             [OtherUser | _] = Users -- [UserName],
  53.             removeItems(remove_other, getFsmPid(OtherUser), Items),
  54.             handleController(Users);
  55.         {remove_other, UserName, Items} ->
  56.             removeItems(remove_other, getFsmPid(UserName), Items),
  57.             [OtherUser | _] = Users -- [UserName],
  58.             removeItems(remove_own, getFsmPid(OtherUser), Items),
  59.             handleController(Users);
  60.         _ -> handleController(Users)
  61.     end.
  62.  
  63. removeItems(Command, Pid, Items) ->
  64.    % gen_fsm:send_all_state_event(Pid, {Command, Items}).
  65.    gen_fsm:send_event(Pid, {Command, Items}).
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top