Now we can call the defined code (lookup performed)

This commit is contained in:
Jonatan Pålsson 2011-02-04 19:52:43 +01:00
parent 4c6d0987bd
commit 40fdb8b431
3 changed files with 35 additions and 13 deletions

View file

@ -9,33 +9,48 @@ 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))
# Say hello
print "Saying hello to server"
s.send(
"Command: hello\n\
Content-Type: text\n\
Content-Length: 0\n\
\n\
")
fs = s.makefile()
data = fs.readline()
token = data.split(" ")[0]
print "Token:", token
print "Data: ", ' '.join(data.split(" ")[1:])
# Define ourselves a function! # Define ourselves a function!
print "Defining a function called myFun"
s.send( s.send(
"Token: 1001\n\ "Token: %s\n\
Command: define\n\ Command: define\n\
Content-Type: text\n\ Content-Type: text\n\
Content-Length: 42\n\ Content-Length: 42\n\
\n\ \n\
function myFun() {return 'Hello world!' ;}") function myFun() {return 'Hello world!' ;}" % token)
fs = s.makefile() fs = s.makefile()
data = fs.readline() data = fs.readline()
token = data.split(" ")[0]
print "Token:", token print "Token:", token
print "Data: ", ' '.join(data.split(" ")[1:]) print "Data: ", ' '.join(data.split(" ")[1:])
# Call that function! # Call that function!
print "Calling myFun"
s.send( s.send(
"Token: 1001\n\ "Token: %s\n\
Command: call\n\ Command: call\n\
Content-Type: text\n\ Content-Type: text\n\
Content-Length: 6\n\ Content-Length: 6\n\
\n\ \n\
myFun()") myFun" % token)
fs = s.makefile() fs = s.makefile()
data = fs.readline() data = fs.readline()
token = data.split(" ")[0]
print "Token:", token print "Token:", token
print "Data: ", ' '.join(data.split(" ")[1:]) print "Data: ", ' '.join(data.split(" ")[1:])
s.close() s.close()

View file

@ -17,6 +17,8 @@ parse(Data) ->
define; define;
{value,{_, "call"}} -> {value,{_, "call"}} ->
call; call;
{value,{_, "hello"}} ->
hello;
false -> false ->
not_found not_found
end, end,

View file

@ -97,22 +97,23 @@ code_change(_OldVsn, State, _Extra) ->
%%----------------------------------------------------- %%-----------------------------------------------------
do_JSCall(Socket, Data, State) -> do_JSCall(Socket, Data, State) ->
JSVM = js_runner:boot(),
Parsed = ggs_protocol:parse(Data), Parsed = ggs_protocol:parse(Data),
NewState = case Parsed of NewState = case Parsed of
{define, Token, Payload} -> {define, Token, Payload} ->
JSVM = getJSVM(Token, State),
js_runner:define(JSVM, Payload), js_runner:define(JSVM, Payload),
send(Socket, "RefID", "Okay, defined that for you!"), send(Socket, "RefID", "Okay, defined that for you!"),
[]; [];
{call, Token, Payload} -> {call, Token, Payload} ->
JSVM = getJSVM(Token, State),
Ret = js_runner:call(JSVM, Payload, []),%Payload, []), Ret = js_runner:call(JSVM, Payload, []),%Payload, []),
send(Socket, "RefID", "JS says: ", Ret); 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(), JSVM = js_runner:boot(),
send(Socket, Client, "__ok_hello"), Client = integer_to_list(getRef()),
%gen_server:call(ggs_mnesia_controller_server, {hello, "Someone said hello!"}), send(Socket, Client, "This is your refID"),
{Client, JSVM}; {Client, JSVM};
{echo, RefID, _, MSG} -> {echo, RefID, _, MSG} ->
send(Socket, RefID, "Your VM is ", getJSVM(RefID, State)), send(Socket, RefID, "Your VM is ", getJSVM(RefID, State)),
@ -138,11 +139,15 @@ getRef() ->
getJSVM(RefID, State) -> getJSVM(RefID, State) ->
VMs = State#state.client_vm_map, VMs = State#state.client_vm_map,
erlang:display(RefID),
erlang:display(VMs),
{value, {_,VM}} = lists:keysearch(RefID, 1, VMs), {value, {_,VM}} = lists:keysearch(RefID, 1, VMs),
VM. VM.
send(Socket, RefID, String) -> send(Socket, RefID, String) ->
gen_tcp:send(Socket, io_lib:fwrite("~p ~p~n", [RefID,String])). {Ref, _} = string:to_integer(RefID),
gen_tcp:send(Socket, io_lib:fwrite("~p ~p~n", [Ref,String])).
send(Socket, RefID, String1, String2) -> send(Socket, RefID, String1, String2) ->
gen_tcp:send(Socket, io_lib:fwrite("~p ~p ~p~n", [RefID, String1, String2])). {Ref, _} = string:to_integer(RefID),
gen_tcp:send(Socket, io_lib:fwrite("~p ~p ~p~n", [Ref, String1, String2])).