aboutsummaryrefslogtreecommitdiffstats
path: root/src/ranch.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-08-20 12:44:53 +0200
committerLoïc Hoguin <[email protected]>2012-08-20 12:44:53 +0200
commit0d84eda41c460433baa93cd06cb82a3d47cf814c (patch)
tree4145666ccff1ecdfd14eaeb59ffd11434382d1f0 /src/ranch.erl
parentd8981b586223af4bffb813e1e76c172828b0810a (diff)
downloadranch-0d84eda41c460433baa93cd06cb82a3d47cf814c.tar.gz
ranch-0d84eda41c460433baa93cd06cb82a3d47cf814c.tar.bz2
ranch-0d84eda41c460433baa93cd06cb82a3d47cf814c.zip
Add the 'ranch_transport' behaviour
At the same time we make the 'port' option optional, defaulting to 0.
Diffstat (limited to 'src/ranch.erl')
-rw-r--r--src/ranch.erl38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/ranch.erl b/src/ranch.erl
index 3360154..3a79312 100644
--- a/src/ranch.erl
+++ b/src/ranch.erl
@@ -22,6 +22,9 @@
-export([get_port/1]).
-export([get_protocol_options/1]).
-export([set_protocol_options/2]).
+-export([filter_options/3]).
+-export([set_option_default/3]).
+-export([require/1]).
%% @doc Start a listener for the given transport and protocol.
%%
@@ -112,3 +115,38 @@ get_protocol_options(Ref) ->
set_protocol_options(Ref, ProtoOpts) ->
ListenerPid = ranch_server:lookup_listener(Ref),
ok = ranch_listener:set_protocol_options(ListenerPid, ProtoOpts).
+
+%% @doc Filter a list of options and remove all unwanted values.
+%%
+%% It takes a list of options, a list of allowed keys and an accumulator.
+%% This accumulator can be used to set default options that should never
+%% be overriden.
+-spec filter_options([{atom(), any()}], [atom()], Acc)
+ -> Acc when Acc :: [any()].
+filter_options([], _, Acc) ->
+ Acc;
+filter_options([Opt = {Key, _}|Tail], AllowedKeys, Acc) ->
+ case lists:member(Key, AllowedKeys) of
+ true -> filter_options(Tail, AllowedKeys, [Opt|Acc]);
+ false -> filter_options(Tail, AllowedKeys, Acc)
+ end.
+
+%% @doc Add an option to a list, but only if it wasn't previously set.
+-spec set_option_default(Opts, atom(), any())
+ -> Opts when Opts :: [{atom(), any()}].
+set_option_default(Opts, Key, Value) ->
+ case lists:keymember(Key, 1, Opts) of
+ true -> Opts;
+ false -> [{Key, Value}|Opts]
+ end.
+
+%% @doc Start the given applications if they were not already started.
+-spec require(list(module())) -> ok.
+require([]) ->
+ ok;
+require([App|Tail]) ->
+ case application:start(App) of
+ ok -> ok;
+ {error, {already_started, App}} -> ok
+ end,
+ require(Tail).