|
| 1 | +%%%------------------------------------------------------------------- |
| 2 | +%%% @author Adinarayana |
| 3 | +%%% @copyright (C) 2019, <COMPANY> |
| 4 | +%%% @doc |
| 5 | +%%% |
| 6 | +%%% @end |
| 7 | +%%% Created : 27. Apr 2019 15:48 |
| 8 | +%%%------------------------------------------------------------------- |
| 9 | +-module(febonnacy_server). |
| 10 | +-author("Adinarayana"). |
| 11 | + |
| 12 | +-behaviour(gen_server). |
| 13 | + |
| 14 | +%% API |
| 15 | +-export([start_link/0]). |
| 16 | + |
| 17 | +%% gen_server callbacks |
| 18 | +-export([init/1, |
| 19 | + handle_call/3, |
| 20 | + handle_cast/2, |
| 21 | + handle_info/2, |
| 22 | + terminate/2, |
| 23 | + code_change/3]). |
| 24 | + |
| 25 | +-define(SERVER, ?MODULE). |
| 26 | + |
| 27 | +-record(state, {}). |
| 28 | + |
| 29 | +%%%=================================================================== |
| 30 | +%%% API |
| 31 | +%%%=================================================================== |
| 32 | + |
| 33 | +%%-------------------------------------------------------------------- |
| 34 | +%% @doc |
| 35 | +%% Starts the server |
| 36 | +%% |
| 37 | +%% @end |
| 38 | +%%-------------------------------------------------------------------- |
| 39 | +-spec(start_link() -> |
| 40 | + {ok, Pid :: pid()} | ignore | {error, Reason :: term()}). |
| 41 | +start_link() -> |
| 42 | + gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). |
| 43 | + |
| 44 | +%%%=================================================================== |
| 45 | +%%% gen_server callbacks |
| 46 | +%%%=================================================================== |
| 47 | + |
| 48 | +%%-------------------------------------------------------------------- |
| 49 | +%% @private |
| 50 | +%% @doc |
| 51 | +%% Initializes the server |
| 52 | +%% |
| 53 | +%% @spec init(Args) -> {ok, State} | |
| 54 | +%% {ok, State, Timeout} | |
| 55 | +%% ignore | |
| 56 | +%% {stop, Reason} |
| 57 | +%% @end |
| 58 | +%%-------------------------------------------------------------------- |
| 59 | +-spec(init(Args :: term()) -> |
| 60 | + {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} | |
| 61 | + {stop, Reason :: term()} | ignore). |
| 62 | +init([]) -> |
| 63 | + ets:new(feb_result, [set,public, named_table]), |
| 64 | + {ok, #state{}}. |
| 65 | + |
| 66 | +%%-------------------------------------------------------------------- |
| 67 | +%% @private |
| 68 | +%% @doc |
| 69 | +%% Handling call messages |
| 70 | +%% |
| 71 | +%% @end |
| 72 | +%%-------------------------------------------------------------------- |
| 73 | +-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()}, |
| 74 | + State :: #state{}) -> |
| 75 | + {reply, Reply :: term(), NewState :: #state{}} | |
| 76 | + {reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} | |
| 77 | + {noreply, NewState :: #state{}} | |
| 78 | + {noreply, NewState :: #state{}, timeout() | hibernate} | |
| 79 | + {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} | |
| 80 | + {stop, Reason :: term(), NewState :: #state{}}). |
| 81 | +handle_call(Request, _From, State) -> |
| 82 | + Result = compute_fibonacci(Request), |
| 83 | + {reply, Result, State}. |
| 84 | + |
| 85 | +%%-------------------------------------------------------------------- |
| 86 | +%% @private |
| 87 | +%% @doc |
| 88 | +%% Handling cast messages |
| 89 | +%% |
| 90 | +%% @end |
| 91 | +%%-------------------------------------------------------------------- |
| 92 | +-spec(handle_cast(Request :: term(), State :: #state{}) -> |
| 93 | + {noreply, NewState :: #state{}} | |
| 94 | + {noreply, NewState :: #state{}, timeout() | hibernate} | |
| 95 | + {stop, Reason :: term(), NewState :: #state{}}). |
| 96 | +handle_cast(_Request, State) -> |
| 97 | + {noreply, State}. |
| 98 | + |
| 99 | +%%-------------------------------------------------------------------- |
| 100 | +%% @private |
| 101 | +%% @doc |
| 102 | +%% Handling all non call/cast messages |
| 103 | +%% |
| 104 | +%% @spec handle_info(Info, State) -> {noreply, State} | |
| 105 | +%% {noreply, State, Timeout} | |
| 106 | +%% {stop, Reason, State} |
| 107 | +%% @end |
| 108 | +%%-------------------------------------------------------------------- |
| 109 | +-spec(handle_info(Info :: timeout() | term(), State :: #state{}) -> |
| 110 | + {noreply, NewState :: #state{}} | |
| 111 | + {noreply, NewState :: #state{}, timeout() | hibernate} | |
| 112 | + {stop, Reason :: term(), NewState :: #state{}}). |
| 113 | +handle_info(_Info, State) -> |
| 114 | + {noreply, State}. |
| 115 | + |
| 116 | +%%-------------------------------------------------------------------- |
| 117 | +%% @private |
| 118 | +%% @doc |
| 119 | +%% This function is called by a gen_server when it is about to |
| 120 | +%% terminate. It should be the opposite of Module:init/1 and do any |
| 121 | +%% necessary cleaning up. When it returns, the gen_server terminates |
| 122 | +%% with Reason. The return value is ignored. |
| 123 | +%% |
| 124 | +%% @spec terminate(Reason, State) -> void() |
| 125 | +%% @end |
| 126 | +%%-------------------------------------------------------------------- |
| 127 | +-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), |
| 128 | + State :: #state{}) -> term()). |
| 129 | +terminate(_Reason, _State) -> |
| 130 | + ok. |
| 131 | + |
| 132 | +%%-------------------------------------------------------------------- |
| 133 | +%% @private |
| 134 | +%% @doc |
| 135 | +%% Convert process state when code is changed |
| 136 | +%% |
| 137 | +%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState} |
| 138 | +%% @end |
| 139 | +%%-------------------------------------------------------------------- |
| 140 | +-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{}, |
| 141 | + Extra :: term()) -> |
| 142 | + {ok, NewState :: #state{}} | {error, Reason :: term()}). |
| 143 | +code_change(_OldVsn, State, _Extra) -> |
| 144 | + {ok, State}. |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +compute_fibonacci(0) -> 0; |
| 149 | +compute_fibonacci(1) -> 1; |
| 150 | +compute_fibonacci(N) -> |
| 151 | + Num2 = case ets:lookup(feb_result,N-2) of |
| 152 | + [] -> |
| 153 | + Second_Prev = compute_fibonacci(N-2), |
| 154 | + ets:insert(feb_result,{N-2, Second_Prev}), |
| 155 | + Second_Prev; |
| 156 | + [{_,Second_Prev}] -> Second_Prev |
| 157 | + end, |
| 158 | + Num1 = case ets:lookup(feb_result,N-1) of |
| 159 | + [] -> |
| 160 | + First_Prev = compute_fibonacci(N-1), |
| 161 | + ets:insert(feb_result,{N-1, First_Prev}), |
| 162 | + First_Prev; |
| 163 | + [{_,First_Prev}] -> First_Prev |
| 164 | + end, |
| 165 | + Num2 + Num1. |
| 166 | +%%%=================================================================== |
| 167 | +%%% Internal functions |
| 168 | +%%%=================================================================== |
0 commit comments