- -module(lzy_name_svc).
- -behaviour(gen_server).
- -export([init/0, init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
- -export([start/0, start/1, stop/0, save/2, load/1, load_all/0, remove/1, remove_all/0]).
- %% Interface functions.
- start() ->
- gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
- start(Args) ->
- gen_server:start_link({local, ?MODULE}, ?MODULE, Args, []).
- stop() ->
- gen_server:call(?MODULE, stop).
- %% @spec save(Key, Value) -> OldValue.
- save(Key, Value) ->
- gen_server:call(?MODULE, {save, Key, Value}).
- %% @spec load(Key) -> Value.
- load(Key) ->
- gen_server:call(?MODULE, {load, Key}).
- %% @spec load_all() -> [{Key, Value}].
- load_all() ->
- gen_server:call(?MODULE, {load_all}).
- %% @spec remove(Key) -> Value.
- remove(Key) ->
- gen_server:call(?MODULE, {remove, Key}).
- %% @spec remove_all() -> [{Key, Value}].
- remove_all() ->
- gen_server:call(?MODULE, {remove_all}).
- %% Callback functions.
- init([]) ->
- {ok, local};
- init([{isolation, NameServer}]) ->
- {ok, {isolation, NameServer}}.
- handle_call({save, Key, Value}, _From, NameServer) ->
- {reply, do_save(Key, Value, NameServer), NameServer};
- handle_call({load, Key}, _From, NameServer) ->
- {reply, do_load(Key, NameServer), NameServer};
- handle_call({load_all}, _From, NameServer) ->
- {reply, do_load_all(NameServer), NameServer};
- handle_call({remove, Key}, _From, NameServer) ->
- {reply, do_remove(Key, NameServer), NameServer};
- handle_call({remove_all}, _From, NameServer) ->
- {reply, do_remove_all(NameServer), NameServer};
- handle_call({stop}, _From, NameServer) ->
- {stop, normal, stopped, NameServer}.
- %% Default implement.
- handle_cast(_Msg, State) ->
- {noreply, State}.
- handle_info(_Info, State) ->
- {noreply, State}.
- terminate(_Reason, _State) ->
- ok.
- code_change(_OldVsn, State, _Extra) ->
- {ok, State}.
- %% Private functions.
- do_save(Key, Value, {isolation, NameServer}) ->
- NameServer ! {self(), save, Key, Value},
- receive
- Msg -> Msg
- end;
- do_save(Key, Value, _) ->
- erlang:put(Key, Value).
- do_load(Key, {isolation, NameServer}) ->
- NameServer ! {self(), load, Key},
- receive
- Msg -> Msg
- end;
- do_load(Key, _) ->
- erlang:get(Key).
- do_load_all({isolation, NameServer}) ->
- NameServer ! {self(), load_all},
- receive
- Msg -> Msg
- end;
- do_load_all(_) ->
- erlang:get().
- do_remove(Key, {isolation, NameServer}) ->
- NameServer ! {self(), remove, Key},
- receive
- Msg -> Msg
- end;
- do_remove(Key, _) ->
- erlang:erase(Key).
- do_remove_all({isolation, NameServer}) ->
- NameServer ! {self(), remove_all},
- receive
- Msg -> Msg
- end;
- do_remove_all(_) ->
- erlang:erase().
上面这段代码就是 lzy_name_svc 名称服务了,有些地方写得有点冗余,呵呵。
为了能够替换字典进程来测试验证名称服务功能,还写了一个超简单的 foo_svc 服务,用来和 lzy_name_svc 通信完成进程字典存取。
- -module(foo_svc).
- -export([start/0, load_all/0, server_pid/0]).
- start() ->
- register(fs, spawn(fun() -> loop() end)).
- load_all() ->
- fs ! {self(), load_all},
- receive
- Msg -> Msg
- end.
- server_pid() ->
- fs ! { self(), server_pid},
- receive
- Msg -> Msg
- end.
- loop() ->
- receive
- {From, save, Key, Value} ->
- From ! erlang:put(Key, Value),
- loop();
- {From, load, Key} ->
- From ! erlang:get(Key),
- loop();
- {From, load_all} ->
- From ! erlang:get(),
- loop();
- {From, remove, Key} ->
- From ! erlang:erase(Key),
- loop();
- {From, remove_all} ->
- From ! erlang:erase(),
- loop();
- {From, server_pid} ->
- From ! self(),
- loop()
- end.
下面的代码就是创建和调用服务的相关代码了,一起贴上来。第一段是以缺省方式启动了 lzy_name_svc 服务,并向存取 abc -> 123 名称。
- C:\Program Files\erl5.6.4\usr>..\bin\erl -sname server
- Eshell V5.6.4 (abort with ^G)
- (server@lzy)1> c(lzy_name_svc).
- {ok,lzy_name_svc}
- (server@lzy)2> c(foo_svc.erl).
- {ok,foo_svc}
- (server@lzy)3> lzy_name_svc:start().
- {ok,<0.47.0>}
- (server@lzy)4> lzy_name_svc:save(abc, 123).
- undefined
- (server@lzy)5> lzy_name_svc:load(abc).
- 123
- (server@lzy)6> lzy_name_svc:load(efg).
- undefined
- (server@lzy)7> lzy_name_svc:load_all().
- [{abc,123},
- {'$ancestors',[<0.35.0>]},
- {'$initial_call',{gen,init_it,
- [gen_server,<0.35.0>,<0.35.0>,
- {local,lzy_name_svc},
- lzy_name_svc,[],[]]}}]
- (server@lzy)8> lzy_name_svc:remove(abc).
- 123
- (server@lzy)9> lzy_name_svc:load(abc).
- undefined
下面这段是启动 foo_svc 服务,用它创建的进程来专门存储名称数据,是通过 lzy_name_svc:start/1 传入的 PID。
- C:\Program Files\erl5.6.4\usr>..\bin\erl -sname server
- Eshell V5.6.4 (abort with ^G)
- (server@lzy)1> foo_svc:start().
- true
- (server@lzy)2> NameSvcPid = foo_svc:server_pid().
- <0.37.0>
- (server@lzy)3> lzy_name_svc:start([{isolation, NameSvcPid}]).
- {ok,<0.40.0>}
- (server@lzy)4> lzy_name_svc:save(abc, 123).
- undefined
- (server@lzy)5> lzy_name_svc:load(abc).
- 123
- (server@lzy)6> foo_svc:load_all().
- [{abc,123}]
- (server@lzy)7> lzy_name_svc:remove_all().
- [{abc,123}]
- (server@lzy)8> foo_svc:load_all().
- []
上边的两段都是在同一机器上的同一 erlang 节点上完成服务调用的,下面这段代码是 lzy_name_svc 服务基于上边状态时,在同一机器的另外了个 erlang 节点上通过 rpc 库完成服务调用的。
- C:\Program Files\erl5.6.4\usr>..\bin\erl -sname client1
- Eshell V5.6.4 (abort with ^G)
- (client1@lzy)1> rpc:call(server@lzy, lzy_name_svc, save, [abc, 123]).
- undefined
- (client1@lzy)2> rpc:call(server@lzy, foo_svc, load_all, []).
- [{abc,123}]
呵呵,挺入门的,就当做为学习过程的记录吧。看好 erlang。
在学习的过程中,有一个事情比较不解,就是对于 字典进程的 “热替换” 。 我想本应该是可以通过 gen_server behaviour 用于“热代码替换”的 code_change 方法完成的,但试了几次都达不到目的,服务倒是跑的正常,可是字典进程就是不能热替换,code_change 正常返回,可是名称数据却还是原有字典进程的。测试验证代码如下:
- C:\Program Files\erl5.6.4\usr>..\bin\erl -sname server
- Eshell V5.6.4 (abort with ^G)
- (server@lzy)1> lzy_name_svc:start().
- {ok,<0.37.0>}
- (server@lzy)2> lzy_name_svc:save(abc, 123).
- undefined
- (server@lzy)3> lzy_name_svc:load_all().
- [{abc,123},
- {'$ancestors',[<0.35.0>]},
- {'$initial_call',{gen,init_it,
- [gen_server,<0.35.0>,<0.35.0>,
- {local,lzy_name_svc},
- lzy_name_svc,[],[]]}}]
- (server@lzy)4> foo_svc:start().
- true
- (server@lzy)5> NameSvcPid = foo_svc:server_pid().
- <0.41.0>
- (server@lzy)6> foo_svc:load_all().
- []
- (server@lzy)7> lzy_name_svc:code_change(foo, NameSvcPid, foo).
- {ok,<0.41.0>}
- (server@lzy)8> lzy_name_svc:load_all().
- [{abc,123},
- {'$ancestors',[<0.35.0>]},
- {'$initial_call',{gen,init_it,
- [gen_server,<0.35.0>,<0.35.0>,
- {local,lzy_name_svc},
- lzy_name_svc,[],[]]}}]
还请哪位 erlang guru 指点~
// 2009.02.07 22:52 添加 ////
这里提供了该名称服务的新迭代版本。
gen_server tasting 之超简单名称服务(续)
添加了如下功能:
- 使用 otp 监控树保证服务可靠性。
- 添加日志功能,记录警告事件。
- 将名称服务打包为 application。
- 开放 socket 服务,使用 vsns://verb /param 自定义协议对外提供访问支持。
安徽新华电脑学校专业职业规划师为你提供更多帮助【在线咨询】