-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacceptor.erl
More file actions
34 lines (29 loc) · 1.02 KB
/
acceptor.erl
File metadata and controls
34 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-module(acceptor).
-behaviour(gen_server).
-export([init/1, handle_call/3]).
-record(acceptor,
{maxBallot :: integer(), acceptedBallot :: integer(), acceptedValue :: string()}).
init(_) ->
{ok,
#acceptor{maxBallot = 0,
acceptedBallot = nil,
acceptedValue = nil}}.
handle_call({prepare, Ballot}, _From, State) ->
io:format("acceptor got prepare ~p\n", [Ballot]),
if Ballot =< State#acceptor.maxBallot ->
{reply, {ballot_too_low, Ballot, State#acceptor.maxBallot}, State};
true ->
{reply,
{prepare, ok, Ballot, State#acceptor.acceptedBallot, State#acceptor.acceptedValue},
State#acceptor{maxBallot = Ballot}}
end;
handle_call({accept, Ballot, Value}, _From, State) ->
if Ballot < State#acceptor.maxBallot ->
{reply, {ballot_too_low, Ballot}, State};
true ->
{reply,
{accept, ok, Ballot},
State#acceptor{maxBallot = Ballot,
acceptedBallot = Ballot,
acceptedValue = Value}}
end.