Now we can define and run functions, but not in the same JSVM at the moment. A lookup is needed
This commit is contained in:
parent
f5fac06849
commit
4c6d0987bd
3 changed files with 49 additions and 23 deletions
|
@ -8,21 +8,35 @@ HOST = 'localhost' # The remote host
|
||||||
PORT = int(sys.argv[1]) # The same port as used by the server
|
PORT = int(sys.argv[1]) # The same port as used by the server
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
s.connect((HOST, PORT))
|
s.connect((HOST, PORT))
|
||||||
|
|
||||||
|
# Define ourselves a function!
|
||||||
|
|
||||||
s.send(
|
s.send(
|
||||||
'Token: 1001\n\
|
"Token: 1001\n\
|
||||||
Command: define\n\
|
Command: define\n\
|
||||||
Content-Type: text\n\
|
Content-Type: text\n\
|
||||||
Content-Length: 40\n\
|
Content-Length: 42\n\
|
||||||
\n\
|
\n\
|
||||||
function helloWorld(x) {return x + 2 ;}')
|
function myFun() {return 'Hello world!' ;}")
|
||||||
fs = s.makefile()
|
fs = s.makefile()
|
||||||
data = fs.readline()
|
data = fs.readline()
|
||||||
token = data.split(" ")[0]
|
token = data.split(" ")[0]
|
||||||
print "Token:", token
|
print "Token:", token
|
||||||
|
print "Data: ", ' '.join(data.split(" ")[1:])
|
||||||
|
|
||||||
s.send(token+" __echo 0 msg")
|
# Call that function!
|
||||||
data = s.recv(1024)
|
|
||||||
print data
|
|
||||||
|
|
||||||
|
s.send(
|
||||||
|
"Token: 1001\n\
|
||||||
|
Command: call\n\
|
||||||
|
Content-Type: text\n\
|
||||||
|
Content-Length: 6\n\
|
||||||
|
\n\
|
||||||
|
myFun()")
|
||||||
|
fs = s.makefile()
|
||||||
|
data = fs.readline()
|
||||||
|
token = data.split(" ")[0]
|
||||||
|
print "Token:", token
|
||||||
|
print "Data: ", ' '.join(data.split(" ")[1:])
|
||||||
s.close()
|
s.close()
|
||||||
|
|
||||||
|
|
|
@ -12,15 +12,27 @@ parse(Data) ->
|
||||||
), Message),
|
), Message),
|
||||||
% Hacky way to build a tuple, filter out not_found later on
|
% Hacky way to build a tuple, filter out not_found later on
|
||||||
Processed = {
|
Processed = {
|
||||||
|
case lists:keysearch("Command", 1, MsgKV) of
|
||||||
|
{value,{_, "define"}} ->
|
||||||
|
define;
|
||||||
|
{value,{_, "call"}} ->
|
||||||
|
call;
|
||||||
|
false ->
|
||||||
|
not_found
|
||||||
|
end,
|
||||||
case lists:keysearch("Token", 1, MsgKV) of
|
case lists:keysearch("Token", 1, MsgKV) of
|
||||||
{value,{_, Value}} ->
|
{value,{_, Value}} ->
|
||||||
Value;
|
Value;
|
||||||
false ->
|
false ->
|
||||||
not_found
|
not_found
|
||||||
end,
|
end,
|
||||||
case lists:keysearch("Command", 1, MsgKV) of
|
case lists:keysearch("Content-Length", 1, MsgKV) of
|
||||||
{value,{_, "define"}} ->
|
{value,{_, Value}} ->
|
||||||
define;
|
{Length, _} = string:to_integer(Value),
|
||||||
|
[_|Cont] = re:split(Data, "\n\n",[{return,list}]),
|
||||||
|
Content = string:join(Cont, "\n\n"),
|
||||||
|
Payload = string:substr(Content,1,Length),
|
||||||
|
Payload;
|
||||||
false ->
|
false ->
|
||||||
not_found
|
not_found
|
||||||
end
|
end
|
||||||
|
|
|
@ -79,6 +79,9 @@ handle_info({tcp, Socket, RawData}, State) ->
|
||||||
io:format("Old map: ~p NewState: ~p~n", [OldMap, NewState]),
|
io:format("Old map: ~p NewState: ~p~n", [OldMap, NewState]),
|
||||||
{noreply, State#state{client_vm_map = OldMap ++ [NewState]}};
|
{noreply, State#state{client_vm_map = OldMap ++ [NewState]}};
|
||||||
|
|
||||||
|
handle_info({tcp_closed, _}, State) ->
|
||||||
|
{stop, "Client closed socket", State};
|
||||||
|
|
||||||
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),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
@ -95,19 +98,16 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
|
|
||||||
do_JSCall(Socket, Data, State) ->
|
do_JSCall(Socket, Data, State) ->
|
||||||
JSVM = js_runner:boot(),
|
JSVM = js_runner:boot(),
|
||||||
js_runner:define(JSVM, "function userCommand(cmd, par) {return cmd+' '+ par}"),
|
|
||||||
Parsed = ggs_protocol:parse(Data),
|
Parsed = ggs_protocol:parse(Data),
|
||||||
NewState = case Parsed of
|
NewState = case Parsed of
|
||||||
{Token, define} ->
|
{define, Token, Payload} ->
|
||||||
io:format("Got define!!\n"),
|
js_runner:define(JSVM, Payload),
|
||||||
[];
|
send(Socket, "RefID", "Okay, defined that for you!"),
|
||||||
{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),
|
|
||||||
[];
|
[];
|
||||||
|
{call, Token, Payload} ->
|
||||||
|
Ret = js_runner:call(JSVM, Payload, []),%Payload, []),
|
||||||
|
send(Socket, "RefID", "JS says: ", Ret);
|
||||||
|
|
||||||
% Set the new state to the reference generated, and JSVM associated
|
% Set the new state to the reference generated, and JSVM associated
|
||||||
{hello} ->
|
{hello} ->
|
||||||
Client = getRef(),
|
Client = getRef(),
|
||||||
|
@ -120,11 +120,11 @@ do_JSCall(Socket, Data, State) ->
|
||||||
{crash, Zero} ->
|
{crash, Zero} ->
|
||||||
10/Zero;
|
10/Zero;
|
||||||
{vms} ->
|
{vms} ->
|
||||||
send(Socket, "RefID", State);
|
send(Socket, "RefID", State)
|
||||||
% Set the new state to []
|
% Set the new state to []
|
||||||
Other ->
|
% Other ->
|
||||||
send(Socket, "RefID", "__error"),
|
% send(Socket, "RefID", "__error"),
|
||||||
[]
|
% []
|
||||||
end,
|
end,
|
||||||
% Return the new state
|
% Return the new state
|
||||||
NewState.
|
NewState.
|
||||||
|
|
Reference in a new issue