OTPを試す
30分プログラム、その380。ErlangでOTPを試してみた。
OTPはErlangのライブラリの一種で、曰く「挙動パターンをまとめたもので、コールバックでカスタマイズ可能」。僕の理解は、無理矢理導入したファンクタ。
要するに、適当なコールバック関数を用意するだけで、高機能・性能なサーバーが作れるライブラリ。
コードを書くときは、Emacsのerlang-modeで"M-x erlang-skel-init"したあとで"M-x tempo-template-erlang-generic-server"するとテンプレートが挿入されて、とてもらくちん。
使い方
1> fact_server:start_link(). {ok,<0.57.0>} 2> gen_server:call(fact_server,{fact,10}). 3628800 3> gen_server:call(fact_server,{fact,0}). 1
ソースコード
%%%------------------------------------------------------------------- %%% File : fact_server.erl %%% Author : MIZUNO Hiroki <mzp@home.local> %%% Description : %%% %%% Created : 1 Oct 2008 by MIZUNO Hiroki <mzp@home.local> %%%------------------------------------------------------------------- -module(fact_server). -behaviour(gen_server). %% API -export([start_link/0]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -record(state, {}). %%==================================================================== %% API %%==================================================================== %%-------------------------------------------------------------------- %% Function: start_link() -> {ok,Pid} | ignore | {error,Error} %% Description: Starts the server %%-------------------------------------------------------------------- start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). %%==================================================================== %% gen_server callbacks %%==================================================================== %%-------------------------------------------------------------------- %% Function: init(Args) -> {ok, State} | %% {ok, State, Timeout} | %% ignore | %% {stop, Reason} %% Description: Initiates the server %%-------------------------------------------------------------------- init([]) -> {ok, #state{}}. %%-------------------------------------------------------------------- %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} | %% {reply, Reply, State, Timeout} | %% {noreply, State} | %% {noreply, State, Timeout} | %% {stop, Reason, Reply, State} | %% {stop, Reason, State} %% Description: Handling call messages %%-------------------------------------------------------------------- % 書き換えた!! fact(0) -> 1; fact(N) -> N * fact(N-1). handle_call({fact,N}, _From, State) -> Reply = fact(N), {reply, Reply, State}. %%-------------------------------------------------------------------- %% Function: handle_cast(Msg, State) -> {noreply, State} | %% {noreply, State, Timeout} | %% {stop, Reason, State} %% Description: Handling cast messages %%-------------------------------------------------------------------- handle_cast(_Msg, State) -> {noreply, State}. %%-------------------------------------------------------------------- %% Function: handle_info(Info, State) -> {noreply, State} | %% {noreply, State, Timeout} | %% {stop, Reason, State} %% Description: Handling all non call/cast messages %%-------------------------------------------------------------------- handle_info(_Info, State) -> {noreply, State}. %%-------------------------------------------------------------------- %% Function: terminate(Reason, State) -> void() %% Description: 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. %%-------------------------------------------------------------------- terminate(_Reason, _State) -> ok. %%-------------------------------------------------------------------- %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState} %% Description: Convert process state when code is changed %%-------------------------------------------------------------------- code_change(_OldVsn, State, _Extra) -> {ok, State}. %%-------------------------------------------------------------------- %%% Internal functions %%--------------------------------------------------------------------