Merge branch 'jonte_rewrite' into rewrite

Conflicts:
	src/ggs_table.erl
This commit is contained in:
Jonatan Pålsson 2011-02-17 23:22:25 +01:00
commit 5bc862c6f9
7 changed files with 278 additions and 62 deletions

View file

@ -1,13 +1,20 @@
-module(ggs_coordinator).
%% API Exports
-export([start_link/0, stop/1]).
-export([start_link/0, stop/1, join_table/1, create_table/1, join_lobby/0,
respawn_player/2, respawn_table/1, remove_player/2]).
%% gen_server callback exports
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).
-define(SERVER, ?MODULE).
-record(co_state,
{players = [], % List of all player processes
player_table_map = [], % Players <-> Table map
table_state_map = [],
tables = []}). % Table <-> Table state map
%% @doc This module act as "the man in the middle".
%% Creates the starting connection between table and players.
@ -16,21 +23,22 @@ start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%% @doc Terminates the coordinator process.
stop(_Reason) ->
ggs_logger:not_implemented().
stop(Reason) ->
gen_server:cast(ggs_coordinator, {stop, Reason}).
%% @doc Joins table with specified token
join_table(_Token) ->
ggs_logger:not_implemented().
%% @doc Joins table with specified token, returns {error, no_such_table}
%% if the specified table token does not exist
join_table(Token) ->
gen_server:call(ggs_coordinator, {join_table, Token}).
%% @doc Create a new table
create_table(_Params) ->
ggs_logger:not_implemented().
%% @doc Create a new table, return {error, Reason} or {ok, TableToken}
create_table(Params) ->
gen_server:call(ggs_coordinator, {create_table, Params}).
%% @doc This is the first function run by a newly created players.
%% Generates a unique token that we use to identify the player.
join_lobby() ->
ggs_logger:not_implemented().
gen_server:call(ggs_coordinator, join_lobby).
%% @doc Act as a supervisor to player and respawns player when it gets bad data.
respawn_player(_Player, _Socket) ->
@ -47,11 +55,38 @@ remove_player(_From, _Player) ->
%% gen_server callbacks
init([]) ->
{ok, ok}.
{ok, #co_state{}}.
handle_call(join_lobby, _From, State) ->
Token = helpers:get_new_token(),
{reply, {ok, Token}, State};
handle_call({join_table, Table}, From, State) ->
{FromPlayer, _Ref} = From,
Tables = State#co_state.tables,
case lists:keyfind(Table, 1, Tables) of
{TableID, TablePID} ->
ggs_table:add_player(TablePID, FromPlayer),
{reply, {ok, TablePID}, State};
false ->
{reply, {error, no_such_table}, State}
end;
handle_call({create_table, {force, TableID}}, From, State) ->
TableIDMap = State#co_state.player_table_map,
Tables = State#co_state.tables,
NewTableProc = ggs_table:start_link(),
{reply, {ok, TableID}, State#co_state{
player_table_map = [{From, TableID} | TableIDMap],
tables = [{TableID, NewTableProc} | Tables]
}};
handle_call(_Message, _From, State) ->
{noreply, State}.
handle_cast({stop, Reason}, State) ->
{stop, normal, state};
handle_cast(_Message, State) ->
{noreply, State}.

41
src/ggs_gamevm_e.erl Normal file
View file

