Permalink
Browse files

Initial Import

  • Loading branch information...
0 parents commit aad767a0e0c6bb993ee35ff42757037f290ca2f8 @zpeters zpeters committed Mar 24, 2011
No changes.
No changes.
No changes.
@@ -0,0 +1,10 @@
+{application, myapp,
+ [{description, "Sample Erlang OTP Application"},
+ {vsn, "0.1.0"},
+ {modules, [myapp_app,
+ myapp_sup,
+ myapp_server]},
+ {registered, [myapp_sup]},
+ {applications, [kernel, stdlib]},
+ {mod, {myapp_app, []}}
+ ]}.
No changes.
No changes.
No changes.
@@ -0,0 +1,18 @@
+-module(myapp_app).
+-behaviour(application).
+
+-export([
+ start/2,
+ stop/1
+ ]).
+
+start(_Type, _StartArgs) ->
+ case myapp_sup:start_link() of
+ {ok, Pid} ->
+ {ok, Pid};
+ Other ->
+ {error, Other}
+ end.
+
+stop(_State) ->
+ ok.
@@ -0,0 +1,144 @@
+%%%-------------------------------------------------------------------
+%%% @author <Zach@ZACH-PC>
+%%% @copyright (C) 2011,
+%%% @doc
+%%% Sample application server
+%%% @end
+%%% Created : 23 Mar 2011 by <Zach@ZACH-PC>
+%%%-------------------------------------------------------------------
+-module(myapp_server).
+
+-behaviour(gen_server).
+
+%% API
+-export([
+ start_link/0,
+ test_call/1,
+ test_cast/1
+ ]).
+
+%% gen_server callbacks
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+-define(SERVER, ?MODULE).
+
+-record(state, {}).
+
+%%%===================================================================
+%%% API
+%%%===================================================================
+
+%%--------------------------------------------------------------------
+%% @doc
+%% Starts the server
+%%
+%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
+%% @end
+%%--------------------------------------------------------------------
+start_link() ->
+ gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
+
+test_call(Msg) ->
+ gen_server:call(?SERVER, {test, Msg}).
+
+test_cast(Msg) ->
+ gen_server:cast(?SERVER, {test, Msg}).
+
+%%%===================================================================
+%%% gen_server callbacks
+%%%===================================================================
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Initializes the server
+%%
+%% @spec init(Args) -> {ok, State} |
+%% {ok, State, Timeout} |
+%% ignore |
+%% {stop, Reason}
+%% @end
+%%--------------------------------------------------------------------
+init([]) ->
+ {ok, #state{}}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Handling call messages
+%%
+%% @spec handle_call(Request, From, State) ->
+%% {reply, Reply, State} |
+%% {reply, Reply, State, Timeout} |
+%% {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, Reply, State} |
+%% {stop, Reason, State}
+%% @end
+%%--------------------------------------------------------------------
+handle_call({test, Message}, _From, State) ->
+ io:format("Call: ~p~n", [Message]),
+ Reply = ok,
+ {reply, Reply, State};
+handle_call(_Request, _From, State) ->
+ Reply = ok,
+ {reply, Reply, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Handling cast messages
+%%
+%% @spec handle_cast(Msg, State) -> {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, State}
+%% @end
+%%--------------------------------------------------------------------
+handle_cast({test, Message}, State) ->
+ io:format("Cast: ~p~n", [Message]),
+ {noreply, State};
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Handling all non call/cast messages
+%%
+%% @spec handle_info(Info, State) -> {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, State}
+%% @end
+%%--------------------------------------------------------------------
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% This function is called by a gen_server when it is about to
+%% terminate. It should be the opposite of Module:init/1 and do any
+%% necessary cleaning up. When it returns, the gen_server terminates
+%% with Reason. The return value is ignored.
+%%
+%% @spec terminate(Reason, State) -> void()
+%% @end
+%%--------------------------------------------------------------------
+terminate(_Reason, _State) ->
+ ok.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Convert process state when code is changed
+%%
+%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
+%% @end
+%%--------------------------------------------------------------------
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%%%===================================================================
+%%% Internal functions
+%%%===================================================================
@@ -0,0 +1,20 @@
+-module(myapp_sup).
+-behaviour(supervisor).
+
+%% API
+-export([start_link/0]).
+
+%% Supervisor callbacks
+-export([init/1]).
+
+-define(SERVER, ?MODULE).
+
+start_link() ->
+ supervisor:start_link({local, ?SERVER}, ?MODULE, []).
+
+init([]) ->
+ Server = {myapp_server, {myapp_server, start_link, []},
+ permanent, 2000, worker, [myapp_server]},
+ Children = [Server],
+ RestartStrategy = {one_for_one, 3, 1},
+ {ok, {RestartStrategy, Children}}.

0 comments on commit aad767a

Please sign in to comment.