From e38bab50545c71f20bd7234e6c6034bc965f5511 Mon Sep 17 00:00:00 2001 From: niklas Date: Thu, 17 Feb 2011 13:52:56 +0100 Subject: [PATCH 1/4] Modified player test --- tests/ggs_player_test.erl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tests/ggs_player_test.erl b/tests/ggs_player_test.erl index 1067f71..be38135 100644 --- a/tests/ggs_player_test.erl +++ b/tests/ggs_player_test.erl @@ -4,20 +4,24 @@ %% @doc start_link should always return ok for any valid socket. A valid socket %% should always return {ok, Pid} and {error, Reason} otherwise. start_link_test() -> - ggs_logger:not_implemented(). + ggs_logger:not_implemented(). %% @doc Given that start_link returned {ok, Player}. Notify shall always return ok and %% deliver a specified message through the socket. notify_test() -> - ggs_logger:not_implemented(). - + Player = start_link("bad arg"), + Message = {"something", ""}, + Ret = ggs_player:notify(Player, self(), Message) + ?assertNot(ok =:= Ret). + %% @doc Given that start_link returned {ok, Player}. get_token shall always return a valid %% player token. a valid token should be unique. get_token_test() -> - ggs_logger:not_implemented(). + ggs_logger:not_implemented(). -%% @doc Given that start_link returned {ok, Pid}. There shouldn't be possible to +%% @doc Given that start_link returned {ok, Pid}. There shouldn't be possible to %% execute this function with the same Player and Table arguments twice. stop_test() -> - ggs_logger:not_implemented(). - + Player = start_link(something), + Table = test, + ok = stop(Player, Table). From 041fee24419644398c9841ebd2b6450076053891 Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Thu, 17 Feb 2011 14:14:32 +0100 Subject: [PATCH 2/4] removed Token and Socket --- src/ggs_table.erl | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/ggs_table.erl b/src/ggs_table.erl index 1e4f323..3c0237d 100644 --- a/src/ggs_table.erl +++ b/src/ggs_table.erl @@ -1,4 +1,4 @@ -%% @doc This module represents a Player with a Socket and a Token +%% @doc This module represents a table with players -module(ggs_table). -behaviour(gen_server). @@ -7,10 +7,10 @@ -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). --record(state, { token, players, socket, game_vm } ). +-record(state, { players, game_vm } ). %% API --export([start_link/2, +-export([start_link/0, add_player/2, remove_player/2, stop/1, @@ -22,9 +22,9 @@ % API implementation % @doc returns a new table -start_link(Token, Socket) -> +start_link() -> GameVM = ggs_gamevm:start_link(), - {ok, Pid} = gen_server:start_link(?MODULE, [Token, Socket, GameVM], []), + {ok, Pid} = gen_server:start_link(?MODULE, [GameVM], []), Pid. %% @private @@ -50,11 +50,8 @@ notify(Table, Player, Message) -> %% ---------------------------------------------------------------------- %% @private -init([Token, Socket, GameVM]) -> - {ok, #state { token = Token, - socket = Socket, - game_vm = GameVM, - players = [] }}. +init([GameVM]) -> + {ok, #state { game_vm = GameVM, players = [] }}. %% @private handle_call({add_player, Player}, _From, #state { players = Players } = State) -> @@ -97,21 +94,20 @@ code_change(_OldVsn, State, _Extra) -> %% ---------------------------------------------------------------------- - % Tests start_link_test() -> - Table = start_link("123", none), + Table = start_link(), ?assertNot(Table =:= undefined). add_player_test() -> - Table = start_link("123", none), + 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("123", none), + Table = start_link(), Player = test_player, Player2 = test_player2, add_player(Table, Player), @@ -124,12 +120,12 @@ remove_player_test() -> {ok, []} = gen_server:call(Table, get_player_list). stop_test() -> - Table = start_link("123", none), + Table = start_link(), ok = stop(Table). % @private notify_test() -> - Table = start_link("123", none), + Table = start_link(), Player = test_player, Message = {server, define, "function helloWorld(x) { }"}, ok = notify(Table, Player, Message). From 44d26278ccc34a0c9a2d2718c063bd496d43bee1 Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Thu, 17 Feb 2011 20:05:12 +0100 Subject: [PATCH 3/4] rewrite to gen_server and added tests --- src/ggs_gamevm.erl | 126 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 29 deletions(-) diff --git a/src/ggs_gamevm.erl b/src/ggs_gamevm.erl index 1a8f547..15cbb64 100644 --- a/src/ggs_gamevm.erl +++ b/src/ggs_gamevm.erl @@ -1,24 +1,34 @@ --module(ggs_gamevm). --export([start_link/0, 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. +-module(ggs_gamevm). +-behaviour(gen_server). + +%% gen_server callbacks +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + terminate/2, code_change/3]). + +-record(state, { port, table } ). + +%% API +-export([start_link/1, define/2, user_command/4, stop/1, call_js/2]). + +-include_lib("eunit/include/eunit.hrl"). + + +%% ---------------------------------------------------------------------- +% API implementation + %% @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() -> +start_link(Table) -> erlang_js:start(), %% @TODO: should only be done once - PortPid = spawn_link( fun() -> - process_flag(trap_exit, true), - {ok, Port} = js_driver:new(), - js:define(Port, <<"function userCommand(user, command, args){return 'Hello world';}">>), - loop(Port) - end ), - PortPid. + {ok, Pid} = gen_server:start_link(?MODULE, [Table], []), + Pid. %% @doc Define some new code on the specified VM, returns the atom ok. define(GameVM, SourceCode) -> - GameVM ! {define,SourceCode}, - ok. + gen_server:cast(GameVM, {define, SourceCode}). %% @doc Execute a user command on the specified VM. This function is %% asynchronous, and returns ok. @@ -28,23 +38,81 @@ define(GameVM, SourceCode) -> %% 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}, + gen_server:cast(GameVM, {user_command, Player, Command, Args}). + +%% @private +% only for tests +call_js(GameVM, SourceCode) -> + gen_server:call(GameVM, {eval, SourceCode}). + +% @doc stops the gamevm process +stop(GameVM) -> + gen_server:cast(GameVM, stop). + + +%% ---------------------------------------------------------------------- + +%% @private +init([Table]) -> + process_flag(trap_exit, true), + {ok, Port} = js_driver:new(), + %% @TODO: add here default JS API instead + {ok, #state { port = Port, table = Table }}. + +%% private +% only needed for the tests +handle_call({eval, SourceCode}, _From, #state { port = Port } = State) -> + {ok, Ret} = js:eval(Port, list_to_binary(SourceCode)), + {reply, Ret, State}. + +%% @private +handle_cast({define, SourceCode}, #state { port = Port } = State) -> + ok = js:define(Port, list_to_binary(SourceCode)), + {noreply, State}; +handle_cast({user_command, Player, Command, Args}, #state { port = Port } = State) -> + Arguments = string:sub(string:join([Player, Command, Args], ","), "'", "\'"), + Js = list_to_binary(string:concat(string:concat("userCommand(", Arguments), ");")), + js_driver:define_js(Port, Js), + {noreply, State}; +handle_cast(stop, State) -> + {stop, normal, State}; +handle_cast(Msg, S) -> + error_logger:error_report([unknown_msg, Msg]), + {noreply, S}. + +%% @private +handle_info(Msg, S) -> + error_logger:error_report([unknown_msg, Msg]), + {noreply, S}. + +%% @private +terminate(_Reason, _State) -> ok. -%% Helper functions +%% @private +code_change(_OldVsn, State, _Extra) -> + {ok, State}. -loop(Port) -> - receive - {define, SourceCode} -> - ok = js:define(Port, list_to_binary(SourceCode)), - loop(Port); - {user_command, User, Command, Args, From, Ref} -> - {ok, Ret} = js:call(Port, <<"userCommand">>, - [ list_to_binary(User), - list_to_binary(Command), - list_to_binary(Args) - ]), - From ! {Ref, Ret}, - loop(Port) - end. +%% ---------------------------------------------------------------------- +% Tests + +start_link_test() -> + erlang_js:start(), %% @TODO: should only be done once + GameVM = start_link(test_table), + ?assertNot(GameVM =:= undefined). + +define_test() -> + GameVM = start_link(test_table), + define(GameVM, "function hello(test) { return test; }"), + ?assertMatch(<<"jeena">>, gen_server:call(GameVM, {eval, "hello('jeena')"})). + +stop_test() -> + GameVM = start_link(test_table), + ok = stop(GameVM). + +user_command_test() -> + GameVM = start_link(test_table), + define(GameVM, "var t = '';\nfunction userCommand(user, command, args) { t = user + command + args; }\n"), + user_command(GameVM, "'jeena'", "'thecommand'", "'theargs'"), + ?assertMatch(<<"jeenathecommandtheargs">>, gen_server:call(GameVM, {eval, "t;"})). + From dd9b92bf43b7969d896209b2aad98371c9d31b33 Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Thu, 17 Feb 2011 20:14:45 +0100 Subject: [PATCH 4/4] fixed escaping problem --- src/ggs_gamevm.erl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ggs_gamevm.erl b/src/ggs_gamevm.erl index 15cbb64..6493c9f 100644 --- a/src/ggs_gamevm.erl +++ b/src/ggs_gamevm.erl @@ -70,7 +70,8 @@ handle_cast({define, SourceCode}, #state { port = Port } = State) -> ok = js:define(Port, list_to_binary(SourceCode)), {noreply, State}; handle_cast({user_command, Player, Command, Args}, #state { port = Port } = State) -> - Arguments = string:sub(string:join([Player, Command, Args], ","), "'", "\'"), + Arguments = string:concat("'", string:concat( + string:join([js_escape(Player), js_escape(Command), js_escape(Args)], "','"), "'")), Js = list_to_binary(string:concat(string:concat("userCommand(", Arguments), ");")), js_driver:define_js(Port, Js), {noreply, State}; @@ -93,6 +94,9 @@ terminate(_Reason, _State) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. +js_escape(S) -> + lists:flatmap(fun($\') -> [$\\, $\']; (X) -> [X] end, S). + %% ---------------------------------------------------------------------- % Tests @@ -113,6 +117,6 @@ stop_test() -> user_command_test() -> GameVM = start_link(test_table), define(GameVM, "var t = '';\nfunction userCommand(user, command, args) { t = user + command + args; }\n"), - user_command(GameVM, "'jeena'", "'thecommand'", "'theargs'"), - ?assertMatch(<<"jeenathecommandtheargs">>, gen_server:call(GameVM, {eval, "t;"})). + user_command(GameVM, "'jeena", "thecommand", "theargs'"), + ?assertMatch(<<"'jeenathecommandtheargs'">>, gen_server:call(GameVM, {eval, "t;"})).