Advertisement
Guest User

trade_calls

a guest
Jan 9th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 4.81 KB | None | 0 0
  1. -module(trade_calls).
  2. -compile(export_all).
  3.  
  4. %%______________________________________________________________________
  5. new_main() ->
  6.     S = self(),
  7.     PidCliX = spawn(fun() -> x(S) end),
  8.     receive PidX -> PidX end,
  9.     spawn(fun() -> y(PidX, PidCliX) end).
  10. x(Parent) ->
  11.     {ok, Pid} = trade_fsm:start_link("X"),
  12.     Parent ! Pid,
  13.     io:format("Spawned X: ~p~n", [Pid]),
  14.     timer:sleep(800),
  15.     trade_fsm:accept_trade(Pid),
  16.     timer:sleep(400),
  17.     trade_fsm:make_final_offer(Pid, "horse"),
  18.     timer:sleep(30000).
  19. y(PidX, PidCliX) ->
  20.     {ok, Pid} = trade_fsm:start_link("Y"),
  21.     io:format("Spawned Y: ~p~n", [Pid]),
  22.     timer:sleep(500),
  23.     trade_fsm:trade(Pid, PidX),
  24.     trade_fsm:make_offer(Pid, "boots"),
  25.     timer:sleep(10000),
  26.     % trade_fsm:retract_offer(Pid, "boots"),
  27.     timer:sleep(3000).
  28.  
  29.  
  30. %% test a little bit of everything and also deadlocks on ready state
  31. %% -- leftover messages possible on race conditions on ready state
  32. main_ab() ->
  33.     S = self(),
  34.     io:format("~p~n", [S]),
  35.     PidCliA = spawn(fun() -> a(S) end),
  36.     receive PidA -> PidA end,
  37.     spawn(fun() -> b(PidA, PidCliA) end).
  38.  
  39. a(Parent) ->
  40.     {ok, Pid} = trade_fsm:start_link("Carl"),
  41.     Parent ! Pid,
  42.     io:format("Spawned Carl: ~p~n", [Pid]),
  43.     %sys:trace(Pid,true),
  44.     timer:sleep(800),
  45.     trade_fsm:accept_trade(Pid),
  46.     timer:sleep(400),
  47.     io:format("~p~n",[trade_fsm:ready(Pid)]),
  48.     timer:sleep(1000),
  49.     trade_fsm:make_offer(Pid, "horse"),
  50.     trade_fsm:make_offer(Pid, "sword"),
  51.     timer:sleep(1000),
  52.     io:format("a synchronizing~n"),
  53.     sync2(),
  54.     trade_fsm:ready(Pid),
  55.     timer:sleep(200),
  56.     trade_fsm:ready(Pid),
  57.     timer:sleep(1000).
  58.  
  59. b(PidA, PidCliA) ->
  60.     {ok, Pid} = trade_fsm:start_link("Jim"),
  61.     io:format("Spawned Jim: ~p~n", [Pid]),
  62.     %sys:trace(Pid,true),
  63.     timer:sleep(500),
  64.     trade_fsm:trade(Pid, PidA),
  65.     trade_fsm:make_offer(Pid, "boots"),
  66.     timer:sleep(200),
  67.     trade_fsm:retract_offer(Pid, "boots"),
  68.     timer:sleep(500),
  69.     trade_fsm:make_offer(Pid, "shotgun"),
  70.     timer:sleep(1000),
  71.     io:format("b synchronizing~n"),
  72.     sync1(PidCliA),
  73.     trade_fsm:make_offer(Pid, "horse"), %% race condition!
  74.     trade_fsm:ready(Pid),
  75.     timer:sleep(200),
  76.     timer:sleep(1000).
  77.  
  78. %% force a race condition on cd trade negotiation
  79. main_cd() ->
  80.     S = self(),
  81.     PidCliC = spawn(fun() -> c(S) end),
  82.     receive PidC -> PidC end,
  83.     spawn(fun() -> d(S, PidC, PidCliC) end),
  84.     receive PidD -> PidD end,
  85.     PidCliC ! PidD.
  86.    
  87. c(Parent) ->
  88.     {ok, Pid} = trade_fsm:start_link("Marc"),
  89.     Parent ! Pid,
  90.     receive PidD -> PidD end,
  91.     io:format("Spawned Marc: ~p~n", [Pid]),
  92.     %sys:trace(Pid, true),
  93.     sync2(),
  94.     trade_fsm:trade(Pid, PidD),
  95.     %% no need to accept_trade thanks to the race condition
  96.     timer:sleep(200),
  97.     trade_fsm:retract_offer(Pid, "car"),
  98.     trade_fsm:make_offer(Pid, "horse"),
  99.     timer:sleep(600),
  100.     trade_fsm:cancel(Pid),
  101.     timer:sleep(1000).
  102.  
  103. d(Parent, PidC, PidCliC) ->
  104.     {ok, Pid} = trade_fsm:start_link("Pete"),
  105.     Parent ! Pid,
  106.     io:format("Spawned Jim: ~p~n", [Pid]),
  107.     %sys:trace(Pid,true),
  108.     sync1(PidCliC),
  109.     trade_fsm:trade(Pid, PidC),
  110.     %% no need to accept_trade thanks to the race condition
  111.     timer:sleep(200),
  112.     trade_fsm:retract_offer(Pid, "car"),
  113.     trade_fsm:make_offer(Pid, "manatee"),
  114.     timer:sleep(100),
  115.     trade_fsm:ready(Pid),
  116.     timer:sleep(1000).
  117.  
  118. main_ef() ->
  119.     S = self(),
  120.     PidCliE = spawn(fun() -> e(S) end),
  121.     receive PidE -> PidE end,
  122.     spawn(fun() -> f(PidE, PidCliE) end).
  123.  
  124. e(Parent) ->
  125.     {ok, Pid} = trade_fsm:start_link("Carl"),
  126.     Parent ! Pid,
  127.     io:format("Spawned Carl: ~p~n", [Pid]),
  128.     %sys:trace(Pid,true),
  129.     timer:sleep(800),
  130.     trade_fsm:accept_trade(Pid),
  131.     timer:sleep(400),
  132.     io:format("~p~n",[trade_fsm:ready(Pid)]),
  133.     timer:sleep(1000),
  134.     trade_fsm:make_offer(Pid, "horse"),
  135.     trade_fsm:make_offer(Pid, "sword"),
  136.     timer:sleep(1000),
  137.     io:format("a synchronizing~n"),
  138.     sync2(),
  139.     trade_fsm:ready(Pid),
  140.     timer:sleep(200),
  141.     trade_fsm:ready(Pid),
  142.     timer:sleep(1000).
  143.  
  144. f(PidE, PidCliE) ->
  145.     {ok, Pid} = trade_fsm:start_link("Jim"),
  146.     io:format("Spawned Jim: ~p~n", [Pid]),
  147.     %sys:trace(Pid,true),
  148.     timer:sleep(500),
  149.     trade_fsm:trade(Pid, PidE),
  150.     trade_fsm:make_offer(Pid, "boots"),
  151.     timer:sleep(200),
  152.     trade_fsm:retract_offer(Pid, "boots"),
  153.     timer:sleep(500),
  154.     trade_fsm:make_offer(Pid, "shotgun"),
  155.     timer:sleep(1000),
  156.     io:format("b synchronizing~n"),
  157.     sync1(PidCliE),
  158.     trade_fsm:make_offer(Pid, "horse"),
  159.     timer:sleep(200),
  160.     trade_fsm:ready(Pid),
  161.     timer:sleep(1000).
  162.  
  163. %%% Utils
  164. sync1(Pid) ->
  165.     Pid ! self(),
  166.     receive ack -> ok end.
  167.  
  168. sync2() ->
  169.     receive
  170.         From -> From ! ack
  171.     end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement