Browse files

OTP-ify tinymq application.

  • Loading branch information...
1 parent e0013a3 commit 0b65c0072fde4253dcd6c85f2718c06d2293303b @robertoaloi robertoaloi committed Aug 31, 2012
Showing with 68 additions and 38 deletions.
  1. +1 −0 .gitignore
  2. +2 −1 src/tinymq.app.src
  3. +42 −0 src/tinymq_app.erl
  4. +9 −12 src/tinymq_channel_controller.erl
  5. +6 −10 src/tinymq_channel_sup.erl
  6. +5 −9 src/tinymq_controller.erl
  7. +3 −6 src/tinymq_sup.erl
View
1 .gitignore
@@ -0,0 +1 @@
+ebin/
View
3 src/tinymq.app.src
@@ -8,5 +8,6 @@
kernel,
stdlib
]},
- {env, []}
+ {env, [{max_age, 60}]},
+ {mod, {tinymq_app, []}}
]}.
View
42 src/tinymq_app.erl
@@ -0,0 +1,42 @@
+-module(tinymq_app).
+
+-behaviour(application).
+
+%% Application callbacks
+-export([start/2, stop/1]).
+
+%%%===================================================================
+%%% Application callbacks
+%%%===================================================================
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% This function is called whenever an application is started using
+%% application:start/[1,2], and should start the processes of the
+%% application. If the application is structured according to the OTP
+%% design principles as a supervision tree, this means starting the
+%% top supervisor of the tree.
+%%
+%% @spec start(StartType, StartArgs) -> {ok, Pid} |
+%% {ok, Pid, State} |
+%% {error, Reason}
+%% StartType = normal | {takeover, Node} | {failover, Node}
+%% StartArgs = term()
+%% @end
+%%--------------------------------------------------------------------
+start(_StartType, _StartArgs) ->
+ tinymq_sup:start_link().
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% This function is called whenever an application has stopped. It
+%% is intended to be the opposite of Module:start/2 and should do
+%% any necessary cleaning up. The return value is ignored.
+%%
+%% @spec stop(State) -> void()
+%% @end
+%%--------------------------------------------------------------------
+stop(_State) ->
+ ok.
View
21 src/tinymq_channel_controller.erl
@@ -2,24 +2,21 @@
-behaviour(gen_server).
--export([start_link/0, start_link/1]).
+-export([start_link/3]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-record(state, {channel, messages = [], subscribers = [], max_age, last_pull, supervisor}).
-start_link() ->
- start_link([]).
+start_link(MaxAge, ChannelSup, Channel) ->
+ gen_server:start_link(?MODULE, [MaxAge, ChannelSup, Channel], []).
-start_link(Args) ->
- gen_server:start_link(?MODULE, Args, []).
-
-init(Options) ->
- MaxAgeSeconds = proplists:get_value(max_age, Options, 60),
- Supervisor = proplists:get_value(supervisor, Options),
- Channel = proplists:get_value(channel, Options),
- {ok, #state{max_age = MaxAgeSeconds, supervisor = Supervisor, channel = Channel, last_pull = erlang:now()},
- MaxAgeSeconds * 1000}.
+init([MaxAge, ChannelSup, Channel]) ->
+ {ok, #state{max_age = MaxAge,
+ supervisor = ChannelSup,
+ channel = Channel,
+ last_pull = erlang:now()},
+ MaxAge * 1000}.
handle_call(_From, _, State) ->
{noreply, State}.
View
16 src/tinymq_channel_sup.erl
@@ -2,21 +2,17 @@
-behaviour(supervisor).
--export([start_link/0, start_link/1, start_child/2, init/1]).
+-export([start_link/0, start_link/1, init/1]).
start_link() ->
supervisor:start_link(?MODULE, []).
start_link(StartArgs) ->
supervisor:start_link(?MODULE, StartArgs).
-start_child(Supervisor, StartArgs) ->
- supervisor:start_child(Supervisor,
- {mq_channel_controller, {tinymq_channel_controller, start_link, [StartArgs]},
- permanent,
- 2000,
- worker,
- [tinymq_channel_controller]}).
-
init(_StartArgs) ->
- {ok, {{one_for_one, 10, 10}, []}}.
+ {ok, {{simple_one_for_one, 10, 10},
+ [
+ {mq_channel_controller, {tinymq_channel_controller, start_link, []},
+ permanent, 2000, worker, [tinymq_channel_controller]}
+ ]}}.
View
14 src/tinymq_controller.erl
@@ -2,20 +2,17 @@
-behaviour(gen_server).
--export([start_link/0, start_link/1]).
+-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-record(state, {dict, max_age}).
start_link() ->
- start_link([]).
+ gen_server:start_link({local, tinymq}, ?MODULE, [], []).
-start_link(Args) ->
- gen_server:start_link({local, tinymq}, ?MODULE, Args, []).
-
-init(Options) ->
- MaxAgeSeconds = proplists:get_value(max_age, Options, 60),
+init([]) ->
+ {ok, MaxAgeSeconds} = application:get_env(max_age),
{ok, #state{dict = dict:new(), max_age = MaxAgeSeconds}}.
handle_call({subscribe, Channel, Timestamp, Subscriber}, From, State) ->
@@ -67,8 +64,7 @@ find_or_create_channel(Channel, #state{dict = Chan2Pid, max_age = MaxAge} = Stat
{Pid, State};
_ ->
{ok, ChannelSup} = tinymq_channel_sup:start_link(),
- {ok, ChannelPid} = tinymq_channel_sup:start_child(ChannelSup,
- [{max_age, MaxAge}, {supervisor, ChannelSup}, {channel, Channel}]),
+ {ok, ChannelPid} = supervisor:start_child(ChannelSup, [MaxAge, ChannelSup, Channel]),
{ChannelPid, State#state{
dict = dict:store(Channel, ChannelPid, Chan2Pid)
}}
View
9 src/tinymq_sup.erl
@@ -2,19 +2,16 @@
-behaviour(supervisor).
--export([start_link/0, start_link/1]).
+-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link(?MODULE, []).
-start_link(StartArgs) ->
- supervisor:start_link(?MODULE, StartArgs).
-
-init(StartArgs) ->
+init([]) ->
{ok, {{one_for_one, 10, 10}, [
- {mq_controller, {tinymq_controller, start_link, [StartArgs]},
+ {mq_controller, {tinymq_controller, start_link, []},
permanent,
2000,
worker,

0 comments on commit 0b65c00

Please sign in to comment.