caparol6991

Broker

Dec 5th, 2019
850
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -module(trade_calls).
  2. -compile(export_all).
  3.  
  4.  
  5.  
  6. main_ab() ->
  7.     S = self(),
  8.     PidCliBR = spawn(fun() -> br() end),
  9.     PidCliA = spawn(fun() -> a(S,PidCliBR) end),
  10.     spawn(fun() -> b(PidCliBR, PidCliA) end).
  11.  
  12. a(Parent, PidCliBR) ->
  13.     {ok, Pid} = trade_fsm:start_link("Carl"),
  14.     cn(PidCliBR,Pid),%wyślij pid do transakcji do brokera
  15.     Parent ! Pid,
  16.     io:format("Spawned Carl: ~p~n", [Pid]),
  17.     %sys:trace(Pid,true),
  18.     timer:sleep(800),
  19.     trade_fsm:accept_trade(Pid),
  20.     timer:sleep(400),
  21.     io:format("~p~n",[trade_fsm:ready(Pid)]),
  22.     timer:sleep(1000),
  23.     trade_fsm:make_offer(Pid, "horse"),
  24.     trade_fsm:make_offer(Pid, "sword"),
  25.     timer:sleep(1000),
  26.     io:format("a synchronizing~n"),
  27.     sync2(),
  28.     trade_fsm:ready(Pid),
  29.     timer:sleep(200),
  30.     trade_fsm:ready(Pid),
  31.     timer:sleep(1000).
  32.  
  33. b(PidCliBR, PidCliA) ->
  34.     {ok, Pid} = trade_fsm:start_link("Jim"),
  35.     cn(PidCliBR,Pid),%wyślij pid do transakcji do brokera
  36.     io:format("Spawned Jim: ~p~n", [Pid]),
  37.     %sys:trace(Pid,true),
  38.     timer:sleep(500),
  39.     wt(PidCliBR,Pid),%wyślij do brokera chęć dokonania transakcji
  40.     trade_fsm:make_offer(Pid, "boots"),
  41.     timer:sleep(200),
  42.     trade_fsm:retract_offer(Pid, "boots"),
  43.     timer:sleep(500),
  44.     trade_fsm:make_offer(Pid, "shotgun"),
  45.     timer:sleep(1000),
  46.     io:format("b synchronizing~n"),
  47.     sync1(PidCliA),
  48.     trade_fsm:make_offer(Pid, "horse"), %% race condition!
  49.     trade_fsm:ready(Pid),
  50.     timer:sleep(200),
  51.     timer:sleep(1000).
  52.  
  53. br() ->
  54.     receive PidA -> PidA end,
  55.     receive PidB -> PidB end,
  56.     sync3(PidA).
  57. %%% Utils
  58. sync1(Pid) ->
  59.     Pid ! self(),
  60.     receive ack -> ok end.
  61.  
  62. sync2() ->
  63.     receive
  64.         From -> From ! ack
  65.     end.
  66. cn(PidBR, PidC) ->
  67.     PidBR ! PidC.
  68.  
  69. wt(PidBR,Pid) ->
  70.     PidBR ! {self(), Pid},
  71.     receive ack -> ok end.
  72.  
  73.  
  74. sync3(P)->
  75.   receive
  76.       {From, Pid} ->
  77.       trade_fsm:trade(Pid, P),
  78.       From ! ack
  79.   end.
RAW Paste Data