Introduce egs_conf for system-wide configuration. Removes network.hrl.

This commit is contained in:
Loïc Hoguin 2010-10-07 00:06:02 +02:00
parent a083357e18
commit 34ff98e43e
5 changed files with 108 additions and 27 deletions

View File

@ -6,6 +6,7 @@
egs, egs,
egs_app, egs_app,
egs_sup, egs_sup,
egs_conf,
egs_game_server, egs_game_server,
egs_login_server, egs_login_server,
egs_exit_mon, egs_exit_mon,

View File

@ -1,7 +1,3 @@
%% @author Loïc Hoguin <essen@dev-extend.eu>
%% @copyright 2010 Loïc Hoguin.
%% @doc Network-related settings.
%%
%% This file is part of EGS. %% This file is part of EGS.
%% %%
%% EGS is free software: you can redistribute it and/or modify %% EGS is free software: you can redistribute it and/or modify
@ -17,15 +13,18 @@
%% You should have received a copy of the GNU Affero General Public License %% You should have received a copy of the GNU Affero General Public License
%% along with EGS. If not, see <http://www.gnu.org/licenses/>. %% along with EGS. If not, see <http://www.gnu.org/licenses/>.
%% EGS network settings. %% Network configuration.
%% Note that changing these options generally requires restarting the server.
-define(PATCH_PORT_JP, 11030). %% @doc Ports to listen to for the patch server.
-define(PATCH_PORT_US, 11230). %% 11030 is the JP port; 11230 is the US port.
{patch_ports, [11030, 11230]}.
-define(LOGIN_PORT_JP_ONE, 12030). %% @doc Ports to listen to for the login server.
-define(LOGIN_PORT_JP_TWO, 12031). %% 12030 and 12031 are the JP ports, one per entrance; 12230 is the US port.
-define(LOGIN_PORT_US, 12230). {login_ports, [12030, 12031, 12230]}.
-define(GAME_IP, << 188, 165, 56, 119 >>). %% @doc Game server IP address and port.
-define(GAME_PORT, 12061). %% They can be modified freely without problem.
-define(GAME_LISTEN_OPTIONS, [binary, {active, false}, {certfile, "priv/ssl/servercert.pem"}, {keyfile, "priv/ssl/serverkey.pem"}, {password, "alpha"}]). %% Note that the port should be available and above 1024.
{game_server, {<< 127, 0, 0, 1 >>, 12061}}.

81
src/egs_conf.erl Normal file
View File

@ -0,0 +1,81 @@
%% @author Loïc Hoguin <essen@dev-extend.eu>
%% @copyright 2010 Loïc Hoguin.
%% @doc EGS configuration gen_server.
%%
%% This file is part of EGS.
%%
%% EGS is free software: you can redistribute it and/or modify
%% it under the terms of the GNU Affero General Public License as
%% published by the Free Software Foundation, either version 3 of the
%% License, or (at your option) any later version.
%%
%% EGS is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%% GNU Affero General Public License for more details.
%%
%% You should have received a copy of the GNU Affero General Public License
%% along with EGS. If not, see <http://www.gnu.org/licenses/>.
-module(egs_conf).
-behavior(gen_server).
-export([start_link/0, stop/0, read/1, reload/0]). %% API.
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% gen_server.
%% Use the module name for the server's name.
-define(SERVER, ?MODULE).
%% API.
%% @spec start_link() -> {ok,Pid::pid()}
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%% @spec stop() -> stopped
stop() ->
gen_server:call(?SERVER, stop).
%% @spec read(Key) -> Value | undefined
read(Key) ->
gen_server:call(?SERVER, {read, Key}).
%% @spec reload() -> ok
reload() ->
gen_server:cast(?SERVER, reload).
%% gen_server.
init([]) ->
case file:consult("priv/egs.conf") of
{ok, Terms} ->
error_logger:info_report("egs_conf started"),
{ok, Terms};
Error ->
error_logger:error_report(["An error occurred when trying to load the configuration file:", Error]),
Error
end.
handle_call({read, Key}, _From, State) ->
{reply, proplists:get_value(Key, State), State};
handle_call(stop, _From, State) ->
{stop, normal, stopped, State};
handle_call(_Request, _From, State) ->
{reply, ignored, State}.
handle_cast(reload, _State) ->
{ok, Terms} = file:consult("priv/egs.conf"),
{noreply, Terms};
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

