Permalink
Browse files

Tidied up configuration records inside config_server and changed how …

…irc-networks are used
  • Loading branch information...
1 parent 1509556 commit dfcbc7a851454c93e69e7753f2dd3bbf7cf19ca0 @wrboyce committed Aug 22, 2011
Showing with 66 additions and 43 deletions.
  1. +0 −13 include/erb.hrl
  2. +62 −26 src/core/erb_config_server.erl
  3. +4 −4 src/core/erb_connector.erl
View
@@ -15,19 +15,6 @@
router=undefined,
dispatcher=undefined}).
-%% @doc Bot configuration
--record(bot_config, {
- id,
- network,
- nick,
- chans,
- enabled}).
-
-%% @doc IRC Network
--record(network, {
- id,
- servers}).
-
%% @doc Data record representing a line from the server
-record(data, {
bot,
@@ -22,7 +22,26 @@
%% Records
-record(state, {}).
--record(config, {service, config, bot}).
+%% mnesia Records
+%% @doc Bot configuration
+-record(bot_config, {
+ id,
+ network,
+ nick,
+ chans,
+ enabled}).
+%% @doc IRC Server
+-record(server, {
+ network,
+ host,
+ port,
+ enabled}).
+%% @doc generic service config
+-record(config, {
+ bot,
+ service,
+ config,
+ enabled}).
%% ===================================================================
%% API
@@ -50,7 +69,7 @@ init([]) ->
mnesia:create_schema([node()]),
mnesia:create_table(bot_config, [{type, set}, {disc_copies, [node()]}, {attributes, record_info(fields, bot_config)}]),
mnesia:create_table(config, [{type, set}, {disc_copies, [node()]}, {attributes, record_info(fields, config)}]),
- mnesia:create_table(network, [{type, set}, {disc_copies, [node()]}, {attributes, record_info(fields, network)}]),
+ mnesia:create_table(server, [{type, set}, {disc_copies, [node()]}, {attributes, record_info(fields, server)}]),
{ok, #state{}}.
%% -------------------------------------------------------------------
@@ -62,6 +81,35 @@ init([]) ->
%% {stop, Reason, State}
%% @doc Handling call messages
%% -------------------------------------------------------------------
+handle_call({addBot, {Nick, Network, Chans}}, _From, State) ->
+ BotConfig = #bot_config{ nick=Nick, network=Network, chans=Chans, enabled=true },
+ error_logger:info_msg("Creating bot ~p~n", [BotConfig]),
+ {atomic, ok} = mnesia:transaction(fun() ->
+ mnesia:write(BotConfig)
+ end),
+ Reply = {ok, bot_from_config(BotConfig)},
+ {reply, Reply, State};
+
+handle_call(getBots, _From, State) ->
+ Q = qlc:q([
+ C || C <- mnesia:table(bot_config),
+ C#bot_config.enabled =:= true]),
+ {atomic, BotConfigs} = mnesia:transaction(fun() -> qlc:e(Q) end),
+ Bots = lists:map(fun(C) -> bot_from_config(C) end, BotConfigs),
+ {reply, {ok, Bots}, State};
+
+handle_call({getServers, NetworkId}, _From, State) ->
+ Q = qlc:q([{S#server.host, S#server.port} || S <- mnesia:table(server),
+ S#server.network =:= NetworkId]),
+ {atomic, Result} = mnesia:transaction(fun() -> qlc:e(Q) end),
+ Reply = case Result of
+ [] ->
+ {error, notfound};
+ _ ->
+ {ok, Result}
+ end,
+ {reply, Reply, State};
+
handle_call({getConfig, Bot, Service}, _From, State) ->
error_logger:info_msg("Retrieving configuration: ~p", [Service]),
Q = qlc:q([
@@ -80,30 +128,6 @@ handle_call({getConfig, Bot, Service}, _From, State) ->
end,
{reply, Reply, State};
-handle_call(getBots, _From, State) ->
- Q = qlc:q([
- #bot{
- id=C#bot_config.id,
- network=C#bot_config.network,
- nick=C#bot_config.nick,
- chans=C#bot_config.chans
- } || C <- mnesia:table(bot_config),
- C#bot_config.enabled =:= true]),
- {atomic, Bots} = mnesia:transaction(fun() -> qlc:e(Q) end),
- {reply, {ok, Bots}, State};
-
-handle_call({getNetwork, NetworkId}, _From, State) ->
- Q = qlc:q([N || N <- mnesia:table(network),
- N#network.id =:= NetworkId]),
- {atomic, Result} = mnesia:transaction(fun() -> qlc:e(Q) end),
- Reply = case Result of
- [Network] ->
- {ok, Network};
- [] ->
- {error, unknown_network}
- end,
- {reply, Reply, State};
-
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
@@ -114,6 +138,11 @@ handle_call(_Request, _From, State) ->
%% {stop, Reason, State}
%% @doc Handling cast messages
%% -------------------------------------------------------------------
+handle_cast({addServer, Network, {Host, Port}}, State) ->
+ {atomic, ok} = mnesia:transaction(fun() ->
+ mnesia:write(#server{ network=Network, host=Host, port=Port })
+ end),
+ {noreply, State};
handle_cast({putConfig, Bot, Service, Config}, State) ->
error_logger:info_msg("Setting configuration: ~p = ~p~n", [Service, Config]),
{atomic, ok} = mnesia:transaction(fun() ->
@@ -153,3 +182,10 @@ code_change(_OldVsn, State, _Extra) ->
%% ===================================================================
%% Internal functions
%% ===================================================================
+bot_from_config(BotConfig) ->
+ #bot{
+ id=BotConfig#bot_config.id,
+ network=BotConfig#bot_config.network,
+ nick=BotConfig#bot_config.nick,
+ chans=BotConfig#bot_config.chans
+ }.
@@ -109,14 +109,14 @@ code_change(_OldVsn, State, _Extra) ->
%% @doc Dispatch lines to the router
%% -------------------------------------------------------------------
open_connection(NetworkId) ->
- {ok, Network} = gen_server:call({global, erb_config_server}, {getNetwork, NetworkId}),
- [{Server, Port}|OtherServers] = Network#network.servers,
+ {ok, Servers} = gen_server:call({global, erb_config_server}, {getServers, NetworkId}),
+ [{Server, Port}|OtherServers] = Servers,
case gen_tcp:connect(Server, Port, [{packet, line}, {active, true}]) of
{ok, Sock} ->
error_logger:info_msg("~s:~B connected.~n", [Server, Port]),
- {Network#network.servers, Sock};
+ {Servers, Sock};
_ ->
- open_connection(Network#network{ servers = OtherServers })
+ open_connection(OtherServers)
end.
dispatch(_Processor, []) ->

0 comments on commit dfcbc7a

Please sign in to comment.