Initial interface
This commit is contained in:
parent
9c5ea322b8
commit
a801008cd9
3 changed files with 36 additions and 198 deletions
|
@ -1,170 +0,0 @@
|
|||
%%%----------------------------------------------------
|
||||
%%% @author Jonatan Pålsson <Jonatan.p@gmail.com>
|
||||
%%% @copyright 2010 Jonatan Pålsson
|
||||
%%% @doc RPC over TCP server
|
||||
%%% @end
|
||||
%%%----------------------------------------------------
|
||||
%%% @author Mattias Pettersson <mattiaspgames@gmail.com>
|
||||
%%% @doc Socket module for GGS
|
||||
%%% @end
|
||||
%%%----------------------------------------------------
|
||||
|
||||
|
||||
-module(ggs_network).
|
||||
|
||||
-behaviour(gen_server).
|
||||
|
||||
%define
|
||||
-define(SERVER, ?MODULE).
|
||||
-define(DEFAULT_PORT, 1055).
|
||||
|
||||
|
||||
% export
|
||||
-export([start_link/0,start_link/1]).
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2]).
|
||||
%-export([get_count/1,crash/0,vms/0,hello/0,echo/0]).
|
||||
-export([get_count/1]).
|
||||
-export([send/3, send/4]).
|
||||
-export([stop/0]).
|
||||
|
||||
%% gen_server callbacks
|
||||
-export([terminate/2, code_change/3]).
|
||||
|
||||
%state
|
||||
-record(state, {port, lsock, client_vm_map = []}).
|
||||
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% @doc Starts gen_server
|
||||
%% @end
|
||||
%%-----------------------------------------------------
|
||||
start_link() ->
|
||||
start_link(?DEFAULT_PORT).
|
||||
|
||||
start_link(Port) ->
|
||||
process_flag(trap_exit, true),
|
||||
gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []).
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% Creation
|
||||
%%-----------------------------------------------------
|
||||
init([Port]) ->
|
||||
{ok, LSock} = gen_tcp:listen(Port, [{active, true},
|
||||
{reuseaddr, true}]),
|
||||
{ok, #state{port = Port, lsock = LSock}, 0}.
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% @doc Fetches the number of requests made to this server
|
||||
%% @spec get_count() -> {ok, Count}
|
||||
%% where
|
||||
%% Count = integer()
|
||||
%% @end
|
||||
%%-----------------------------------------------------
|
||||
get_count(get_count) ->
|
||||
gen_server:call(?SERVER, get_count).
|
||||
|
||||
%crash() -> gen_server:call(?SERVER, crash).
|
||||
%vms() -> gen_server:call(?SERVER, vms).
|
||||
%hello() -> gen_server:call(?SERVER, hello).
|
||||
%echo() -> gen_server:call(?SERVER, {echo, RefID, _, MSG}).
|
||||
|
||||
|
||||
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% @doc Stops the server.
|
||||
%% @spec stop() -> ok
|
||||
%% @end
|
||||
%%-----------------------------------------------------
|
||||
stop() ->
|
||||
gen_server:cast(?SERVER, stop).
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% Handlers
|
||||
%%-----------------------------------------------------
|
||||
handle_call(get_count, _From, State) ->
|
||||
{reply, {ok, State#state.client_vm_map}, State}.
|
||||
|
||||
handle_cast(stop, State) ->
|
||||
{stop, normal, State}.
|
||||
|
||||
handle_info({tcp, Socket, RawData}, State) -> %parameters coming from gen_server
|
||||
NewState = do_JSCall(Socket, RawData, State), %TODO
|
||||
OldMap = State#state.client_vm_map,
|
||||
io:format("Old map: ~p NewState: ~p~n", [OldMap, NewState]),
|
||||
{noreply, State#state{client_vm_map = OldMap ++ [NewState]}};
|
||||
|
||||
handle_info(timeout, #state{lsock = LSock} = State) ->
|
||||
{ok, _Sock} = gen_tcp:accept(LSock),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% TCP Calls
|
||||
%%-----------------------------------------------------
|
||||
send(Socket, RefID, String) ->
|
||||
gen_tcp:send(Socket, io_lib:fwrite("~p ~p~n", [RefID,String])).
|
||||
|
||||
send(Socket, RefID, String1, String2) ->
|
||||
gen_tcp:send(Socket, io_lib:fwrite("~p ~p ~p~n", [RefID, String1, String2])).
|
||||
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% gen_server callbacks
|
||||
%%-----------------------------------------------------
|
||||
terminate(_Reason, _State) ->
|
||||
ok.
|
||||
|
||||
code_change(_OldVsn, State, _Extra) ->
|
||||
{ok, State}.
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% Internal functions
|
||||
%%-----------------------------------------------------
|
||||
do_JSCall(Socket, Data, State) ->
|
||||
JSVM = js_runner:boot(),
|
||||
js_runner:define(JSVM, "function userCommand(cmd, par) {return cmd+' '+ par}"),
|
||||
Parsed = ggs_protocol:parse(Data),
|
||||
NewState = case Parsed of
|
||||
{cmd, Command, Parameter} ->
|
||||
% Set the new state to []
|
||||
Ret = js_runner:call(JSVM, "userCommand",
|
||||
[list_to_binary(Command),
|
||||
list_to_binary(Parameter)]),
|
||||
send(Socket, "RefID", "JS says: ", Ret),
|
||||
[];
|
||||
% Set the new state to the reference generated, and JSVM associated
|
||||
{hello} ->
|
||||
Client = getRef(),
|
||||
send(Socket, Client, "__ok_hello"),
|
||||
{Client, JSVM};
|
||||
{echo, RefID, _, MSG} ->
|
||||
send(Socket, RefID, "Your VM is ", getJSVM(RefID, State)),
|
||||
[];
|
||||
{crash, Zero} ->
|
||||
10/Zero;
|
||||
{vms} ->
|
||||
send(Socket, "RefID", State);
|
||||
% Set the new state to []
|
||||
Other ->
|
||||
send(Socket, "RefID", "__error"),
|
||||
[]
|
||||
end,
|
||||
% Return the new state
|
||||
NewState.
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% Helpers
|
||||
%%-----------------------------------------------------
|
||||
getRef() ->
|
||||
{A1,A2,A3} = now(),
|
||||
random:seed(A1, A2, A3),
|
||||
random:uniform(1000).
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% Helpers
|
||||
%%-----------------------------------------------------
|
||||
getJSVM(RefID, State) ->
|
||||
VMs = State#state.client_vm_map,
|
||||
{value, {_,VM}} = lists:keysearch(RefID, 1, VMs),
|
||||
VM.
|
|
@ -0,0 +1,36 @@
|
|||
%% @doc This module represents a Player with a Socket and a Token
|
||||
|
||||
-module(ggs_table).
|
||||
-export([
|
||||
start_link/1,
|
||||
add_player/2,
|
||||
remove_player/2,
|
||||
stop/2,
|
||||
notify/3
|
||||
]).
|
||||
|
||||
% @doc returns a new table
|
||||
start_link(Token) ->
|
||||
not_implemented().
|
||||
|
||||
% @doc adds a player to a table
|
||||
add_player(Table, Player) ->
|
||||
not_implemented().
|
||||
|
||||
% @doc removes player form a table
|
||||
remove_player(Table, Player) ->
|
||||
not_implemented().
|
||||
|
||||
% @doc stops the table process
|
||||
stop(Table) ->
|
||||
not_implemented().
|
||||
|
||||
% @doc notifies the table with a message from a player
|
||||
notify(Table, Player, Message) ->
|
||||
not_implemented().
|
||||
|
||||
|
||||
% loop
|
||||
|
||||
|
||||
% private helpers
|
|
@ -1,28 +0,0 @@
|
|||
-module(ggs_vm_runner).
|
||||
-export([start_link/0, define/2, user_command/4]).
|
||||
|
||||
%Mattias
|
||||
start_link() ->
|
||||
erlang_js:start(),
|
||||
{ok, Port} = js_driver:new(),
|
||||
js:define(Port, <<"function userCommand(user, command, args){}">>),
|
||||
PortPid = spawn_link(fun() -> loop(Port) end ),
|
||||
PortPid.
|
||||
|
||||
|
||||
loop(Port) ->
|
||||
receive
|
||||
{define, SourceCode} ->
|
||||
ok = js:define(Port, list_to_binary(SourceCode)),
|
||||
loop(Port);
|
||||
{user_command, User, Command, Args} ->
|
||||
{ok, Ret} = js:call(Port, <<"userCommand">>, list_to_binary([User,Command,Args])),
|
||||
loop(Port)
|
||||
end.
|
||||
|
||||
|
||||
define(GameVM, SourceCode) ->
|
||||
GameVM ! {define,SourceCode}.
|
||||
|
||||
user_command(GameVM, User, Command, Args) ->
|
||||
GameVM ! {user_command, User, Command, Args}.
|
Reference in a new issue