I am trying to implement a counter using the Erlang/OTP Client Server. In the following code I can set a count to count up from, but the incr() function does not work. Could someone please explain to me what I am doing wrong?
-module(counter2).
-behaviour(gen_server).
-export([start/1, stop/0, incr/0, get_count/0]).
-export([init/1, handle_call/3, handle_cast/2, terminate/2, handle_info/2]).
% Client Functions
start(Args) ->
gen_server:start_link({local, counter2}, counter2, [Args], []).
stop() ->
gen_server:cast(conter2, stop).
incr() ->
io:format("incr called: ~n"),
gen_server:call(counter2, {incr, self()}).
get_count() ->
gen_server:call(counter2, {get_count, self()}).
% Callback functions
init(Args) ->
Count = Args,
{ok, Count}.
handle_call({incr, Pid}, _From, Count) ->
{NewCount, Reply} = incr(Count, Pid),
io:format("In incr Reply: ~p~n", [Reply]),
{reply, Reply, NewCount};
handle_call({get_count, Pid}, _From, Count) ->
{NewCount, Reply} = get_count(Count, Pid),
{reply, Reply, NewCount}.
handle_cast(stop, Count) ->
{stop, normal, Count}.
handle_info(_Msg, Count) ->
{noreply, Count}.
terminate(_Reason, _Count) ->
ok.
incr(Count, _Pid) ->
io:format("In incr:~n"),
NewCount = (Count + 1),
{ok, NewCount}.
get_count(Count, _Pid) ->
{ok, Count}.
The counter function incr() should count up from a set value and return the value to the client request.