Merge branch 'master' of github.com:jeena/GGS
This commit is contained in:
commit
872270405f
11 changed files with 88 additions and 104 deletions
2
HOWTO
2
HOWTO
|
@ -3,7 +3,7 @@ python version 2.x set to default.
|
||||||
|
|
||||||
INSTALL
|
INSTALL
|
||||||
1. Cd into directory where you to have the project
|
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/
|
3. cd GGS/
|
||||||
4. git submodule init
|
4. git submodule init
|
||||||
5. git submodule update
|
5. git submodule update
|
||||||
|
|
2
build
2
build
|
@ -3,4 +3,4 @@
|
||||||
for i in `find src -name "*.erl"`
|
for i in `find src -name "*.erl"`
|
||||||
do
|
do
|
||||||
erlc -o ebin $i
|
erlc -o ebin $i
|
||||||
done
|
done
|
17
client
Normal file
17
client
Normal 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
|
|
@ -12,8 +12,16 @@
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
GameServer.addGame(game_name, main());
|
GameServer.addGame(game_name, main());
|
||||||
GameServer.addClient(game_name, new TicTacToeClient(frames.player1.document.getElementById("p1"), GameServer));
|
GameServer.addClient(
|
||||||
GameServer.addClient(game_name, new TicTacToeClient(frames.player2.document.getElementById("p2"), GameServer));
|
game_name,
|
||||||
|
new TicTacToeClient(frames.player1.document.getElementById("p1"),
|
||||||
|
GameServer
|
||||||
|
));
|
||||||
|
GameServer.addClient(
|
||||||
|
game_name,
|
||||||
|
new TicTacToeClient(frames.player2.document.getElementById("p2"),
|
||||||
|
GameServer
|
||||||
|
));
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen">
|
<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen">
|
||||||
|
|
0
games/tic-tac-toe-js/proxy.rb
Normal file
0
games/tic-tac-toe-js/proxy.rb
Normal file
|
@ -31,7 +31,7 @@ Command: define\n\
|
||||||
Content-Type: text\n\
|
Content-Type: text\n\
|
||||||
Content-Length: 49\n\
|
Content-Length: 49\n\
|
||||||
\n\
|
\n\
|
||||||
function myFun() {return callErlang('a', 'b') ;}" % token)
|
function myFun() {return 'Hello World!' ;}" % token)
|
||||||
fs = s.makefile()
|
fs = s.makefile()
|
||||||
data = fs.readline()
|
data = fs.readline()
|
||||||
print "Token:", token
|
print "Token:", token
|
||||||
|
|
|
@ -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).
|
-module(ggs_protocol).
|
||||||
-export([parse/1]).
|
-export([parse/1]).
|
||||||
|
|
||||||
|
@ -39,31 +36,4 @@ parse(Data) ->
|
||||||
not_found
|
not_found
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
Processed.
|
gen_server:cast(ggs_server, 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.
|
|
||||||
|
|
|
@ -11,7 +11,6 @@
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/1,
|
-export([start_link/1,
|
||||||
start_link/0,
|
start_link/0,
|
||||||
get_count/0,
|
|
||||||
stop/0
|
stop/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
@ -39,16 +38,6 @@ start_link(Port) ->
|
||||||
start_link() ->
|
start_link() ->
|
||||||
start_link(?DEFAULT_PORT).
|
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.
|
%% @doc Stops the server.
|
||||||
%% @spec stop() -> ok
|
%% @spec stop() -> ok
|
||||||
|
@ -73,21 +62,14 @@ init([Port]) ->
|
||||||
{ok, State#state{lsock = LSock}, 0}
|
{ok, State#state{lsock = LSock}, 0}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
handle_call(get_count, _From, State) ->
|
|
||||||
{reply, {ok, State#state.client_vm_map}, State};
|
|
||||||
|
|
||||||
handle_call({backup_state, OldState}, _From, State) ->
|
handle_call({backup_state, OldState}, _From, State) ->
|
||||||
io:format("Received old state from backup~n"),
|
io:format("Received old state from backup~n"),
|
||||||
{noreply, OldState}.
|
{noreply, OldState}.
|
||||||
|
|
||||||
handle_cast(stop, State) ->
|
|
||||||
{stop, normal, State}.
|
|
||||||
|
|
||||||
handle_info({tcp, Socket, RawData}, State) ->
|
handle_info({tcp, Socket, RawData}, State) ->
|
||||||
NewState = do_JSCall(Socket, RawData, State),
|
ggs_protocol:parse(RawData),
|
||||||
OldMap = State#state.client_vm_map,
|
{noreply, State#state{lsock = Socket}};
|
||||||
io:format("Old map: ~p NewState: ~p~n", [OldMap, NewState]),
|
|
||||||
{noreply, State#state{client_vm_map = OldMap ++ [NewState]}};
|
|
||||||
|
|
||||||
handle_info({tcp_closed, Socket}, State) ->
|
handle_info({tcp_closed, Socket}, State) ->
|
||||||
gen_tcp:close(Socket),
|
gen_tcp:close(Socket),
|
||||||
|
@ -109,49 +91,34 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
|
handle_cast(stop, State) ->
|
||||||
|
{stop, normal, State};
|
||||||
|
|
||||||
do_JSCall(Socket, Data, State) ->
|
% Handle javascript defines
|
||||||
Parsed = ggs_protocol:parse(Data),
|
handle_cast({define, Token, Payload}, State) ->
|
||||||
NewState = case Parsed of
|
JSVM = getJSVM(Token, State),
|
||||||
{define, Token, Payload} ->
|
js_runner:define(JSVM, Payload),
|
||||||
JSVM = getJSVM(Token, State),
|
send(State#state.lsock, Token, "Okay, defined that for you!"),
|
||||||
js_runner:define(JSVM, Payload),
|
{noreply, State};
|
||||||
send(Socket, Token, "Okay, defined that for you!"),
|
|
||||||
[];
|
% Handle javascript calls
|
||||||
{call, Token, Payload} ->
|
handle_cast({call, Token, Payload}, State) ->
|
||||||
io:format("Got call request: ~p~n", [Payload]),
|
io:format("Got call request: ~p~n", [Payload]),
|
||||||
JSVM = getJSVM(Token, State),
|
JSVM = getJSVM(Token, State),
|
||||||
erlang:display(erlang:port_info(JSVM)),
|
erlang:display(erlang:port_info(JSVM)),
|
||||||
{ok, Ret} = js_runner:call(JSVM, Payload, []),%Payload, []),
|
{ok, Ret} = js_runner:call(JSVM, Payload, []),
|
||||||
send(Socket, Token, "JS says:", binary_to_list(Ret));
|
send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)),
|
||||||
|
{noreply, State};
|
||||||
|
|
||||||
% Set the new state to the reference generated, and JSVM associated
|
% Set the new state to the reference generated, and JSVM associated
|
||||||
{hello, _, _} ->
|
handle_cast({hello, _, _}, State) ->
|
||||||
JSVM = js_runner:boot(),
|
JSVM = js_runner:boot(),
|
||||||
Client = getRef(),
|
Client = getRef(),
|
||||||
send(Socket, Client, "This is your refID"),
|
send(State#state.lsock, Client, "This is your refID"),
|
||||||
|
OldMap = State#state.client_vm_map,
|
||||||
%% This shouldnt be here...
|
NewState = State#state{client_vm_map = OldMap ++ [{Client, JSVM}]},
|
||||||
OldMap = State#state.client_vm_map,
|
gen_server:cast(ggs_backup, {set_backup, NewState}),
|
||||||
BackupState = State#state{client_vm_map = OldMap ++ [{Client, JSVM}]},
|
{noreply, NewState}.
|
||||||
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.
|
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
%% Helpers
|
%% Helpers
|
||||||
%%-----------------------------------------------------
|
%%-----------------------------------------------------
|
||||||
|
|
|
@ -1,13 +1,35 @@
|
||||||
-module(js_runner).
|
-module(js_runner).
|
||||||
-export([define/2,call/3, boot/0]).
|
-export([boot/0,define/2,call/3]).
|
||||||
|
|
||||||
boot() ->
|
%Mattias
|
||||||
|
boot() ->
|
||||||
erlang_js:start(),
|
erlang_js:start(),
|
||||||
{ok, Port} = js_driver:new(),
|
{ok, Port} = js_driver:new(),
|
||||||
|
PortPid = spawn(fun() ->
|
||||||
|
port_process(Port) end ),
|
||||||
|
register(port_pid, PortPid),
|
||||||
Port.
|
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) ->
|
call(Port, Func, Params) ->
|
||||||
js:call(Port, list_to_binary(Func), Params).
|
port_pid!{call, self(), Port, Func, Params}.
|
||||||
|
|
2
start
2
start
|
@ -1,3 +1,3 @@
|
||||||
#!/usr/bin/env bash
|
#!/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
|
||||||
|
|
Reference in a new issue