From 0d84eda41c460433baa93cd06cb82a3d47cf814c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 20 Aug 2012 12:44:53 +0200 Subject: Add the 'ranch_transport' behaviour At the same time we make the 'port' option optional, defaulting to 0. --- src/ranch.erl | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/ranch.erl') 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). -- cgit v1.2.3