From b0426732cc19598f0c0c310b1e79918252495259 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Mon, 17 Jan 2011 15:47:00 +0100 Subject: Add plugin support for alternative name lookup OTP behaviour instances (gen_server, gen_fsm, gen_event) can currently register themselves either locally or globally, and the behaviour libraries (including gen.erl) support both addressing methods, as well as the normal Pid and {Name, Node}. However, there are alternative registry implementations - e.g. gproc - and one can well imagine other ways of locating a behaviour instance, e.g. on a node connected only via a TCP tunnel, rather than via Distributed Erlang. In all these cases, one needs to write extra code to identify the behaviour instance, even though the instance itself need not be aware of how it is located. This patch introduces a new way of locating a behaviour instance: {via, Module, Name}. Module is expected to export a subset of the functions in global.erl, namely: register_name(Name, Pid) -> yes | no whereis_name(Name) -> pid() | undefined unregister_name(Name) -> ok send(Name, Msg) -> Pid Semantics are expected to be the same as for global.erl This can be used in all places where {global, Name} is accepted. faulty export in gen_fsm_SUITE.erl await process death in dummy_via:reset() fix error in gen_[server|fsm]:enter_loop() fix documentation --- lib/stdlib/doc/src/gen_server.xml | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'lib/stdlib/doc/src/gen_server.xml') diff --git a/lib/stdlib/doc/src/gen_server.xml b/lib/stdlib/doc/src/gen_server.xml index 1045766e01..90b77d5cb6 100644 --- a/lib/stdlib/doc/src/gen_server.xml +++ b/lib/stdlib/doc/src/gen_server.xml @@ -83,9 +83,10 @@ gen_server:abcast -----> Module:handle_cast/2 start_link(ServerName, Module, Args, Options) -> Result Create a gen_server process in a supervision tree. - ServerName = {local,Name} | {global,GlobalName} + ServerName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Module = atom() Args = term() Options = [Option] @@ -111,14 +112,22 @@ gen_server:abcast -----> Module:handle_cast/2 If ServerName={global,GlobalName} the gen_server is registered globally as GlobalName using global:register_name/2. If no name is provided, - the gen_server is not registered.

+ the gen_server is not registered. + If EventMgrName={via,Module,ViaName}, the event manager will + register with the registry represented by Module. + The Module callback should export the functions + register_name/2, unregister_name/1, + whereis_name/1 and send/2, which should behave like the + corresponding functions in global. Thus, + {via,global,GlobalName} is a valid reference.

Module is the name of the callback module.

Args is an arbitrary term which is passed as the argument to Module:init/1.

If the option {timeout,Time} is present, the gen_server is allowed to spend Time milliseconds initializing or it will be terminated and the start function - will return {error,timeout}.

+ will return {error,timeout}. +

If the option {debug,Dbgs} is present, the corresponding sys function will be called for each item in Dbgs. See @@ -151,9 +160,10 @@ gen_server:abcast -----> Module:handle_cast/2 start(ServerName, Module, Args, Options) -> Result Create a stand-alone gen_server process. - ServerName = {local,Name} | {global,GlobalName} + ServerName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Module = atom() Args = term() Options = [Option] @@ -178,9 +188,10 @@ gen_server:abcast -----> Module:handle_cast/2 call(ServerRef, Request, Timeout) -> Reply Make a synchronous call to a generic server. - ServerRef = Name | {Name,Node} | {global,GlobalName} | pid() + ServerRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Node = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Request = term() Timeout = int()>0 | infinity Reply = term() @@ -198,6 +209,8 @@ gen_server:abcast -----> Module:handle_cast/2 registered at another node, or {global,GlobalName}, if the gen_server is globally registered. + {via,Module,ViaName}, if the gen_server is + registered through an alternative process registry.

Request is an arbitrary term which is passed as one of the arguments to Module:handle_call/3.

@@ -281,9 +294,10 @@ gen_server:abcast -----> Module:handle_cast/2 cast(ServerRef, Request) -> ok Send an asynchronous request to a generic server. - ServerRef = Name | {Name,Node} | {global,GlobalName} | pid() + ServerRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Node = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Request = term() @@ -355,9 +369,10 @@ gen_server:abcast -----> Module:handle_cast/2    Dbg = trace | log | statistics     | {log_to_file,FileName} | {install,{Func,FuncState}} State = term() - ServerName = {local,Name} | {global,GlobalName} + ServerName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Timeout = int() | infinity
-- cgit v1.2.3