<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rusty Razor Blade &#187; gen_server</title>
	<atom:link href="http://www.rustyrazorblade.com/category/gen_server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rustyrazorblade.com</link>
	<description>Tech Thoughts, Mostly on LAMP - by Jon Haddad</description>
	<lastBuildDate>Wed, 21 Jul 2010 20:42:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Erlang: Understanding gen_server</title>
		<link>http://www.rustyrazorblade.com/2009/04/erlang-understanding-gen_server/</link>
		<comments>http://www.rustyrazorblade.com/2009/04/erlang-understanding-gen_server/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 18:08:47 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[gen_server]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=538</guid>
		<description><![CDATA[Gen_server is a great way to create simple servers without having to write a lot of code at all.  Here&#8217;s a brief overview to get you started.
For some reason, figuring out how the gen_server behavior works in erlang was kind of a pain for me.  I think it&#8217;s because I can&#8217;t just implement [...]]]></description>
			<content:encoded><![CDATA[<p>Gen_server is a great way to create simple servers without having to write a lot of code at all.  Here&#8217;s a brief overview to get you started.</p>
<p>For some reason, figuring out how the <a href="http://erlang.org/doc/man/gen_server.html">gen_server </a>behavior works in erlang was kind of a pain for me.  I think it&#8217;s because I can&#8217;t just implement something, I need to know <em>why</em> it works.  Well, now I know.</p>
<p>So, lets get started.  <a href="http://erlang.org/doc/man/gen_server.html">Gen_server </a>is built into erlang.  It abstracts the message passing away.  As far as I can tell, yes, you could write this functionality yourself.  But why would you want to when it&#8217;s done for you?</p>
<p>For starters, there&#8217;s a few functions you need to implement.  If you&#8217;re using <a href="http://erlide.sourceforge.net/">Erlide</a>, you can create a new module and choose <a href="http://erlang.org/doc/man/gen_server.html">gen_server </a>as the behavior, and it&#8217;ll give you a skeleton.  Nifty.  I strongly recommend going this route, as it has a lot of helpful comments in the code.  </p>
<p>Lets write an addition and subtraction server.  It&#8217;s really not that useful, but it&#8217;s enough to get started and understand what&#8217;s happening.</p>
<p><strong>Init</strong></p>
<blockquote><p>
%% &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
%% Function: init/1<br />
%% Description: Initiates the server<br />
%% Returns: {ok, State}          |<br />
%%          {ok, State, Timeout} |<br />
%%          ignore               |<br />
%%          {stop, Reason}<br />
%% &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
init([]) -><br />
    {ok, 0}.
</p></blockquote>
<p>Here&#8217;s a very important thing to understand.   <strong>A <a href="http://erlang.org/doc/man/gen_server.html">gen_server </a>maintains a state.</strong>  The State that&#8217;s returned above is the starting state of the server.  In our case, the initial state will be zero.</p>
<p><strong>handle_call</strong><br />
Now we want to add some functionality.  Lets add addition and subtraction.  We&#8217;ll need to export our new functions as well, so lets make sure we put this in with the exports:</p>
<blockquote><p>-export([add/1, start/0, subtract/1]).</p></blockquote>
<p>And the actual code:</p>
<blockquote><p>add(Num) -> gen_server:call( ?MODULE, {add, Num}).<br />
subtract(Num) -> gen_server:call( ?MODULE, {subtract, Num}).</p>
<p>handle_call({add, Num}, _From, State) -> {reply, State + Num, State + Num};<br />
handle_call({subtract, Num}, _From, State) -> {reply, State &#8211; Num, State &#8211; Num};
</p></blockquote>
<p>When you call counting:add, it will in turn call the gen_server:call function, which takes a module and a parameter.  It will then call &#8220;handle_call&#8221; with this paramater, the Pid it came from, and the State of the server.  The parameter can be anything, so if we use a tuple, we can take advantage of erlang&#8217;s pattern matching.  handle_call will return what the gen_server should do next &#8211; reply (return a result), noreply, or stop.  In this case, we&#8217;re replying.  </p>
<p>The second parameter is the reply.  We&#8217;re just going to return the current count here.  </p>
<p>We also need to return the new server state in parameter 3.  I chose to put the new state in param 2 because it&#8217;s useful, but we could just as easily put anything there &#8211; an atom, a random string, whatever.  </p>
<p>There&#8217;s a lot of options within there, so you might want to check the docs.</p>
<p>I STRONGLY recommend using <a href="http://erlide.sourceforge.net/">Erlide </a>for your development.  It auto compiles and gives you an interactive shell right in the editor.  It made Erlang development so much easier.  It&#8217;ll also create the stubs for the other functions you&#8217;ll need (terminate, code change, handle_info, handle_cast).</p>
<p>FYI, If you get an error like this:</p>
<blockquote><p>** exception exit: {noproc,{gen_server,call,[calculator,{add,1}]}}<br />
     in function  gen_server:call/2</p></blockquote>
<p>It&#8217;s because your gen_server isn&#8217;t actually running.  Make sure you start it before you test with </p>
<blockquote><p>calculator:start().</p></blockquote>
<p>Hopefully this is enough to get you started.  Read up more in the <a href="http://erlang.org/doc/man/gen_server.html">official Erlang docs for gen_server</a>.  Scroll to the bottom to find the specifications on what functions you must implement, and what they need to return.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2009/04/erlang-understanding-gen_server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
