View Source jhn_server behaviour (jhn_stdlib v5.1.0)

This is a generic server in the spirit of OTP's gen_server but subtly different. The behaviour module provides the server of a client-server relation. A generic server process (jhn_server) implemented using this module has a standard set of interface functions. It also fits into an OTP supervision tree and supports generic OTP tooling for, e.g., dynamic upgrades.

A jhn_server process assumes all specific parts to be located in a callback module exporting a predefined set of functions, some optional.

The relationship between the behavior functions and the callback functions is as follows: ----->/-----> fuction call/return ====> message sent

User module: <-------------> jhn_server module: <--/==> Callback module:

-----------------------------------------------------------------------

jhn_server:create/1/2 -----> proc_lib:spawn ----> init/1

jhn_server:create/1/2 <----- proc_lib:spawn <==== init/1

-----------------------------------------------------------------------

jhn_server:call/2/3 =====> (jhn_server) -----> request/2

jhn_server:call/2/3 <===== (jhn_server) <----- jhn_server:reply/1/2 (optional)

-----------------------------------------------------------------------

jhn_server:sync/1/2 =====> (jhn_server)

jhn_server:sync/1/2 <===== (jhn_server)

-----------------------------------------------------------------------

jhn_server:cast/2/3 =====> (jhn_server) -----> request/2

All the following are optional

Other process =====> Callback module

-----------------------------------------------------------------------

Id ! Message =====> message/2

sys:terminate/2 =====> terminate/3

sys:code_change/4 =====> code_change/3

sys:get_status/2 =====> format_status/2

If a callback function fails or returns a bad value, the jhn_server process terminates. N.B. that a gen_server process does not trap exit signals automatically, this must be explicitly initiated in the callback module. The process is by default linked to the parent.

jhn_server supports hibernation. Any call, cast or plain message sent to the process that has not a matching clause in the appropriate request/2 or messsage/2 call back function will be discarded and logged as unexpected. If the optional callback funtion message/2 is not defined there are no matching function clauses at all.

The callback module shall export:

init(Args) ==> {ok, State} | {hibernate, State} | ignore | {error, stop} | {stop, Reason}

request(Msg, State) ==> {ok, State} | {hibernate, State} | {stop, Reason}

The callback module can export:

message(Msg, State) ==> {ok, State} | {hibernate, State} | {stop, Reason}

terminate(Reason, State) ==> Return value ignored

Lets the user module clean up always called when server terminates

code_change(OldVsn, Data, Extra) ==> {ok, State}

format_status(Opt, _) ==> {ok, State}

Uses the old format will be replaced.

Summary

Functions

A cast is made to servers registered as Server on all the connected nodes.

A cast is made to servers registered as Server on all Nodes.

A call is made to Server with default timeout 5000ms.

A call is made to Server with Timeout. Will generate a timeout exception if the Server takes more than Timeout time to answer. Generates an exception if the process is dead, dies, or no process registered under that name. If the server replies that is returned from the call.

A timer a timer started using delayed_cast/2, returns time remaining or false.

A cast is made to the server, allways returns ok.

Creates a jhn_server.

Creates a jhn_server with options. Options are: {link, Boolean} -> if the server is linked to the parent, default true {timeout, infinity | Integer} -> Time in ms for the server to create and initialise, after that an exception is generated, default 5000. {name, Atom} -> name that the server is registered under. {arg, Term} -> argument provided to the init/1 callback function, default is 'no_arg'.

A cast is made to the calling Server after Delay ms, always returns a timer reference that can be canceled using cancel/1.

Called inside a request/2 of callback function for a call it will provide an opaque data data enables a reply to the call outside the scope of the callback function.

Called inside callback function to provide reply to a call.

Called by any process will send a reply to the one that sent the request to the server, the From argument is the result from a call to from/1 inside a request/2 callback function in response to a jhn_server:call/2/3

A synchronization is made with Server with default timeout 5000ms.

A synchronization is made with Server with Timeout. Will generate a timeout exception if the Server takes more than Timeout time to answer. Generates an exception if the process is dead, dies, or no process registered under that name. If the server returns, ok is returned.

Called inside a request/2 and it tells if it is a call or a cast.

Types

-type from() :: reference().
-type init_return(State) :: ignore | return(State).
-type opt() :: {atom(), _}.
-type opts() :: [opt()].
-type return(State) :: {ok, State} | {hibernate, State} | {error, _} | {stop, _}.
-type server_ref() :: atom() | {atom(), node()} | pid().

Callbacks

Link to this callback

code_change/3

View Source (optional)
-callback code_change(_, State, _) -> return(State).
Link to this callback

format_status/2

View Source (optional)
-callback format_status(_, _) -> _.
-callback init(State) -> init_return(State).
-callback message(_, State) -> return(State).
-callback request(_, State) -> return(State).
-callback terminate(_, _, _) -> _.

Functions

-spec abcast(atom(), _) -> ok.

A cast is made to servers registered as Server on all the connected nodes.

Link to this function

abcast(Nodes, Name, Msg)

View Source
-spec abcast([node()], atom(), _) -> ok.

A cast is made to servers registered as Server on all Nodes.

-spec call(server_ref(), _) -> _.

A call is made to Server with default timeout 5000ms.

Link to this function

call(Server, Msg, Timeout)

View Source
-spec call(server_ref(), _, timeout()) -> _.

A call is made to Server with Timeout. Will generate a timeout exception if the Server takes more than Timeout time to answer. Generates an exception if the process is dead, dies, or no process registered under that name. If the server replies that is returned from the call.

-spec cancel(reference()) -> non_neg_integer() | false.

A timer a timer started using delayed_cast/2, returns time remaining or false.

-spec cast(server_ref(), _) -> ok.

A cast is made to the server, allways returns ok.

-spec create(atom()) -> {ok, pid()} | ignore | {error, _}.

Creates a jhn_server.

-spec create(atom(), opts()) -> {ok, pid()} | ignore | {error, _}.

Creates a jhn_server with options. Options are: {link, Boolean} -> if the server is linked to the parent, default true {timeout, infinity | Integer} -> Time in ms for the server to create and initialise, after that an exception is generated, default 5000. {name, Atom} -> name that the server is registered under. {arg, Term} -> argument provided to the init/1 callback function, default is 'no_arg'.

-spec delayed_cast(non_neg_integer(), _) -> reference().

A cast is made to the calling Server after Delay ms, always returns a timer reference that can be canceled using cancel/1.

-spec from() -> from().

Called inside a request/2 of callback function for a call it will provide an opaque data data enables a reply to the call outside the scope of the callback function.

-spec reply(_) -> ok.

Called inside callback function to provide reply to a call.

-spec reply(from(), _) -> ok.

Called by any process will send a reply to the one that sent the request to the server, the From argument is the result from a call to from/1 inside a request/2 callback function in response to a jhn_server:call/2/3

-spec sync(server_ref()) -> ok.

A synchronization is made with Server with default timeout 5000ms.

-spec sync(server_ref(), timeout()) -> _.

A synchronization is made with Server with Timeout. Will generate a timeout exception if the Server takes more than Timeout time to answer. Generates an exception if the process is dead, dies, or no process registered under that name. If the server returns, ok is returned.

-spec type() -> call | cast.

Called inside a request/2 and it tells if it is a call or a cast.