Initial directory struct + OTP stuffs
This commit is contained in:
parent
a44a538ac6
commit
c88dcf7829
13 changed files with 160 additions and 0 deletions
BIN
ebin/.GGS.app.swp
Normal file
BIN
ebin/.GGS.app.swp
Normal file
Binary file not shown.
13
ebin/GGS.app
Normal file
13
ebin/GGS.app
Normal file
|
@ -0,0 +1,13 @@
|
|||
{application, ggs,
|
||||
[{description, "The Generic Game Server"},
|
||||
{vsn, "0.1.0"},
|
||||
{modules, [
|
||||
ggs_app,
|
||||
ggs_sup,
|
||||
ggs_server
|
||||
]},
|
||||
{registered, [ggs_sup]},
|
||||
{applications, [kernel, stdlib]},
|
||||
{mod, {ggs_app, []}}
|
||||
]}.
|
||||
|
1
erlang_js
Submodule
1
erlang_js
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 5350ed21606606dbee5ecb07e974f2abb9106270
|
7
js_runner.erl
Normal file
7
js_runner.erl
Normal file
|
@ -0,0 +1,7 @@
|
|||
-module(js_runner).
|
||||
-export([run/0]).
|
||||
boot() ->
|
||||
erlang_js:start(),
|
||||
{ok, Port} = js_driver:new(),
|
||||
ok = js:define(Port, <<"function helloworld(name){return 'Hello, ' + name}">>),
|
||||
js:call(Port, <<"helloworld">>, [<<"Generic Game Server">>]).
|
BIN
js_test.beam
Normal file
BIN
js_test.beam
Normal file
Binary file not shown.
7
js_test.erl
Normal file
7
js_test.erl
Normal file
|
@ -0,0 +1,7 @@
|
|||
-module(js_test).
|
||||
-export([run/0]).
|
||||
run() ->
|
||||
erlang_js:start(),
|
||||
{ok, Port} = js_driver:new(),
|
||||
ok = js:define(Port, <<"function helloworld(name){return 'Hello, ' + name}">>),
|
||||
js:call(Port, <<"helloworld">>, [<<"Generic Game Server">>]).
|
BIN
src/.ggs_app.erl.swp
Normal file
BIN
src/.ggs_app.erl.swp
Normal file
Binary file not shown.
BIN
src/.ggs_server.erl.swp
Normal file
BIN
src/.ggs_server.erl.swp
Normal file
Binary file not shown.
BIN
src/.ggs_sup.erl.swp
Normal file
BIN
src/.ggs_sup.erl.swp
Normal file
Binary file not shown.
14
src/ggs_app.erl
Normal file
14
src/ggs_app.erl
Normal file
|
@ -0,0 +1,14 @@
|
|||
-module(ggs_app).
|
||||
-behaviour(application).
|
||||
-export([start/2, stop/1]).
|
||||
|
||||
start(_StartType, _StartArgs) ->
|
||||
case ggs_sup:start() of
|
||||
{ok, Pid} ->
|
||||
{ok, Pid};
|
||||
Other ->
|
||||
{error, Other}
|
||||
end.
|
||||
|
||||
stop(_State) ->
|
||||
ok.
|
BIN
src/ggs_server.beam
Normal file
BIN
src/ggs_server.beam
Normal file
Binary file not shown.
98
src/ggs_server.erl
Normal file
98
src/ggs_server.erl
Normal file
|
@ -0,0 +1,98 @@
|
|||
%%%----------------------------------------------------
|
||||
%%% @author Jonatan Pålsson <Jonatan.p@gmail.com>
|
||||
%%% @copyright 2010 Jonatan Pålsson
|
||||
%%% @doc RPC over TCP server
|
||||
%%% @end
|
||||
%%%----------------------------------------------------
|
||||
|
||||
-module(ggs_server).
|
||||
-behaviour(gen_server).
|
||||
|
||||
%% API
|
||||
-export([start_link/1,
|
||||
start_link/0,
|
||||
get_count/0,
|
||||
stop/0
|
||||
]).
|
||||
|
||||
%% gen_server callbacks
|
||||
-export([init/1, handle_call/3, handle_cast/2,
|
||||
handle_info/2, terminate/2, code_change/3]).
|
||||
|
||||
|
||||
-define(SERVER, ?MODULE).
|
||||
-define(DEFAULT_PORT, 1055).
|
||||
|
||||
-record(state, {port, lsock, request_count = 0}).
|
||||
|
||||
%%%====================================================
|
||||
%%% API
|
||||
%%%====================================================
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% @doc Starts the server
|
||||
%% @end
|
||||
%%-----------------------------------------------------
|
||||
start_link(Port) ->
|
||||
gen_server:start_link({local, ?SERVER}, ?MODULE, [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
|
||||
%% @end
|
||||
%%-----------------------------------------------------
|
||||
stop() ->
|
||||
gen_server:cast(?SERVER, stop).
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% gen_server callbacks
|
||||
%%-----------------------------------------------------
|
||||
|
||||
init([Port]) ->
|
||||
{ok, LSock} = gen_tcp:listen(Port, [{active, true}]),
|
||||
{ok, #state{port = Port, lsock = LSock}, 0}.
|
||||
|
||||
handle_call(get_count, _From, State) ->
|
||||
{reply, {ok, State#state.request_count}, State}.
|
||||
|
||||
handle_cast(stop, State) ->
|
||||
{stop, normal, State}.
|
||||
|
||||
handle_info({tcp, Socket, RawData}, State) ->
|
||||
do_JSCall(Socket, RawData),
|
||||
RequestCount = State#state.request_count,
|
||||
{noreply, State#state{request_count = RequestCount + 1}};
|
||||
handle_info(timeout, #state{lsock = LSock} = State) ->
|
||||
{ok, _Sock} = gen_tcp:accept(LSock),
|
||||
{noreply, State}.
|
||||
|
||||
terminate(_Reason, _State) ->
|
||||
ok.
|
||||
|
||||
code_change(_OldVsn, State, _Extra) ->
|
||||
{ok, State}.
|
||||
|
||||
%%-----------------------------------------------------
|
||||
%% Internal functions
|
||||
%%-----------------------------------------------------
|
||||
|
||||
do_JSCall(Socket, Data) ->
|
||||
gen_tcp:send(Socket, io_lib:fwrite("~p~n", ["I got a call from j00!"])).
|
||||
|
||||
args_to_terms(RawArgs) ->
|
||||
{ok, Toks, _Line} = erl_scan:string("[" ++ RawArgs ++ "]. ", 1),
|
||||
{ok, Args} = erl_parse:parse_term(Toks),
|
||||
Args.
|
20
src/ggs_sup.erl
Normal file
20
src/ggs_sup.erl
Normal file
|
@ -0,0 +1,20 @@
|
|||
-module(ggs_sup).
|
||||
-behaviour(supervisor).
|
||||
|
||||
%% API
|
||||
-export([start_link/0]).
|
||||
|
||||
%% Supervisor callbacks
|
||||
-export([init/1]).
|
||||
-define(SERVER, ?MODULE).
|
||||
|
||||
start_link() ->
|
||||
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
|
||||
|
||||
init([]) ->
|
||||
Server = {ggs_server, {ggs_server, start_link, []},
|
||||
permanent, 2000, worker, [ggs_server]},
|
||||
Children = [Server],
|
||||
RestartStrategy = {one_for_one, 0, 1},
|
||||
{ok, {RestartStrategy, Children}}.
|
||||
|
Reference in a new issue