Created
June 12, 2019 06:32
-
-
Save googya/f9dd03942ef4acdc35ec2dd618c8573b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(server1). | |
-export([start/2, rpc/2]). | |
start(Name, Mod) -> | |
register(Name, spawn(fun() -> loop(Name, Mod, Mod:init()) end)). | |
rpc(Name, Request) -> | |
io:format(" self at rpc is ~p~n", [self()]), | |
io:format(" Name at rpc is ~p~n", [Name]), | |
Name ! { self(), Request }, | |
receive | |
{ Name, Response } -> | |
io:format(" self is ~p~n", [self()]), | |
Response | |
end. | |
loop(Name, Mod, State) -> | |
io:format(" self at loop is ~p~n", [self()]), | |
receive | |
{ From, Request } -> | |
io:format(" From in loop is ~p~n", [From]), | |
{ Response, State1 } = Mod:handle(Request, State), | |
From ! { Name, Response}, | |
loop(Name, Mod, State1) | |
end. | |
-module(name_server). | |
-export([init/0, add/2, find/1, handle/2]). | |
-import(server1, [rpc/2]). | |
add(Name, Place) -> | |
io:format(" self in add is ~p~n", [self()]), | |
rpc(name_server, { add, Name, Place}). | |
find(Name) -> | |
io:format(" self at find is ~p~n", [self()]), | |
rpc(name_server, {find, Name}). | |
init() -> dict:new(). | |
handle({add, Name, Place}, Dict) -> | |
io:format(" self at handle add is ~p~n", [self()]), | |
{ok, dict:store(Name, Place, Dict)}; | |
handle({find, Name}, Dict) -> | |
io:format(" self at handle find is ~p~n", [self()]), | |
{dict:find(Name, Dict), Dict}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用以下命令查看进程信息