Skip to content

Instantly share code, notes, and snippets.

@2garryn
Last active December 24, 2015 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2garryn/6745209 to your computer and use it in GitHub Desktop.
Save 2garryn/6745209 to your computer and use it in GitHub Desktop.
-module(example).
-behaviour(gen_server).
%% API
-export([start_link/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-export([set_coordinates/2, get_coordinates/1]).
start_link() ->
gen_server:start_link(?MODULE, [], []).
set_coordinates(Pid, XY = {X, Y}) when is_integer(X), is_integer(Y) ->
gen_server:cast(Pid, {set_coors, XY}),
ok.
get_coordinates(Pid) ->
gen_server:call(Pid, get_coors).
init([]) ->
{ok, {0,0}}.
handle_call(get_coors, _From, Position) ->
{reply, Position, Position}.
handle_cast({set_coors, NewPosition}, _Position) ->
{noreply, NewPosition}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment