From 6aad821279aa0da13539633922dd34d031318396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20P=C3=A5lsson?= Date: Sun, 6 Feb 2011 22:32:08 +0100 Subject: [PATCH 1/5] Converted protocol -> ggs_server communication to OTP --- erlang_js | 2 +- src/ggs_protocol.erl | 32 +--------------- src/ggs_server.erl | 89 ++++++++++++++------------------------------ 3 files changed, 30 insertions(+), 93 deletions(-) diff --git a/erlang_js b/erlang_js index 5350ed2..cbac148 160000 --- a/erlang_js +++ b/erlang_js @@ -1 +1 @@ -Subproject commit 5350ed21606606dbee5ecb07e974f2abb9106270 +Subproject commit cbac148c440a93db44bad767a43c6d8619f6871f diff --git a/src/ggs_protocol.erl b/src/ggs_protocol.erl index 4d65739..ede5115 100644 --- a/src/ggs_protocol.erl +++ b/src/ggs_protocol.erl @@ -1,6 +1,3 @@ -%% This is a parser for JSON implemented using mochijson2 -%% Don't feed it anything other than valid JSON. - -module(ggs_protocol). -export([parse/1]). @@ -39,31 +36,4 @@ parse(Data) -> not_found end }, - Processed. -% case Message of -% ["__get_vms"] -> -% {vms}; -% [RefID, "__error", Size, Message ] -> -% {ok, you_said_error}; -% [_, "__boot", _ ] -> -% {ok, you_said_boot}; -% [RefID, "__stop", _] -> -% {ok, you_said_stop}; -% [RefID, "__start", _] -> -% {ok, you_said_start}; -% ["__hello", _] -> -% {hello}; -% [RefID, "__define",_, JavaScript ] -> -% {ok, you_said_define}; -% [RefID, "__echo", Length, Msg ] -> -% {Ref, _} = string:to_integer(RefID), -% {echo, Ref, Length, Msg}; -% [RefID, Command, _, Parameter ] -> -% {cmd, Command, Parameter}; -% %% Debugging tools, not for production use -% ["__crash"] -> -% {crash, 0}; -% %% End debugging tools -% Other -> -% {out_of_bounds, Other} -% end. + gen_server:cast(ggs_server, Processed). diff --git a/src/ggs_server.erl b/src/ggs_server.erl index dff29f3..68b1625 100644 --- a/src/ggs_server.erl +++ b/src/ggs_server.erl @@ -11,7 +11,6 @@ %% API -export([start_link/1, start_link/0, - get_count/0, stop/0 ]). @@ -39,16 +38,6 @@ start_link(Port) -> start_link() -> start_link(?DEFAULT_PORT). -%%----------------------------------------------------- -%% @doc Fetches the number of requests made to this server -%% @spec get_count() -> {ok, Count} -%% where -%% Count = integer() -%% @end -%%----------------------------------------------------- -get_count() -> - gen_server:call(?SERVER, get_count). - %%----------------------------------------------------- %% @doc Stops the server. %% @spec stop() -> ok @@ -73,21 +62,14 @@ init([Port]) -> {ok, State#state{lsock = LSock}, 0} end. -handle_call(get_count, _From, State) -> - {reply, {ok, State#state.client_vm_map}, State}; - handle_call({backup_state, OldState}, _From, State) -> io:format("Received old state from backup~n"), {noreply, OldState}. -handle_cast(stop, State) -> - {stop, normal, State}. handle_info({tcp, Socket, RawData}, State) -> - 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]}}; + ggs_protocol:parse(RawData), + {noreply, State#state{lsock = Socket}}; handle_info({tcp_closed, Socket}, State) -> gen_tcp:close(Socket), @@ -109,49 +91,34 @@ code_change(_OldVsn, State, _Extra) -> %%----------------------------------------------------- %% Internal functions %%----------------------------------------------------- +handle_cast(stop, State) -> + {stop, normal, State}; -do_JSCall(Socket, Data, State) -> - Parsed = ggs_protocol:parse(Data), - NewState = case Parsed of - {define, Token, Payload} -> - JSVM = getJSVM(Token, State), - js_runner:define(JSVM, Payload), - send(Socket, Token, "Okay, defined that for you!"), - []; - {call, Token, Payload} -> - 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, []),%Payload, []), - send(Socket, Token, "JS says:", binary_to_list(Ret)); +% Handle javascript defines +handle_cast({define, Token, Payload}, State) -> + JSVM = getJSVM(Token, State), + js_runner:define(JSVM, Payload), + send(State#state.lsock, Token, "Okay, defined that for you!"), + {noreply, State}; + +% Handle javascript calls +handle_cast({call, Token, Payload}, State) -> + 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, []),%Payload, []), + send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)), + {noreply, State}; - % Set the new state to the reference generated, and JSVM associated - {hello, _, _} -> - JSVM = js_runner:boot(), - Client = getRef(), - send(Socket, Client, "This is your refID"), - - %% This shouldnt be here... - OldMap = State#state.client_vm_map, - BackupState = State#state{client_vm_map = OldMap ++ [{Client, JSVM}]}, - gen_server:cast(ggs_backup, {set_backup, BackupState}), - %% - - {Client, JSVM}; - {echo, RefID, _, MSG} -> - send(Socket, RefID, "Your VM is ", getJSVM(RefID, State)), - []; - {crash, Zero} -> - 10/Zero; - {vms} -> - send(Socket, "RefID", State); - % Set the new state to [] - Other -> - send(Socket, "RefID", "__error"), - [] - end, - % Return the new state - NewState. +% Set the new state to the reference generated, and JSVM associated +handle_cast({hello, _, _}, State) -> + 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}. %%----------------------------------------------------- %% Helpers %%----------------------------------------------------- From c67bcc32e7452f1064330ca1db0bfe8ce37a45f2 Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Mon, 7 Feb 2011 01:21:51 -0800 Subject: [PATCH 2/5] removed double -sname --- start | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 start diff --git a/start b/start old mode 100755 new mode 100644 index 7dd9673..6de5737 --- a/start +++ b/start @@ -1,3 +1,3 @@ #!/usr/bin/env bash -erl -sname ggs -mnesia -boot start_sasl -pa erlang_js/ebin/ -pa ebin -pa src -s start_ggs -sname ggs +erl -sname ggs -mnesia -boot start_sasl -pa erlang_js/ebin/ -pa ebin -pa src -s start_ggs From fa63ea104bf036e2bd822260b946593847e2ad9c Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Fri, 11 Feb 2011 12:28:34 +0100 Subject: [PATCH 3/5] can't remember --- HOWTO | 2 +- build | 2 +- client | 17 +++++++++++++++++ erlang_js | 2 +- games/tic-tac-toe-js/index.html | 12 ++++++++++-- games/tic-tac-toe-js/proxy.rb | 0 python_client | 2 +- src/key_value_store.erl | 22 ++++++++++++++++++++++ start | 0 9 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 client create mode 100644 games/tic-tac-toe-js/proxy.rb create mode 100644 src/key_value_store.erl mode change 100644 => 100755 start diff --git a/HOWTO b/HOWTO index 064d127..2d28ea8 100644 --- a/HOWTO +++ b/HOWTO @@ -3,7 +3,7 @@ python version 2.x set to default. INSTALL 1. Cd into directory where you to have the project -2. git-clone git@github.com:jeena/GGS.git (remember to have a local key) +2. git clone git@github.com:jeena/GGS.git (remember to have a local key) 3. cd GGS/ 4. git submodule init 5. git submodule update diff --git a/build b/build index d468113..a9c1c79 100755 --- a/build +++ b/build @@ -3,4 +3,4 @@ for i in `find src -name "*.erl"` do erlc -o ebin $i -done +done \ No newline at end of file diff --git a/client b/client new file mode 100644 index 0000000..dff11aa --- /dev/null +++ b/client @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby -wKU + +require 'socket' # Sockets are in standard library + +hostname = 'localhost' +port = 7000 + +s = TCPSocket.open(hostname, port) + + + +s.print(q.chop) + +while line = s.gets # Read lines from the socket + puts "Got Echo: " + line.chop # And print with platform line terminator +end +s.close # Close the socket when done diff --git a/erlang_js b/erlang_js index cbac148..5350ed2 160000 --- a/erlang_js +++ b/erlang_js @@ -1 +1 @@ -Subproject commit cbac148c440a93db44bad767a43c6d8619f6871f +Subproject commit 5350ed21606606dbee5ecb07e974f2abb9106270 diff --git a/games/tic-tac-toe-js/index.html b/games/tic-tac-toe-js/index.html index 97d3c4f..5bdb286 100644 --- a/games/tic-tac-toe-js/index.html +++ b/games/tic-tac-toe-js/index.html @@ -12,8 +12,16 @@ function init() { GameServer.addGame(game_name, main()); - GameServer.addClient(game_name, new TicTacToeClient(frames.player1.document.getElementById("p1"), GameServer)); - GameServer.addClient(game_name, new TicTacToeClient(frames.player2.document.getElementById("p2"), GameServer)); + GameServer.addClient( + game_name, + new TicTacToeClient(frames.player1.document.getElementById("p1"), + GameServer + )); + GameServer.addClient( + game_name, + new TicTacToeClient(frames.player2.document.getElementById("p2"), + GameServer + )); } diff --git a/games/tic-tac-toe-js/proxy.rb b/games/tic-tac-toe-js/proxy.rb new file mode 100644 index 0000000..e69de29 diff --git a/python_client b/python_client index f79ff6e..9a1f1a9 100755 --- a/python_client +++ b/python_client @@ -31,7 +31,7 @@ Command: define\n\ Content-Type: text\n\ Content-Length: 49\n\ \n\ -function myFun() {return callErlang('a', 'b') ;}" % token) +function myFun() {return 'Hello World!' ;}" % token) fs = s.makefile() data = fs.readline() print "Token:", token diff --git a/src/key_value_store.erl b/src/key_value_store.erl new file mode 100644 index 0000000..70a2dc6 --- /dev/null +++ b/src/key_value_store.erl @@ -0,0 +1,22 @@ +-module(key_value_store). +-export(start/0,stop/0,set_item/2,get_item/1,get/1,length(0,clean/0). + +start() -> + spawn_link(fun() -> loop([]) end). + +stop() -> + self() ! {stop} + +loop(Touples) -> + receive -> + {set_item, Key, Value} -> + Touple = find(Touples, Key) + {stop} -> + {'EXIT', normal} + +find([], _) -> false; +find([{Key, _}|Tuples], Match) -> + case Key == Match of + true -> true; + false -> find(Tuples, Match) + end. \ No newline at end of file diff --git a/start b/start old mode 100644 new mode 100755 From 3ccef1e7114fc1bae4a6e8d7913f910932d5e115 Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Fri, 11 Feb 2011 13:58:59 +0100 Subject: [PATCH 4/5] removed file --- src/key_value_store.erl | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 src/key_value_store.erl diff --git a/src/key_value_store.erl b/src/key_value_store.erl deleted file mode 100644 index 70a2dc6..0000000 --- a/src/key_value_store.erl +++ /dev/null @@ -1,22 +0,0 @@ --module(key_value_store). --export(start/0,stop/0,set_item/2,get_item/1,get/1,length(0,clean/0). - -start() -> - spawn_link(fun() -> loop([]) end). - -stop() -> - self() ! {stop} - -loop(Touples) -> - receive -> - {set_item, Key, Value} -> - Touple = find(Touples, Key) - {stop} -> - {'EXIT', normal} - -find([], _) -> false; -find([{Key, _}|Tuples], Match) -> - case Key == Match of - true -> true; - false -> find(Tuples, Match) - end. \ No newline at end of file From 8c1fe0b4fdd4f380edf0d7be0dd479bbc10a819f Mon Sep 17 00:00:00 2001 From: Kallfaktorn Date: Sun, 13 Feb 2011 16:44:11 +0100 Subject: [PATCH 5/5] Port to process. --- src/ggs_server.erl | 4 ++-- src/js_runner.erl | 32 +++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/ggs_server.erl b/src/ggs_server.erl index 68b1625..91ea620 100644 --- a/src/ggs_server.erl +++ b/src/ggs_server.erl @@ -106,13 +106,13 @@ handle_cast({call, Token, Payload}, State) -> 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, []),%Payload, []), + {ok, Ret} = js_runner:call(JSVM, Payload, []), send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)), {noreply, State}; % 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, diff --git a/src/js_runner.erl b/src/js_runner.erl index ca866c4..780bfe1 100644 --- a/src/js_runner.erl +++ b/src/js_runner.erl @@ -1,13 +1,35 @@ -module(js_runner). --export([define/2,call/3, boot/0]). +-export([boot/0,define/2,call/3]). -boot() -> +%Mattias +boot() -> erlang_js:start(), {ok, Port} = js_driver:new(), + PortPid = spawn(fun() -> + port_process(Port) end ), + register(port_pid, PortPid), Port. -define(Port, Data) -> - ok = js:define(Port, list_to_binary(Data)). + +port_process(Port) -> +receive + {get_port, From} -> + From!{ok,Port}, + port_process(Port); + {define,From, JSVM, Data} -> + ok = js:define(JSVM, list_to_binary(Data)), + From!{ok}, + port_process(Port); + {call, From, JSVM, Func, Params} -> + {ok,Ret} = js:call(JSVM, list_to_binary(Func), Params), + From!{ok,Ret}, + port_process(Port) +end. + +define(Port, Data) -> + port_pid!{define,self(),Port,Data}. + + call(Port, Func, Params) -> - js:call(Port, list_to_binary(Func), Params). + port_pid!{call, self(), Port, Func, Params}.