This repository has been archived on 2024-01-29. You can view files and clone it, but cannot push or open issues or pull requests.
distributed_system_coursework/lib/server.ex

358 lines
9.8 KiB
Elixir
Raw Normal View History

2024-01-16 19:59:01 +00:00
defmodule ServerMacros do
2024-01-16 20:42:57 +00:00
def create_create_loop(name, do: match_exp, else: process_exp, after: after_exp) do
2024-01-16 19:59:01 +00:00
function_name = :"#{name}_loop"
ast1 = quote do
{:timeout} -> :timeout
end
ast2 = quote do
value ->
2024-01-16 22:06:10 +00:00
log("Got unexpected value: #{inspect(value)}")
Process.send_after(self(), value, t + 2000)
unquote(function_name)(v, t)
2024-01-16 19:59:01 +00:00
end
ast3 = ast1 ++ match_exp ++ ast2
2024-01-16 20:42:57 +00:00
2024-01-16 23:46:30 +00:00
after_exp = if after_exp == nil do
2024-01-16 20:42:57 +00:00
quote do
2024-01-16 23:46:30 +00:00
t -> :timeout
2024-01-16 20:42:57 +00:00
end
else
2024-01-16 23:46:30 +00:00
after_exp
end
quote do
def unquote(function_name)(v, t) do
var!(v) = v
unquote(process_exp)
receive do
unquote(ast3)
after
unquote(after_exp)
2024-01-16 19:59:01 +00:00
end
end
end
end
2024-01-16 20:42:57 +00:00
def create_create_loop(name, do: exp, else: else_exp) do
create_create_loop(name, do: exp, else: else_exp, after: nil)
end
def create_create_loop(name, do: exp, after: after_exp) do
create_create_loop(name, do: exp, else: nil, after: after_exp)
end
2024-01-16 19:59:01 +00:00
def create_create_loop(name, do: exp) do
2024-01-16 20:42:57 +00:00
create_create_loop(name, do: exp, else: nil, after: nil)
2024-01-16 19:59:01 +00:00
end
defmacro create_loop(name, clauses) do
create_create_loop(name, clauses)
end
defmacro try_propose(val, do: ready, else: recal_do) do
ast1 = quote do
{:timeout} -> unquote(recal_do)
{:abort} -> unquote(recal_do)
end
ast2 = quote do
{:decision, v} ->
var!(state) = apply_game(var!(state), v)
unquote(recal_do)
v ->
raise "Unknown message on try_propose #{inspect(v)}"
end
ast3 = ast1 ++ ready ++ ast2
quote do
v = Paxos.propose(var!(state).paxos, var!(state).instance, unquote(val), 1000)
case v do
unquote(ast3)
end
end
end
end
defmodule Server do
require ServerMacros
import ServerMacros
require Utils
import Utils
2024-01-16 22:06:10 +00:00
create_log 3
2024-01-16 19:59:01 +00:00
def start(name, participants) do
log("starting server")
pid = spawn(Server, :init, [name, participants])
register_name(name, pid, false)
end
def init(name, participants) do
2024-01-16 20:42:57 +00:00
paxos = Paxos.start(alter_name(name, "_paxos"), Enum.map(participants, fn name -> alter_name(name, "_paxos") end), true)
2024-01-16 19:59:01 +00:00
state = %{
name: name,
procs: participants,
games: %{},
paxos: paxos,
instance: 0,
}
run(state)
end
runfn do
{:start_game, participants, pid_to_inform} ->
{state, game_id} = try_to_create_game(state, participants)
2024-01-16 23:46:30 +00:00
state = set_modifed(state, game_id)
2024-01-16 19:59:01 +00:00
safecast(pid_to_inform, {:start_game_ans, game_id})
state
2024-01-16 22:06:10 +00:00
{:get_game_state, game_id, pid_to_inform} ->
get_game_state(state, game_id, pid_to_inform)
2024-01-16 23:46:30 +00:00
{:make_move, game_id, move, pid_to_inform} ->
state = try_to_play_checks(state, game_id, move, pid_to_inform)
end
def try_to_play_checks(state, game_id, move, pid_to_inform, repeat \\ false) do
cond do
state.games[game_id] == :not_playing_in_game ->
safecast(pid_to_inform, {:make_move, game_id, :not_playing})
state
state.games[game_id] == nil ->
if repeat do
safecast(pid_to_inform, {:make_move, game_id, :game_does_not_exist})
state
else
state = qurey_status(state)
try_to_play_checks(state, game_id, move, pid_to_inform, true)
end
true ->
state = qurey_status(state)
game = state.games[game_id]
cond do
game.modified ->
safecast(pid_to_inform, {:make_move, game_id, :player_moved_before, game.game_state, game.hand})
set_modifed(state, game_id)
is_finished(game, game_id) ->
{_, score} = state.games[game_id]
safecast(pid_to_inform, {:make_move, game_id, :game_finished, score})
state
true ->
try_to_play(state, game_id, move, pid_to_inform)
end
end
end
def try_to_play(state, game_id, move, pid_to_inform) do
name = state.name
try_propose {:make_move, game_id, name, move}
do
{:decision, {:make_move, ^game_id, ^name, ^move}} ->
state = apply_game(state, {:make_move, game_id, name, move})
if is_finished(state, game_id) do
{_, score} = state.games[game_id]
safecast(pid_to_inform, {:make_move, game_id, :game_finished, score})
else
game = state.games[game_id]
safecast(pid_to_inform, {:make_move, game_id, game.game_state, game.hand[state.name]})
end
set_modifed(state, game_id)
{:decision, {:make_move, ^game_id, new_name, new_move}} ->
state = apply_game(state, {:make_move, game_id, new_name, new_move})
if is_finished(state, game_id) do
{_, score} = state.games[game_id]
safecast(pid_to_inform, {:make_move, game_id, :game_finished, score})
else
game = state.games[game_id]
safecast(pid_to_inform, {:make_move, game_id, :player_moved_before, game.game_state, game.hand})
end
set_modifed(state, game_id)
else
try_to_play(state, game_id, move, pid_to_inform)
end
2024-01-16 22:06:10 +00:00
end
def get_game_state(state, game_id, pid_to_inform, repeat \\ false) do
cond do
state.games[game_id] == :not_playing_in_game ->
safecast(pid_to_inform, {:game_state, game_id, :not_playing})
state
state.games[game_id] == nil ->
if repeat do
safecast(pid_to_inform, {:game_state, game_id, :game_does_not_exist})
state
else
state = qurey_status(state)
get_game_state(state, game_id, pid_to_inform, true)
end
true ->
state = qurey_status(state)
2024-01-16 23:46:30 +00:00
safecast(pid_to_inform, {:game_state, game_id, state.games[game_id].game_state, state.games[game_id].hand[state.name]})
2024-01-16 22:06:10 +00:00
state
end
2024-01-16 19:59:01 +00:00
end
def try_to_create_game(state, participants) do
game_ids = Map.keys(state.games)
2024-01-16 20:42:57 +00:00
latest = Enum.at(Enum.sort(game_ids), length(game_ids) - 1)
new_game_id = if latest do latest else 0 end + 1
2024-01-16 19:59:01 +00:00
# TODO: randomize game state
new_game_state = [1, 1]
# TODO: randomize Initial hand value
hand = Enum.reduce(participants, %{}, fn p, acc -> Map.put(acc, p, 1) end)
try_propose {:start_game, new_game_id, participants, new_game_state, hand}
do
{:decision, {:start_game, ^new_game_id, ^participants, ^new_game_state, ^hand}} ->
state = apply_game(state, {:start_game, new_game_id, participants, new_game_state, hand})
{state, new_game_id}
else
try_to_create_game(state, participants)
end
end
#
2024-01-16 22:06:10 +00:00
# Utils
2024-01-16 19:59:01 +00:00
#
2024-01-16 22:06:10 +00:00
2024-01-16 23:46:30 +00:00
def is_finished(state, game) do
case state.games[game] do
{:finished, _} -> true
_ -> false
end
end
2024-01-16 22:06:10 +00:00
def qurey_status(state) do
v = Paxos.get_decision(state.paxos, state.instance, 100)
or_state v != nil do
state = apply_game(state, v)
qurey_status(state)
end
end
2024-01-16 19:59:01 +00:00
2024-01-16 23:46:30 +00:00
def set_modifed(state, game, val \\ false) do
or_state not is_finished(state, game) do
%{state | games: Map.put(state.games, game, %{state.games | modified: val})}
end
end
2024-01-16 22:06:10 +00:00
#
# Apply Game States
#
2024-01-16 23:46:30 +00:00
def apply_game(state, {:make_move, game_id, new_name, new_move}) do
raise :todo
end
2024-01-16 19:59:01 +00:00
def apply_game(state, {:start_game, game_id, participants, new_game_state, hand}) do
2024-01-16 22:06:10 +00:00
cond do
state.games[game_id] ->
raise :game_already_exists
state.name in participants ->
%{state |
games: Map.put(state.games, game_id, %{
game_state: new_game_state,
participants: participants,
2024-01-16 23:46:30 +00:00
hand: hand,
modified: true,
2024-01-16 22:06:10 +00:00
}),
instance: state.instance + 1
}
true ->
%{state |
games: Map.put(state.games, game_id, :not_playing_in_game),
instance: state.instance + 1,
}
2024-01-16 19:59:01 +00:00
end
end
2024-01-16 22:06:10 +00:00
def apply_game(_, _), do: raise :do_not_know_how_to_apply_game_state
2024-01-16 19:59:01 +00:00
############
# Interface
############
create_loop :start_game do
2024-01-16 20:42:57 +00:00
{:start_game_ans, game_id} ->
2024-01-16 19:59:01 +00:00
log("Started a game #{game_id}")
2024-01-16 20:42:57 +00:00
{:start_game, game_id}
end
2024-01-16 19:59:01 +00:00
def start_game(name, participants) do
safecast(name, {:start_game, participants, self()})
2024-01-16 22:06:10 +00:00
start_game_loop(nil, 1000)
end
create_loop :get_game_state do
{:game_state, ^v, :not_playing} ->
IO.puts("Not Playing in that game")
2024-01-16 23:46:30 +00:00
{:not_playing}
2024-01-16 22:06:10 +00:00
{:game_state, ^v, game_state, hand} ->
IO.puts("Got game state, #{inspect(game_state)}, hand: #{inspect(hand)}")
2024-01-16 23:46:30 +00:00
{:state, game_state, hand}
2024-01-16 22:06:10 +00:00
{:game_state, ^v, :game_does_not_exist} ->
IO.puts("Got game does not exist")
2024-01-16 23:46:30 +00:00
{:not_exists}
2024-01-16 19:59:01 +00:00
end
2024-01-16 22:06:10 +00:00
def get_game_state(name, game_id) do
safecast(name, {:get_game_state, game_id, self()})
get_game_state_loop(game_id, 1000)
end
2024-01-16 23:46:30 +00:00
create_loop :make_move do
{:make_move, ^v, :game_does_not_exist} ->
IO.puts("Got game does not exist")
{:not_exists}
{:make_move, ^v, :not_playing} ->
IO.puts("Not Playing in that game")
{:not_playing}
{:make_move, ^v, :game_finished, score} ->
IO.puts("Game finsihed, #{score}")
{:game_finished, score}
{:make_move, ^v, :player_moved_before, game_state, hand} ->
IO.puts("Player moved_before, #{inspect(game_state)} #{inspect(hand)}")
{:player_moved_before, game_state, hand}
{:make_move, ^v, game_state, hand} ->
IO.puts("Got game state, #{inspect(game_state)}, hand: #{inspect(hand)}")
{:state, game_state, hand}
end
def make_move(name, game_id, move) do
safecast(name, {:make_move, game_id, move, self()})
make_move_loop(game_id, 1000)
end
2024-01-16 22:06:10 +00:00
2024-01-16 19:59:01 +00:00
############
# Debug
############
def spinup(number_of_participants) do
procs = Enum.to_list(0..number_of_participants) |> Enum.map(fn n -> :"p#{n}" end)
Enum.map(procs, fn proc -> Server.start(proc, procs) end)
end
def kill (pids) do
pids |> Enum.map(fn m -> Process.exit(m, :kill) end)
end
end