Interface modifications. Methods for vm handling.
This commit is contained in:
parent
05d5b60a9f
commit
1e08fe0015
3 changed files with 42 additions and 62 deletions
|
@ -95,44 +95,29 @@ handle_cast(stop, State) ->
|
|||
{stop, normal, State};
|
||||
|
||||
% Handle javascript defines
|
||||
handle_cast({define, Token, Payload}, State) ->
|
||||
JSVM = getJSVM(Token, State),
|
||||
%js_runner:define(JSVM, Payload),
|
||||
JSVM!{define,self(),Payload},
|
||||
handle_cast({define, Token, SourceCode}, State) ->
|
||||
GameVM = getJSVM(Token, State),
|
||||
ggs_vm_runner:define(GameVM, SourceCode),
|
||||
send(State#state.lsock, Token, "Okay, defined that for you!"),
|
||||
{noreply, State};
|
||||
|
||||
% Handle javascript calls
|
||||
handle_cast({call, Token, Payload}, State) ->
|
||||
io:format("test1~n"),
|
||||
io:format("Got call request: ~p~n", [Payload]),
|
||||
io:format("test2~n"),
|
||||
JSVM = getJSVM(Token, State),
|
||||
JSVM!{get_port, self()},
|
||||
receive
|
||||
{ok, Port} -> erlang:display(erlang:port_info(Port)),
|
||||
io:format("test1~n")
|
||||
end,
|
||||
%erlang:display(erlang:port_info(Port)),
|
||||
%{ok, Ret} = js_runner:call(JSVM, Payload, []),
|
||||
JSVM!{call, self(), Payload, []},
|
||||
receive
|
||||
{ok, Ret} ->
|
||||
send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)),
|
||||
{noreply, State}
|
||||
end;
|
||||
handle_cast({call, Token, Command}, State) ->
|
||||
GameVM = getJSVM(Token, State),
|
||||
ggs_vm_runner:user_command(GameVM, "User", Command, []),
|
||||
%send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)), Unessecary
|
||||
{noreply, State};
|
||||
|
||||
% Set the new state to the reference generated, and JSVM associated
|
||||
handle_cast({hello, _, _}, 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,
|
||||
JSVM!{get_port, self()},
|
||||
receive
|
||||
{ok, Port} -> NewState = State#state{client_vm_map = OldMap ++ [{Client, Port}]},
|
||||
gen_server:cast(ggs_backup, {set_backup, NewState}),
|
||||
{noreply, NewState}
|
||||
end.
|
||||
NewState = State#state{client_vm_map = OldMap ++ [{Client, GameVM}]},
|
||||
gen_server:cast(ggs_backup, {set_backup, NewState}),
|
||||
{noreply, NewState}.
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% Helpers
|
||||
%%-----------------------------------------------------
|
||||
|
|
28
src/ggs_vm_runner.erl
Normal file
28
src/ggs_vm_runner.erl
Normal file
|
@ -0,0 +1,28 @@
|
|||
-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}.
|
|
@ -1,33 +0,0 @@
|
|||
-module(js_runner).
|
||||
-export([boot/0]).
|
||||
|
||||
%Mattias
|
||||
boot() ->
|
||||
erlang_js:start(),
|
||||
{ok, Port} = js_driver:new(),
|
||||
PortPid = spawn(fun() -> port_process(Port) end ),
|
||||
PortPid.
|
||||
|
||||
|
||||
port_process(Port) ->
|
||||
receive
|
||||
{get_port, From} ->
|
||||
From!{ok,Port},
|
||||
port_process(Port);
|
||||
{define, From, Data} ->
|
||||
ok = js:define(From, list_to_binary(Data)),
|
||||
From!{ok},
|
||||
port_process(Port);
|
||||
{call, From, Func, Params} ->
|
||||
{ok,Ret} = js:call(From, list_to_binary(Func), Params), %Port unsure
|
||||
From!{ok,Ret},
|
||||
port_process(Port)
|
||||
end.
|
||||
|
||||
%These two babies will be ambigiuous
|
||||
%define(Port, Data) ->
|
||||
% port_pid!{define,self(),Port,Data}.
|
||||
|
||||
|
||||
%call(Port, Func, Params) ->
|
||||
% port_pid!{call, self(), Port, Func, Params}.
|
Reference in a new issue