Added tracking of {Client, JSVM} mappings
This commit is contained in:
parent
43a51430bc
commit
2c083dd3dd
3 changed files with 30 additions and 41 deletions
20
echo_test.rb
20
echo_test.rb
|
@ -1,20 +0,0 @@
|
||||||
#!/usr/bin/env ruby -wKU
|
|
||||||
|
|
||||||
require 'socket' # Sockets are in standard library
|
|
||||||
|
|
||||||
hostname = 'localhost'
|
|
||||||
port = 7000
|
|
||||||
|
|
||||||
print "Which port @ loclhost?"
|
|
||||||
port = gets
|
|
||||||
|
|
||||||
s = TCPSocket.open(hostname, port.chop)
|
|
||||||
|
|
||||||
s.print("__hello")
|
|
||||||
|
|
||||||
while true
|
|
||||||
line = s.gets # Read lines from the socket
|
|
||||||
puts ">> " + line.chop # And print with platform line terminator
|
|
||||||
s.print(gets.chop)
|
|
||||||
end
|
|
||||||
s.close # Close the socket when done
|
|
|
@ -10,17 +10,17 @@ parse(Data) ->
|
||||||
case Message of
|
case Message of
|
||||||
[RefID, "__error", Size, Message ] ->
|
[RefID, "__error", Size, Message ] ->
|
||||||
{ok, you_said_error};
|
{ok, you_said_error};
|
||||||
[_, "__boot", _ ] ->
|
[_, "__boot", _ ] ->
|
||||||
{ok, you_said_boot};
|
{ok, you_said_boot};
|
||||||
[RefID, "__stop", _] ->
|
[RefID, "__stop", _] ->
|
||||||
{ok, you_said_stop};
|
{ok, you_said_stop};
|
||||||
[RefID, "__start", _] ->
|
[RefID, "__start", _] ->
|
||||||
{ok, you_said_start};
|
{ok, you_said_start};
|
||||||
["__hello", _] ->
|
["__hello", _] ->
|
||||||
{hello};
|
{hello};
|
||||||
[RefID, "__define",_, JavaScript ] ->
|
[RefID, "__define",_, JavaScript ] ->
|
||||||
{ok, you_said_define};
|
{ok, you_said_define};
|
||||||
[RefID, Command, _, Parameter ] ->
|
[RefID, Command, _, Parameter ] ->
|
||||||
{cmd, Command, Parameter};
|
{cmd, Command, Parameter};
|
||||||
Other ->
|
Other ->
|
||||||
{out_of_bounds, Other}
|
{out_of_bounds, Other}
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
-define(SERVER, ?MODULE).
|
-define(SERVER, ?MODULE).
|
||||||
-define(DEFAULT_PORT, 1055).
|
-define(DEFAULT_PORT, 1055).
|
||||||
|
|
||||||
-record(state, {port, lsock, request_count = 0}).
|
-record(state, {port, lsock, client_vm_map = []}).
|
||||||
|
|
||||||
%%%====================================================
|
%%%====================================================
|
||||||
%%% API
|
%%% API
|
||||||
|
@ -66,15 +66,16 @@ init([Port]) ->
|
||||||
{ok, #state{port = Port, lsock = LSock}, 0}.
|
{ok, #state{port = Port, lsock = LSock}, 0}.
|
||||||
|
|
||||||
handle_call(get_count, _From, State) ->
|
handle_call(get_count, _From, State) ->
|
||||||
{reply, {ok, State#state.request_count}, State}.
|
{reply, {ok, State#state.client_vm_map}, State}.
|
||||||
|
|
||||||
handle_cast(stop, State) ->
|
handle_cast(stop, State) ->
|
||||||
{stop, normal, State}.
|
{stop, normal, State}.
|
||||||
|
|
||||||
handle_info({tcp, Socket, RawData}, State) ->
|
handle_info({tcp, Socket, RawData}, State) ->
|
||||||
do_JSDefine(Socket, RawData),
|
NewState = do_JSCall(Socket, RawData, State),
|
||||||
RequestCount = State#state.request_count,
|
OldMap = State#state.client_vm_map,
|
||||||
{noreply, State#state{request_count = RequestCount + 1}};
|
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) ->
|
handle_info(timeout, #state{lsock = LSock} = State) ->
|
||||||
{ok, _Sock} = gen_tcp:accept(LSock),
|
{ok, _Sock} = gen_tcp:accept(LSock),
|
||||||
|
@ -90,26 +91,34 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
|
|
||||||
do_JSDefine(Socket, Data) ->
|
do_JSCall(Socket, Data, State) ->
|
||||||
JSVM = js_runner:boot(),
|
JSVM = js_runner:boot(),
|
||||||
js_runner:define(JSVM, "function userCommand(cmd, par) {return cmd+' '+ par}"),
|
js_runner:define(JSVM, "function userCommand(cmd, par) {return cmd+' '+ par}"),
|
||||||
Parsed = ggs_protocol:parse(Data),
|
Parsed = ggs_protocol:parse(Data),
|
||||||
case Parsed of
|
NewState = case Parsed of
|
||||||
{cmd, Command, Parameter} ->
|
{cmd, Command, Parameter} ->
|
||||||
|
% Set the new state to []
|
||||||
Ret = js_runner:call(JSVM, "userCommand",
|
Ret = js_runner:call(JSVM, "userCommand",
|
||||||
[list_to_binary(Command),
|
[list_to_binary(Command),
|
||||||
list_to_binary(Parameter)]),
|
list_to_binary(Parameter)]),
|
||||||
send(Socket, "JS says: ", Ret);
|
send(Socket, "JS says: ", Ret),
|
||||||
|
[];
|
||||||
|
% Set the new state to the reference generated, and JSVM associated
|
||||||
{hello} ->
|
{hello} ->
|
||||||
io:format("Got hello!"),
|
Client = make_ref(),
|
||||||
send(Socket, make_ref(), "__ok_hello");
|
send(Socket, Client, "__ok_hello"),
|
||||||
|
{Client, JSVM};
|
||||||
|
% Set the new state to []
|
||||||
Other ->
|
Other ->
|
||||||
io:format("Got '~p'", [Other]),
|
io:format("Got '~p'", [Other]),
|
||||||
send(Socket, "__error")
|
send(Socket, "__error"),
|
||||||
end.
|
[]
|
||||||
|
end,
|
||||||
do_JSCall(Socket, Function, Parameters) ->
|
% Return the new state
|
||||||
ok.
|
NewState.
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
%% Helpers
|
||||||
|
%%-----------------------------------------------------
|
||||||
|
|
||||||
send(Socket, String) ->
|
send(Socket, String) ->
|
||||||
gen_tcp:send(Socket, io_lib:fwrite("~p~n", [String])).
|
gen_tcp:send(Socket, io_lib:fwrite("~p~n", [String])).
|
||||||
|
|
Reference in a new issue