What is Megaphone?

What is Megaphone?
The Megaphone project is about enhancing open source chat software. Specifically, the goal is to allow ejabberd to support 1,000,000 simultaneous users. See The Plan page for more details on how I plan to solve this problem. See the About this Blog page for more details on why I created this blog.

Wednesday, December 21, 2011

OTP Behavior: gen_server

Previously
  • I had some success with trying to send messages around.
  • I created some code to format messages correctly on the ejabberd side.
  • I decided that I needed to read up on erlang behaviors.
OK, so last time I said erlang supervisor behavior but I decided to broaden the scope just a bit to include 

  • gen_server
  • supervisor
  • application
  • gen_fsm
I will come up with a very brief summary of what each behavior is supposed to do, what you have to do in order to implement the service, what calls clients make in order to use the service.

gen_server



This is supposed to cover servers that you would normally start up in another process and that you want to monitor.  The monitoring aspect will have to wait until a discussion of the supervisor behavior.


Client API

  • gen_server:start(Module, Args, Options)
    Start up the server. Calls init(Args).
  • gen_server:start(Name, Module, Args, Options)
    Start up the server, registering the process with Name.  
    Calls init(Args) in module, Module.
  • gen_server:start_link(Module, Args, Options)
    Start up the server, link to the calling process.  
    Calls init(Args) in module, Module.
  • gen_server:start_link(Name, Module, Args, Options)
    Start up the server, link to the calling process.  Register the PID for the process as Name.  Calls init(Args) in module, Module.
  • gen_server:call(Server, Message)
    Make a synchronous call to the server.  Name must match the value used in gen_server:start/_link.  Calls handle_call(Message) on the server.  The Server param is one of 
    • PID - the PID of the server process
    • Name - the name of the server process
    • {local, Name} - the name of the server process
    • {global, Name} - the name of the global server process.
  • gen_server:cast(Name, Message)
    Make asynchronous call to the server.  Name must match the value used in gen_server:start/_link.  Calls handle_cast(Message) on the server.

Callbacks

  • init(Arguments) -> {ok, LoopData}Called by start and start_link when the server is started up.
  • handle_call (Message, Data)
    Called by gen_server:call. The results of the call vary depending on the return value:
    • {reply, Reply, NewData} - returns the Reply to the client.
    • {noreply, NewData} - do not reply to the client.
    • {stop, Reason, Reply, NewData}- returns the Reply to the client and then stops the server.
    • {stop, noreply, Reason, NewData}- stop the server without sending a reply.
  • handle_cast (Message, Data) -> {noreply, NewData}
    Called by gen_server:cast.   The results of the call vary depending on the return value: 
    • {noreply, NewState} - does nothing remarkable.  
    • {stop, Reason, NewState} - shuts down the server with reason Reason.

  • terminate(Reason, Data) -> <ignored>Called when the server is shutting down as a result of returning stop from handle_call or handle_cast.  This gives the server a chance to clean up.

  • handle_info(Info, Data)
    optional function
    This is called if the server receives a message other than call or cast. If not defined then receiving such a message will terminate the server.
Next up, the supervisor behavior.

No comments:

Post a Comment