Permalink
Browse files

Make it a real OTP application

There's an OTP application that you can start now. It expects the
-conf argument to be pointing to a config file.

Look at settings-app.cfg.sample for an example config file. It defines
parameters for several connections to IRC.
  • Loading branch information...
1 parent 9fa329e commit f738ba920ceffe24c1ac40cce835856aabb62a90 @gdamjan committed Oct 5, 2011
Showing with 83 additions and 9 deletions.
  1. +19 −6 README.md
  2. +14 −0 settings-app.cfg.sample
  3. +3 −2 src/ircbot.app.src
  4. +46 −0 src/ircbot_app.erl
  5. +1 −1 src/ircbot_fsm.erl
View
@@ -61,8 +61,9 @@ will handle all the details to run the new code without even disconnecting.
[code switching]: http://en.wikipedia.org/wiki/Erlang_%28programming_language%29#Hot_code_loading_and_modules
-Experimental OOP API (*)
-------------------------
+
+Parametrized Module API
+-----------------------
Using the parametrized module support in Erlang we can do something like this
too:
@@ -72,10 +73,22 @@ too:
IrcBot:connect().
IrcBot:add_plugin(plugins.rps, []).
-I'm yet to decide if it's smart to do this, since this feature is considered
-experimental in Erlang. (*) The parametrized module feature is becoming
-officially supported in Erlang R14, so I'm deciding that it will be the official
-API of this bot.
+
+The parametrized module feature is officially supported since Erlang R14, so
+now I consider this the official API of the bot.
+
+
+Real OTP Application
+--------------------
+
+To start it:
+
+ erl -sname ircbot@localhost -setcookie xxx -pa ebin/ \
+ -s ircbot_app -conf settings-app.cfg
+
+To connect a remote shell to it:
+
+ erl -sname ctrl -remsh ircbot@localhost -setcookie xxx
Similar projects
View
@@ -0,0 +1,14 @@
+%%% Be carefull with the syntax of this file. The contents must be proper
+%%% Erlang terms. See http://www.erlang.org/doc/man/file.html#consult-1
+
+{connection, [
+ {nickname, "erlbot_test"},
+ {server, {"chat.freenode.net", 6667}},
+ {channels, ["#erlbot_test"]}
+]}.
+
+{connection, [
+ {nickname, "erlbot_test"},
+ {server, {"irc.oftc.net", 6667}},
+ {channels, ["#erlbot_test"]}
+]}.
View
@@ -3,8 +3,9 @@
{vsn, "0.01"},
{modules, [
ircbot_app, ircbot_fsm, ircbot_api,
- ircbot_lib, pong_plugin, ctcp_plugin
+ ircbot_lib, ircbot_plugins,
+ pong_plugin, ctcp_plugin
]},
{mod, {ircbot_app, []}},
- {applications, [kernel, stdlib]}]
+ {applications, [kernel, stdlib, sasl]}]
}.
View
@@ -0,0 +1,46 @@
+-module(ircbot_app).
+-author('gdamjan@gmail.com').
+
+-behaviour(application).
+-behaviour(supervisor).
+
+%% Application and Supervisor callbacks
+-export([start/2, stop/1, init/1]).
+
+%% API
+-export([start/0]).
+
+-define(SUPERVISOR, ?MODULE).
+-define(CHILD(I), {I, {I, start_link, []}, temporary, 5000, worker, [I]}).
+
+start() ->
+ application:start(sasl),
+ application:start(ircbot).
+
+
+%% app behaviour
+start(_Type, _StartArgs) ->
+ {ok, Sup} = supervisor:start_link({local, ?SUPERVISOR}, ?MODULE, []),
+ {ok, SettingsFile} = init:get_argument(conf),
+ {ok, Settings} = file:consult(SettingsFile),
+ connect_all(Sup, Settings),
+ {ok, Sup}.
+
+stop(_State) ->
+ exit(whereis(?SUPERVISOR), shutdown).
+
+
+%% supervisor behaviour
+init([]) ->
+ {ok, {{simple_one_for_one, 10, 60}, [?CHILD(ircbot_fsm)]}}.
+
+
+%% helper
+connect_all(Sup, Settings) ->
+ lists:foreach(
+ fun ({connection, Args}) ->
+ {ok, Child} = supervisor:start_child(Sup, [Args]),
+ gen_fsm:send_event(Child, connect)
+ end,
+ Settings
+ ).
View
@@ -23,7 +23,7 @@
}).
-% api to use in the shell
+%% api to use in the shell
new(Settings) ->
{ok, Ref} = start(Settings),
ircbot_api:new(Ref).

0 comments on commit f738ba9

Please sign in to comment.