View File

@ -21,7 +21,6 @@
-export([keepalive/1, info/2, cast/3, raw/3, event/2]). -export([keepalive/1, info/2, cast/3, raw/3, event/2]).
-include("include/records.hrl"). -include("include/records.hrl").
-include("include/network.hrl").
%% @doc Don't keep alive here, authentication should go fast. %% @doc Don't keep alive here, authentication should go fast.
keepalive(_State) -> keepalive(_State) ->
@ -56,9 +55,9 @@ event({system_client_version_info, _Entrance, _Language, _Platform, Version}, St
end; end;
%% Game server info request handler. %% Game server info request handler.
%% @todo Remove the dependency on network.hrl through configuration files.
event(system_game_server_request, State=#state{socket=Socket}) -> event(system_game_server_request, State=#state{socket=Socket}) ->
psu_proto:send_0216(?GAME_IP, ?GAME_PORT, State), {ServerIP, ServerPort} = egs_conf:read(game_server),
psu_proto:send_0216(ServerIP, ServerPort, State),
ssl:close(Socket), ssl:close(Socket),
closed; closed;

View File

@ -22,8 +22,6 @@
-export([init/1]). %% Supervisor callbacks. -export([init/1]). %% Supervisor callbacks.
-export([start_link/0, upgrade/0]). %% Other functions. -export([start_link/0, upgrade/0]). %% Other functions.
-include("include/network.hrl").
%% @spec start_link() -> ServerRet %% @spec start_link() -> ServerRet
%% @doc API for starting the supervisor. %% @doc API for starting the supervisor.
start_link() -> start_link() ->
@ -47,13 +45,16 @@ upgrade() ->
%% @spec init([]) -> SupervisorTree %% @spec init([]) -> SupervisorTree
%% @doc supervisor callback. %% @doc supervisor callback.
%% @todo Probably link egs_conf to a supervisor too.
init([]) -> init([]) ->
%% Start egs_cron, egs_game, egs_login, egs_patch. To be replaced by configurable modules. egs_conf:start_link(),
Processes = [{egs_user_model, {egs_user_model, start_link, []}, permanent, 5000, worker, dynamic}, PatchPorts = egs_conf:read(patch_ports),
{egs_game_server, {egs_game_server, start_link, [?GAME_PORT]}, permanent, 5000, worker, dynamic}, LoginPorts = egs_conf:read(login_ports),
{egs_login_server_jp1, {egs_login_server, start_link, [?LOGIN_PORT_JP_ONE]}, permanent, 5000, worker, dynamic}, {_ServerIP, GamePort} = egs_conf:read(game_server),
{egs_login_server_jp2, {egs_login_server, start_link, [?LOGIN_PORT_JP_TWO]}, permanent, 5000, worker, dynamic}, PatchProcs = [{{egs_patch_server, Port}, {psu_patch, start_link, [Port]}, permanent, 5000, worker, dynamic} || Port <- PatchPorts],
{egs_login_server_us, {egs_login_server, start_link, [?LOGIN_PORT_US]}, permanent, 5000, worker, dynamic}, LoginProcs = [{{egs_login_server, Port}, {egs_login_server, start_link, [Port]}, permanent, 5000, worker, dynamic} || Port <- LoginPorts],
{psu_patch_jp, {psu_patch, start_link, [?PATCH_PORT_JP]}, permanent, 5000, worker, dynamic}, OtherProcs = [
{psu_patch_us, {psu_patch, start_link, [?PATCH_PORT_US]}, permanent, 5000, worker, dynamic}], {egs_user_model, {egs_user_model, start_link, []}, permanent, 5000, worker, dynamic},
{ok, {{one_for_one, 10, 10}, Processes}}. {egs_game_server, {egs_game_server, start_link, [GamePort]}, permanent, 5000, worker, dynamic}
],
{ok, {{one_for_one, 10, 10}, PatchProcs ++ LoginProcs ++ OtherProcs}}.