Permalink
Browse files

Initial commit together with the first test for creating a named pipe

Also the rebar.config file
  • Loading branch information...
1 parent e07dc11 commit 85cdf4b9feaccf0505530b5a1bc28c4fd0c88bb6 Gianfranco Alongi committed Mar 21, 2012
@@ -0,0 +1 @@
+{cover_enabled,true}.
@@ -0,0 +1,3 @@
+-record(message,{body :: binary(),
+ byte_size :: non_neg_integer()
+ }).
@@ -0,0 +1,12 @@
+{application, pubsub,
+ [
+ {description, ""},
+ {vsn, "1"},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib
+ ]},
+ {mod, { pubsub_app, []}},
+ {env, []}
+ ]}.
@@ -0,0 +1,16 @@
+-module(pubsub_app).
+
+-behaviour(application).
+
+%% Application callbacks
+-export([start/2, stop/1]).
+
+%% ===================================================================
+%% Application callbacks
+%% ===================================================================
+
+start(_StartType, _StartArgs) ->
+ pubsub_sup:start_link().
+
+stop(_State) ->
+ ok.
@@ -0,0 +1,64 @@
+%%%-------------------------------------------------------------------
+%%% @author Gianfranco Alongi <zenon@zentop.local>
+%%% @copyright (C) 2012, Gianfranco Alongi
+%%% Created : 21 Mar 2012 by Gianfranco Alongi <zenon@zentop.local>
+%%%-------------------------------------------------------------------
+-module(pubsub_pipes).
+-behaviour(gen_server).
+
+-export([new_pipe/1,
+ get_pipes/0
+ ]).
+-export([start_link/0,
+ stop/0
+ ]).
+
+%% gen_server callbacks
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+-record(state, {pipes :: atom() %% ets table
+ }).
+
+%%%===================================================================
+start_link() ->
+ gen_server:start_link(?MODULE, ?MODULE, [], []).
+
+stop() ->
+ gen_server:call(?MODULE,stop).
+
+-spec(new_pipe(string()) -> ok).
+new_pipe(PipeName) ->
+ gen_server:call(?MODULE,{create_pipe,PipeName}).
+
+-spec(get_pipes() -> [string()]).
+get_pipes() ->
+ gen_server:call(?MODULE,get_pipes).
+
+%%%===================================================================
+init([]) ->
+ {ok, #state{pipes = ets:new(pipes,[bag])}}.
+
+handle_call({create_pipe,PipeName}, _From, State) ->
+ ets:insert_new(State#state.pipes,{PipeName,[]}),
+ {reply, ok, State};
+
+handle_call(get_pipes,_From,State) ->
+ Pipes = ets:foldl(fun({Key,_},Acc) -> [Key|Acc] end,
+ [],
+ State#state.pipes),
+ {reply,Pipes, State}.
+
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%%%===================================================================
@@ -0,0 +1,28 @@
+
+-module(pubsub_sup).
+
+-behaviour(supervisor).
+
+%% API
+-export([start_link/0]).
+
+%% Supervisor callbacks
+-export([init/1]).
+
+%% Helper macro for declaring children of supervisor
+-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
+
+%% ===================================================================
+%% API functions
+%% ===================================================================
+
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% ===================================================================
+%% Supervisor callbacks
+%% ===================================================================
+
+init([]) ->
+ {ok, { {one_for_one, 5, 10}, []} }.
+
@@ -0,0 +1,20 @@
+-module(pubsub_pipes_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+create_pipe_test() ->
+ {setup,
+ fun setup/0,
+ fun cleanup/1,
+ fun() ->
+ PipeName = "A",
+ {ok,_} = pubsub_pipes:new_pipe(PipeName),
+ ?assertEqual([PipeName],pubsub_pipes:get_pipes())
+ end}.
+
+setup() ->
+ pubsub_pipes:start_link().
+
+cleanup(_) ->
+ pubsub_pipes:stop().
+
+

0 comments on commit 85cdf4b

Please sign in to comment.