Now we can reconnect to a JSVM\!

This commit is contained in:
Jonatan Pålsson 2011-02-14 19:47:28 +01:00
commit 56c70c38b7
27 changed files with 381 additions and 40 deletions

BIN
src/.ggs_connection.erl.swp Normal file

Binary file not shown.

170
src/ggs_network.erl Normal file
View file

@ -0,0 +1,170 @@
%%%----------------------------------------------------
%%% @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.

View file

@ -90,8 +90,8 @@ handle_cast(stop, State) ->
% Handle javascript defines
handle_cast({srv_cmd, "define", Headers, Data}, State) ->
Token = ggs_protocol:getToken(Headers),
JSVM = getJSVM(Token, State),
js_runner:define(JSVM, Data),
GameVM = getJSVM(Token, State),
ggs_vm_runner:define(GameVM, Data),
send(State#state.lsock, "Token", "Okay, defined that for you!"),
{noreply, State};
@ -99,21 +99,21 @@ handle_cast({srv_cmd, "define", Headers, Data}, State) ->
handle_cast({srv_cmd, "call", Headers, Data}, State) ->
Token = ggs_protocol:getToken(Headers),
io:format("Got call request: ~p~n", [Data]),
JSVM = getJSVM(Token, State),
erlang:display(erlang:port_info(JSVM)),
{ok, Ret} = js_runner:call(JSVM, Data, []),%Payload, []),
GameVM = getJSVM(Token, State),
Ret = ggs_vm_runner:user_command(GameVM, "User", Data, []),
send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)),
{noreply, State};
% Set the new state to the reference generated, and JSVM associated
handle_cast({srv_cmd, "hello", Headers, Data}, State) ->
JSVM = js_runner:boot(),
GameVM = ggs_vm_runner:start_link(),
Client = getRef(),
send(State#state.lsock, Client, "This is your refID"),
OldMap = State#state.client_vm_map,
NewState = State#state{client_vm_map = OldMap ++ [{Client, JSVM}]},
gen_server:cast(ggs_backup, {set_backup, NewState}),
NewState = State#state{client_vm_map = OldMap ++ [{Client, GameVM}]},
gen_server:cast(ggs_backup, {set_backup, NewState}),
{noreply, NewState}.
%%-----------------------------------------------------
%% Helpers
%%-----------------------------------------------------

42
src/ggs_vm_runner.erl Normal file
View file

@ -0,0 +1,42 @@
-module(ggs_vm_runner).
-export([start_link/0, define/2, user_command/4]).
%Mattias
start_link() ->
erlang_js:start(),
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.
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.
define(GameVM, SourceCode) ->
GameVM ! {define,SourceCode}.
user_command(GameVM, User, Command, Args) ->
Ref = make_ref(),
GameVM ! {user_command, User, Command, Args, self(), Ref},
receive
{Ref, RetVal} ->
RetVal;
Other -> Other
end.

View file

@ -1,13 +0,0 @@
-module(js_runner).
-export([define/2,call/3, boot/0]).
boot() ->
erlang_js:start(),
{ok, Port} = js_driver:new(),
Port.
define(Port, Data) ->
ok = js:define(Port, list_to_binary(Data)).
call(Port, Func, Params) ->
js:call(Port, list_to_binary(Func), Params).