Erlang provides us with a ready-made implementation of such a server, which is gen_server, without having to write the server program on its own.
Here are a few things to do:
1. Determine the name of our callback module
2. Writing interface functions
3. Implementation of six callback methods
That means we just have to focus on the implementation of the callback module OK.
1:-module (XXXX) define module name
2: An interface function, a function that can be called.
Here you can define all the required methods as portals to function calls.
such as the common Start (), Stop (), and so on. Their role is to invoke Gen_server to complete the specified function.
3: Implement the callback function, altogether need to implement six.
Init ()/1----This method is called by the server when we call Gen_server:start_link
HANDLE_CALL/3----This method is called by the server when we call Gen_server:call
HANDLE_CAST/2----This method is called by the server when we call Gen_server:cast
HANDLE_INFO/2----Handle native messages sent to the server from outside, such as a pass-through PID! Message sent over
TERMINATE/2----method that is called when the server is terminated
CODE_CHANGE/2----Code hot-swap, function called when software upgrade code is replaced
Erlang's Gen_server Chapter