From 2c083dd3ddcabab0d52bf388526300324e25158f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20P=C3=A5lsson?= Date: Mon, 31 Jan 2011 18:36:15 +0100 Subject: [PATCH] Added tracking of {Client, JSVM} mappings --- echo_test.rb | 20 -------------------- src/ggs_protocol.erl | 12 ++++++------ src/ggs_server.erl | 39 ++++++++++++++++++++++++--------------- 3 files changed, 30 insertions(+), 41 deletions(-) delete mode 100644 echo_test.rb diff --git a/echo_test.rb b/echo_test.rb deleted file mode 100644 index e62b07f..0000000 --- a/echo_test.rb +++ /dev/null @@ -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 diff --git a/src/ggs_protocol.erl b/src/ggs_protocol.erl index 0e979d6..8fc2baf 100644 --- a/src/ggs_protocol.erl +++ b/src/ggs_protocol.erl @@ -10,17 +10,17 @@ parse(Data) -> case Message of [RefID, "__error", Size, Message ] -> {ok, you_said_error}; - [_, "__boot", _ ] -> + [_, "__boot", _ ] -> {ok, you_said_boot}; - [RefID, "__stop", _] -> + [RefID, "__stop", _] -> {ok, you_said_stop}; - [RefID, "__start", _] -> + [RefID, "__start", _] -> {ok, you_said_start}; - ["__hello", _] -> + ["__hello", _] -> {hello}; - [RefID, "__define",_, JavaScript ] -> + [RefID, "__define",_, JavaScript ] -> {ok, you_said_define}; - [RefID, Command, _, Parameter ] -> + [RefID, Command, _, Parameter ] -> {cmd, Command, Parameter}; Other -> {out_of_bounds, Other} diff --git a/src/ggs_server.erl b/src/ggs_server.erl index 5509823..1d5d324 100644 --- a/src/ggs_server.erl +++ b/src/ggs_server.erl @@ -23,7 +23,7 @@ -define(SERVER, ?MODULE). -define(DEFAULT_PORT, 1055). --record(state, {port, lsock, request_count = 0}). +-record(state, {port, lsock, client_vm_map = []}). %%%==================================================== %%% API @@ -66,15 +66,16 @@ init([Port]) -> {ok, #state{port = Port, lsock = LSock}, 0}. 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) -> {stop, normal, State}. handle_info({tcp, Socket, RawData}, State) -> - do_JSDefine(Socket, RawData), - RequestCount = State#state.request_count, - {noreply, State#state{request_count = RequestCount + 1}}; + NewState = do_JSCall(Socket, RawData, State), + OldMap = State#state.client_vm_map, + 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) -> {ok, _Sock} = gen_tcp:accept(LSock), @@ -90,26 +91,34 @@ code_change(_OldVsn, State, _Extra) -> %% Internal functions %%----------------------------------------------------- -do_JSDefine(Socket, Data) -> +do_JSCall(Socket, Data, State) -> JSVM = js_runner:boot(), js_runner:define(JSVM, "function userCommand(cmd, par) {return cmd+' '+ par}"), Parsed = ggs_protocol:parse(Data), - case Parsed of + NewState = case Parsed of {cmd, Command, Parameter} -> + % Set the new state to [] Ret = js_runner:call(JSVM, "userCommand", [list_to_binary(Command), 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} -> - io:format("Got hello!"), - send(Socket, make_ref(), "__ok_hello"); + Client = make_ref(), + send(Socket, Client, "__ok_hello"), + {Client, JSVM}; + % Set the new state to [] Other -> io:format("Got '~p'", [Other]), - send(Socket, "__error") - end. - -do_JSCall(Socket, Function, Parameters) -> - ok. + send(Socket, "__error"), + [] + end, + % Return the new state + NewState. +%%----------------------------------------------------- +%% Helpers +%%----------------------------------------------------- send(Socket, String) -> gen_tcp:send(Socket, io_lib:fwrite("~p~n", [String])).