@ -0,0 +1,41 @@
-module(ggs_gamevm_e).
-export([start_link/1, define/2, user_command/4]).
%% @doc This module is responsible for running the game VM:s. You can issue
%% commands to a vm using this module.
%% @doc Create a new VM process. The process ID is returned and can be used
%% with for example the define method of this module.
start_link(Table) ->
PortPid = spawn( fun() ->
loop(Table)
end ),
PortPid.
%% @doc Define some new code on the specified VM, returns the atom ok.
define(GameVM, SourceCode) ->
GameVM ! {define,SourceCode},
ok.
%% @doc Execute a user command on the specified VM. This function is
%% asynchronous, and returns ok.
%% @spec user_command(GameVM, User, Command, Args) -> ok
%% GameVM = process IS of VM
%% Player = the player running the command
%% Command = a game command to run
%% Args = arguments for the Command parameter
user_command(GameVM, Player, Command, Args) ->
Ref = make_ref(),
GameVM ! {user_command, Player, Command, Args, self(), Ref},
ok.
%% Helper functions
loop(Table) ->
receive
{define, SourceCode} ->
loop(Table);
{user_command, _User, Command, _Args, _From, _Ref} ->
io:format("GameVM received a message~n"),
ggs_table:notify_all_players(Table, Command),
loop(Table)
end.

View file

