Merge branch 'master' of github.com:jeena/GGS

This commit is contained in:
Jeena Paradies 2011-02-14 21:37:08 +01:00
commit 872270405f
11 changed files with 88 additions and 104 deletions

2
HOWTO
View file

@ -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

2
build
View file

@ -3,4 +3,4 @@
for i in `find src -name "*.erl"`
do
erlc -o ebin $i
done
done

17
client Normal file
View file

@ -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

@ -1 +1 @@
Subproject commit cbac148c440a93db44bad767a43c6d8619f6871f
Subproject commit 5350ed21606606dbee5ecb07e974f2abb9106270

View file

@ -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
));
}
</script>
<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen">

View file

View file

@ -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

View file

@ -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).

View file

@ -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, []),
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
%%-----------------------------------------------------

View file

@ -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}.

2
start
View file

@ -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