Merge branch 'master' of github.com:jeena/GGS
This commit is contained in:
commit
ac550f616e
2 changed files with 37 additions and 25 deletions
|
@ -97,28 +97,42 @@ handle_cast(stop, State) ->
|
|||
% Handle javascript defines
|
||||
handle_cast({define, Token, Payload}, State) ->
|
||||
JSVM = getJSVM(Token, State),
|
||||
js_runner:define(JSVM, Payload),
|
||||
%js_runner:define(JSVM, Payload),
|
||||
JSVM!{define,self(),Payload},
|
||||
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]),
|
||||
JSVM = getJSVM(Token, State),
|
||||
erlang:display(erlang:port_info(JSVM)),
|
||||
{ok, Ret} = js_runner:call(JSVM, Payload, []),
|
||||
send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)),
|
||||
{noreply, State};
|
||||
|
||||
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;
|
||||
% Set the new state to the reference generated, and JSVM associated
|
||||
handle_cast({hello, _, _}, State) ->
|
||||
JSVM = js_runner:boot(),
|
||||
JSVM = js_runner:boot(),
|
||||
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}),
|
||||
{noreply, NewState}.
|
||||
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.
|
||||
%%-----------------------------------------------------
|
||||
%% Helpers
|
||||
%%-----------------------------------------------------
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
-module(js_runner).
|
||||
-export([boot/0,define/2,call/3]).
|
||||
-export([boot/0]).
|
||||
|
||||
%Mattias
|
||||
boot() ->
|
||||
erlang_js:start(),
|
||||
{ok, Port} = js_driver:new(),
|
||||
PortPid = spawn(fun() ->
|
||||
port_process(Port) end ),
|
||||
register(port_pid, PortPid),
|
||||
Port.
|
||||
PortPid = spawn(fun() -> port_process(Port) end ),
|
||||
PortPid.
|
||||
|
||||
|
||||
port_process(Port) ->
|
||||
|
@ -16,20 +14,20 @@ receive
|
|||
{get_port, From} ->
|
||||
From!{ok,Port},
|
||||
port_process(Port);
|
||||
{define,From, JSVM, Data} ->
|
||||
ok = js:define(JSVM, list_to_binary(Data)),
|
||||
{define, From, Data} ->
|
||||
ok = js:define(From, list_to_binary(Data)),
|
||||
From!{ok},
|
||||
port_process(Port);
|
||||
{call, From, JSVM, Func, Params} ->
|
||||
{ok,Ret} = js:call(JSVM, list_to_binary(Func), Params),
|
||||
{call, From, Func, Params} ->
|
||||
{ok,Ret} = js:call(From, list_to_binary(Func), Params), %Port unsure
|
||||
From!{ok,Ret},
|
||||
port_process(Port)
|
||||
end.
|
||||
|
||||
|
||||
define(Port, Data) ->
|
||||
port_pid!{define,self(),Port,Data}.
|
||||
%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}.
|
||||
%call(Port, Func, Params) ->
|
||||
% port_pid!{call, self(), Port, Func, Params}.
|
||||
|
|
Reference in a new issue