迷彩 潜伏 隐蔽 伪装
分类: Erlang
2013-02-07 19:38:02
原文地址:Erlang构建简单应用的过程解析 作者:flagcugb
{application, %(1) demo, %(2) [ %(3) {description, "this is a test erlang project"}, {vsn, "0.1.0"}, {modules, [demo_app, demo_sup, demo_server]}, {registered, [demo_sup]}, {applications, [kernel, stdlib]}, {mod, {demo_app, []}}] }.
%% %% flagcugb@126.com %% -module(demo_app). -behaviour(application). -export([ start/2, stop/1 ]). start(_Type, _StartArgs) -> case demo_sup:start_link() of {ok, Pid} -> {ok, Pid}; Error -> Error end. stop(_State) -> ok.
-module(demo_sup). -behaviour(supervisor). -export([start_link/0]). -export([init/1]). -define(SERVER, ?MODULE). start_link() -> supervisor:start_link({local, ?SERVER}, ?MODULE, []). init([]) -> Server = {demo_server, {demo_server, start_link, []}, permanent, 2000, worker, [demo_server]}, Children = [Server], RestartSdemoategy = {one_for_one, 0, 1}, {ok, {RestartSdemoategy, Children}}.第五步, 实现工作者模块 demo_server.erl
%%% ------------------------------------------------------------------- %%% Author : flagcugb@126.com %%% Description : 每秒钟打印一个 Tic 信息 %%% %%% ------------------------------------------------------------------- -module(demo_server). -behaviour(gen_server). -export([start_link/0, stop/0]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -record(state, {}). -define(LOOP_TIME, 1000). %% @doc 启动一个gen_server start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). stop() -> ok. init([]) -> ok = start_tic_timer(), erlang:send_after(?LOOP_TIME, self(), {'tic'}), {ok, #state{}}. handle_call(_Request, _From, State) -> Reply = ok, {reply, Reply, State}. handle_cast(_Msg, State) -> {noreply, State}. handle_info({'tic'}, State) -> ok = do_tic(), ok = start_tic_timer(), {noreply, State}; handle_info(_Info, State) -> {noreply, State}. terminate(_Reason, _State) -> ok. code_change(_OldVsn, State, _Exdemoa) -> {ok, State}. %%------------- %% internal API %%------------- do_tic() -> io:format("* Tic ~p~n", [calendar:local_time()]), ok. start_tic_timer() -> erlang:send_after(?LOOP_TIME, self(), {'tic'}), ok.第六步, 编译
.PHONY:all clean all: erlc -o ebin src/*.erl clean: @echo "cleaning ... ..." -rm ebin/*.beam @echo "ok"
第七步, 启动
erl -pa ebin