@ -1,5 +1,9 @@
-module(ggs_player).
-export([start_link/1, notify/3, get_token/1, stop/2]).
-record(pl_state,
{token, % Player's token
socket, % Player's socket
table}). % Player's table
%% @doc This module handles communication between a player and GGS. This module is
%%responsible for:
@ -13,14 +17,28 @@
%% identifying the player.
%% @spec start_link(Socket::socket()) -> {ok, Pid} | {error, Reason}
start_link(Socket) ->
loop(Socket).
% The socket is in 'active' mode, and that means we are pushed any data
% that arrives on it, we do not need to recv() manually. Since the socket
% was opened in our parent process, we need to change the owner of it to
% us, otherwise these messages end up in our parent.
erlang:port_connect(Socket, self()),
{ok, Token} = ggs_coordinator:join_lobby(),
TableStatus = ggs_coordinator:join_table(1337),
case TableStatus of
{ok, Table} ->
loop(#pl_state{socket = Socket, token = Token, table = Table});
{error, no_such_table} ->
ggs_coordinator:create_table({force, 1337}),
{ok, Table} = ggs_coordinator:join_table(1337),
loop(#pl_state{socket = Socket, token = Token, table = Table})
end.
%% @doc Handles incoming messages from the GGS and forwards them through the player
%% socket to the player.
%% @spec notify(Player::Pid(), From::Pid(),
%% {Command::String(), Message::string()}) -> ok
notify(Player, From, Message) ->
ggs_logger:not_implemented().
Player ! {notify, From, Message}.
%% @doc Get the player token uniquely representing the player.
%% @spec get_token() -> string()
@ -36,13 +54,13 @@ stop(_Player,_Table) ->
%% Internals
loop(Socket) ->
% The socket is in 'active' mode, and that means we are pushed any data
% that arrives on it, we do not need to recv() manually. Since the socket
% was opened in our parent process, we need to change the owner of it to
% us, otherwise these messages end up in our parent.
erlang:port_connect(Socket, self()),
receive {tcp, Socket, Data} -> % Just echo for now..
gen_tcp:send(Socket,Data),
loop(Socket)
loop(#pl_state{token = Token, socket = Socket, table = Table} = State) ->
receive
{tcp, Socket, Data} -> % Just echo for now..
io:format("Notifying table..~n"),
ggs_table:notify_game(Table, Token, Data),
loop(State);
{notify, From, Message} ->
gen_tcp:send(Socket, Message),
loop(State)
end.

View file

@ -5,7 +5,8 @@
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
terminate/2, code_change/3, notify_all_players/2, notify_game/3,
add_player/2]).
-record(state, { players, game_vm } ).
@ -23,9 +24,7 @@
% @doc returns a new table
start_link() ->
GameVM = ggs_gamevm:start_link(),
{ok, Pid} = gen_server:start_link(?MODULE, [GameVM], []),
Pid.
{ok, Pid} = gen_server:start_link(?MODULE, [], []),
%% @private
call(Pid, Msg) ->
@ -47,19 +46,34 @@ stop(Table) ->
notify(Table, Player, Message) ->
gen_server:cast(Table, {notify, Player, Message}).
notify_all_players(Table, Message) ->
gen_server:cast(Table, {notify_all_players, Message}).
notify_game(Table, From, Message) ->
io:format("Notify game called on"),
erlang:display(Table),
io:format("~n"),
gen_server:cast(Table, {notify_game, Message, From}).
%% ----------------------------------------------------------------------
%% @private
init([GameVM]) ->
{ok, #state { game_vm = GameVM, players = [] }}.
init([]) ->
GameVM = ggs_gamevm_e:start_link(self()), %% @TODO: Temporary erlang gamevm
{ok, #state {
game_vm = GameVM,
players = [] }}.
%% @private
handle_call({add_player, Player}, _From, #state { players = Players } = State) ->
{reply, ok, State#state { players = [Player | Players] }};
handle_call({remove_player, Player}, _From, #state { players = Players } = State) ->
{reply, ok, State#state { players = Players -- [Player] }};
handle_call(get_player_list, _From, #state { players = Players } = State) ->
{reply, {ok, Players}, State};
handle_call(Msg, _From, State) ->
error_logger:error_report([unknown_msg, Msg]),
{reply, ok, State}.
@ -68,11 +82,25 @@ handle_call(Msg, _From, State) ->
handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) ->
case Message of
{server, define, Args} ->
ggs_gamevm:define(GameVM, Args);
ggs_gamevm_e:define(GameVM, Args);
{game, Command, Args} ->
ggs_gamevm:user_command(GameVM, Player, Command, Args)
ggs_gamevm_e:user_command(GameVM, Player, Command, Args)
end,
{noreply, State};
handle_cast({notify_game, Message, From}, #state { game_vm = GameVM } = State) ->
io:format("notify_game message received~n"),
ggs_gamevm_e:user_command(GameVM, From, Message, ""),
{noreply, State};
handle_cast({notify_all_players, Message}, #state{players = Players} = State) ->
io:format("Notifying all players... ~p~n", [Players]),
lists:foreach(fun(P) ->
io:format("Notifying ~p~n", [P]),
ggs_player:notify(P, "Server", Message)
end, Players),
{noreply, State};
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast(Msg, S) ->
@ -92,43 +120,80 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% @TODO: Please put these tests in a separate file. We can't compile this file if
%% they contain errors from switching vms
%% ----------------------------------------------------------------------
% Tests
start_link_test() ->
Table = start_link(),
?assertNot(Table =:= undefined).
%<<<<<<< HEAD
%start_link_test() ->
% Table = start_link(),
% ?assertNot(Table =:= undefined).
add_player_test() ->
Table = start_link(),
Player = test_player,
add_player(Table, Player),
{ok, [Player]} = gen_server:call(Table, get_player_list).
%add_player_test() ->
% Table = start_link(),
% Player = test_player,
% add_player(Table, Player),
% {ok, [Player]} = gen_server:call(Table, get_player_list).
remove_player_test() ->
Table = start_link(),
Player = test_player,
Player2 = test_player2,
add_player(Table, Player),
{ok, [Player]} = gen_server:call(Table, get_player_list),
add_player(Table, Player2),
{ok, [Player2, Player]} = gen_server:call(Table, get_player_list),
remove_player(Table, Player),
{ok, [Player2]} = gen_server:call(Table, get_player_list),
remove_player(Table, Player2),
{ok, []} = gen_server:call(Table, get_player_list).
stop_test() ->
Table = start_link(),
ok = stop(Table).
%remove_player_test() ->
% Table = start_link(),
% Player = test_player,
% Player2 = test_player2,
% add_player(Table, Player),
% {ok, [Player]} = gen_server:call(Table, get_player_list),
% add_player(Table, Player2),
% {ok, [Player2, Player]} = gen_server:call(Table, get_player_list),
% remove_player(Table, Player),
% {ok, [Player2]} = gen_server:call(Table, get_player_list),
% remove_player(Table, Player2),
% {ok, []} = gen_server:call(Table, get_player_list).
%
%stop_test() ->
% Table = start_link(),
% ok = stop(Table).
% @private
notify_test() ->
Table = start_link(),
Player = test_player,
Message = {server, define, "function helloWorld(x) { }"},
ok = notify(Table, Player, Message).
%notify_test() ->
% Table = start_link(),
% Player = test_player,
% Message = {server, define, "function helloWorld(x) { }"},
% ok = notify(Table, Player, Message).
%=======
%%start_link_test() ->
% Table = start_link("123", none),
% ?assertNot(Table =:= undefined).
%
%add_player_test() ->
% Table = start_link("123", none),
% Player = test_player,
% add_player(Table, Player),
% {ok, [Player]} = gen_server:call(Table, get_player_list).
%remove_player_test() ->
% Table = start_link("123", none),
% Player = test_player,
% Player2 = test_player2,
% add_player(Table, Player),
% {ok, [Player]} = gen_server:call(Table, get_player_list),
% add_player(Table, Player2),
% {ok, [Player2, Player]} = gen_server:call(Table, get_player_list),
% remove_player(Table, Player),
% {ok, [Player2]} = gen_server:call(Table, get_player_list),
% remove_player(Table, Player2),
% {ok, []} = gen_server:call(Table, get_player_list).
%
%stop_test() ->
% Table = start_link("123", none),
% ok = stop(Table).
%
%% @private
%notify_test() ->
% Table = start_link("123", none),
% Player = test_player,
% Message = {server, define, "function helloWorld(x) { }"},
% ok = notify(Table, Player, Message).
%>>>>>>> jonte_rewrite
%Message2 = {game, "helloWorld", "test"},
%ok = notify(Table, Player, Message2).

View file

@ -1,4 +1,8 @@
-module(helpers).
-export([not_implemented/0, get_new_token/0]).
not_implemented() ->
exit("Not implemented").
get_new_token() ->
string:strip(os:cmd("uuidgen"), right, $\n ).

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash
erl -boot start_sasl -pa ebin_test -pa erlang_js/ebin/ -pa erlv8/ebin -pa ebin -pa src -eval 'ggs_protocol_test:test_parse().'
erl -boot start_sasl -pa ebin_test -pa erlang_js/ebin/ -pa ebin -pa src -eval 'ggs_coordinator_test:test().'

View file

@ -0,0 +1,53 @@
-module(ggs_coordinator_test).
-include_lib("eunit/include/eunit.hrl").
coordinator_test_() ->
{foreach,
fun() ->
{ok, _Coord} = ggs_coordinator:start_link(),
timer:sleep(100)
end,
fun(_X) ->
ggs_coordinator:stop("End of test"),
timer:sleep(100)
end,
[
fun test_start_link/0,
fun test_stop/0,
fun test_join_bad_table/0,
fun test_join_lobby/0
]
}.
test_start_link() ->
% Check process info
PInfo = whereis(ggs_coordinator),
?assert((PInfo /= undefined)). % Did the server start?
test_stop() ->
ok = ggs_coordinator:stop(""), % Extra cleaning
timer:sleep(100),
% Did it stop?
?assert((whereis(ggs_coordinator)) == undefined).
test_join_bad_table() ->
Response = ggs_coordinator:join_table("Nonexistant table"),
?assert(Response == {error, no_such_table}).
test_join_lobby() ->
{Response, _} = ggs_coordinator:join_lobby(),
?assert(Response /= error).
%% 'Manual' tests
create_table_test() ->
{ok, _Coord} = ggs_coordinator:start_link(),
timer:sleep(100),
% Forcibly create a table. This functionality should be disabled
% in the production system, but is pretty nice for testing.
Response = ggs_coordinator:create_table({force, 1337}),
?assert(Response == {ok, 1337}).
join_good_table_test() ->
Response = ggs_coordinator:join_table(1337),
?assert(Response == {ok, 1337}).