From 11b93c3c17d0bf2b66bb67df18ca762e632c30cd Mon Sep 17 00:00:00 2001 From: Matt Mullins Date: Tue, 30 Aug 2011 16:14:12 -0500 Subject: [PATCH] Refactored the way configuration data is passed through the IRC application. Instead of passing configuration data all the way from the irc_sup, all that is passed is the instance name, and the irc_conn is responsible for getting the configuration data from the config module. --- irc/irc_conn.erl | 10 ++++++---- irc/irc_net_sup.erl | 13 +++++++------ irc/irc_sup.erl | 4 ++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/irc/irc_conn.erl b/irc/irc_conn.erl index 318838a..2108db6 100644 --- a/irc/irc_conn.erl +++ b/irc/irc_conn.erl @@ -3,7 +3,7 @@ -vsn(1). -export([ - start_link/2 + start_link/3 ]). -export([ @@ -24,11 +24,13 @@ buffer = "" % for the TCP session }). -start_link(Instance, Config) -> +start_link(Instance, Supervisor, TableId) -> gen_server:start_link(?MODULE, - {Instance, Config}, []). + {Instance, Supervisor, TableId}, []). -init({Instance, Config = {Host, Port, Nick, Realname, _Channels}}) -> +init({Instance, Supervisor, TableId}) -> + {Instance, Config} = lists:keyfind(Instance, 1, config:get_config(irc)), + {Host, Port, Nick, Realname, _Channels} = Config, {ok, Socket} = gen_tcp:connect(Host, Port, [list]), send_command(#irc_command{command = "NICK", middles = [Nick]}), send_command(#irc_command{command = "USER", diff --git a/irc/irc_net_sup.erl b/irc/irc_net_sup.erl index cd4be7a..f9f6e65 100644 --- a/irc/irc_net_sup.erl +++ b/irc/irc_net_sup.erl @@ -7,17 +7,18 @@ -behavior(supervisor). -export([ - start_link/2 + start_link/1 ]). -export([init/1]). -start_link(Instance, Config) -> - supervisor:start_link(?MODULE, - {Instance, Config}). +start_link(Instance) -> + supervisor:start_link(?MODULE, Instance). -init({Instance, Config}) -> +init(Instance) -> + Supervisor = self(), + TableId = ets:new(?MODULE, [set, public]), Connection = {connection, - {irc_conn, start_link, [Instance, Config]}, + {irc_conn, start_link, [Instance, Supervisor, TableId]}, permanent, % persistence type ("Restart" in the manual) 5, % shutdown [sends signal, with timeout for response] worker, % it's not a supervisor itself diff --git a/irc/irc_sup.erl b/irc/irc_sup.erl index 9ae2bd3..a4b4dd4 100644 --- a/irc/irc_sup.erl +++ b/irc/irc_sup.erl @@ -18,9 +18,9 @@ init(_) -> Restart = {one_for_one, 5, 60}, {ok, {Restart, Children}}. -config_to_childspec({Instance, Config}) -> +config_to_childspec({Instance, _Config}) -> {Instance, % child's name - {irc_net_sup, start_link, [Instance, Config]}, % MFA + {irc_net_sup, start_link, [Instance]}, % MFA permanent, % Restart 5, % shutdown [timeout of 5 seconds] supervisor, -- 2.11.0