Skip to content

Instantly share code, notes, and snippets.

@Kintull
Created November 28, 2017 23:41
Show Gist options
  • Save Kintull/b728367ddeab86b2ad98a041e73911b7 to your computer and use it in GitHub Desktop.
Save Kintull/b728367ddeab86b2ad98a041e73911b7 to your computer and use it in GitHub Desktop.
Simple server that implements LIFO stack
%% @author kintull
%% @doc This module implements simple LIFO stack
%% To test the module compile it and run server:test()
-module(server).
-behavior(gen_server).
%%------------------------------
%% API
%%------------------------------
-export([start_link/0, pop_data/1, pick_data/1,
push_data/2, stop_server/1, print_queue/1,
test/0]).
%%------------------------------
%% gen_server callbacks
%%------------------------------
-export([init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3]).
%%------------------------------
%% Records
%%------------------------------
-record(state, {queue = []}).
%%------------------------------
%% gen_server callbacks
%%------------------------------
init([])->
io:format("Start server~n"),
{ok, #state{}}.
start_link() -> gen_server:start_link(?MODULE, [], []).
handle_info(_, Status) ->
io:format("Unknown message received~n"),
{noreply, Status}.
code_change(_Old, State, _Extra) ->
{ok, State}.
terminate(_Reason, _State) ->
io:format("Shutting down~n"),
ok.
%%------------------------------
%% handle_cast
%%------------------------------
handle_cast(print_queue, State = #state{queue = Queue}) ->
io:format("Queue: ~p~n",[Queue]),
{noreply, State};
handle_cast({push_data, Data}, #state{queue = Queue}) ->
{noreply, #state{queue = [Data|Queue]}};
handle_cast(_, State) ->
io:format("Unknown cast"),
{noreply, State}.
%%------------------------------
%% handle_call
%%------------------------------
handle_call(pop_data, _From, #state{queue = []}) ->
{reply, nok, #state{queue = []}};
handle_call(pop_data, _From, #state{queue = Queue}) ->
{reply, hd(Queue), #state{queue = tl(Queue)}};
handle_call(pick_data, _From, State = #state{queue = Queue}) ->
case Queue of
[] ->
Result = nok;
_ ->
Result = hd(Queue)
end,
{reply, Result, State};
handle_call(terminate, _From, State) ->
{stop, normal, ok, State};
handle_call(Call, _From, State) ->
io:format("Unknown call: ~p~n",[Call]),
{reply, unknown, State}.
%%------------------------------
%% API
%%------------------------------
pop_data(Pid) ->
gen_server:call(Pid, pop_data).
pick_data(Pid) ->
gen_server:call(Pid, pick_data).
push_data(Pid, Data) ->
gen_server:cast(Pid, {push_data, Data}).
stop_server(Pid) ->
gen_server:call(Pid, terminate).
print_queue(Pid) ->
gen_server:cast(Pid, print_queue).
test()->
{ok, Pid} = start_link(),
push_data(Pid, data1),
print_queue(Pid),
push_data(Pid, data2),
print_queue(Pid),
pick_data(Pid),
print_queue(Pid),
pop_data(Pid),
print_queue(Pid),
pop_data(Pid),
print_queue(Pid),
pop_data(Pid),
print_queue(Pid),
stop_server(Pid).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment