diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2010-10-02 03:23:17 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2010-10-02 03:23:17 +0200 |
commit | 2b99ed6fd9cf84c405f426ea0914b233b1f40ef1 (patch) | |
tree | afa943f5058179204c855f42f14021be5d0816fb /src/core | |
parent | 15725caf30a564d33868bf2dccb2c98ea5eb34eb (diff) | |
download | ephraim-2b99ed6fd9cf84c405f426ea0914b233b1f40ef1.tar ephraim-2b99ed6fd9cf84c405f426ea0914b233b1f40ef1.zip |
Replaced ephraim_config code by gen_server behaviour
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/ephraim.erl | 7 | ||||
-rw-r--r-- | src/core/ephraim_config.erl | 59 |
2 files changed, 32 insertions, 34 deletions
diff --git a/src/core/ephraim.erl b/src/core/ephraim.erl index c59be72..61387a5 100644 --- a/src/core/ephraim.erl +++ b/src/core/ephraim.erl @@ -24,12 +24,7 @@ stop() -> init() -> register(ephraim, self()), - Config = spawn(ephraim_config, init, []), - % Wait for config process - receive - {Config, started} -> - io:format("Reading config file...~n") - end, + ephraim_config:start_link(), GUI = spawn(ephraim_gui, init, []), Conn = spawn(ephraim_conn, init, []), diff --git a/src/core/ephraim_config.erl b/src/core/ephraim_config.erl index 6c49351..6c556c2 100644 --- a/src/core/ephraim_config.erl +++ b/src/core/ephraim_config.erl @@ -1,15 +1,20 @@ -module(ephraim_config). --compile([debug_info, export_all]). +-behaviour(gen_server). --spec init() -> ok. -init() -> - register(ephraim_config, self()), - ephraim ! {self(), started}, - +-export([start_link/0]). +-export([get/1]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). + +-spec start_link() -> {ok, pid()}. +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +-spec init(term()) -> {ok, dict()}. +init(_Args) -> {ok, Terms} = file:consult(config_path() ++ "/ephraim.cfg"), Config = dict:from_list(lists:map(fun(Term) -> {element(1, Term), Term} end, Terms)), - loop(Config). + {ok, Config}. -spec config_path() -> string(). @@ -28,28 +33,26 @@ config_path() -> end, Path. - -spec get(atom()) -> tuple() | error. get(Key) -> + gen_server:call(?MODULE, {get, Key}). + + +-spec handle_call(term(), {pid(), term()}, dict()) -> {reply, tuple() | error, dict()}. +handle_call({get, Key}, _From, Config) -> ephraim_config ! {self(), get, Key}, - receive - {config, ok, Key, Value} -> - Value; - {config, error, Key} -> - error - end. - --spec loop(dict()) -> ok. -loop(Config) -> - receive - stop -> - ok; - {From, get, Key} -> - case dict:find(Key, Config) of - {ok, Value} -> - From ! {config, ok, Key, Value}; - error -> - From ! {config, error, Key} - end, - loop(Config) + case dict:find(Key, Config) of + {ok, Value} -> + {reply, Value, Config}; + error -> + {reply, error, Config} end. + +handle_cast(_Msg, Config) -> + {noreply, Config}. +handle_info(_Msg, Config) -> + {noreply, Config}. +terminate(_Reason, _Config) -> + ok. +code_change(_OldVersion, Config, _Extra) -> + {ok, Config}. |