aboutsummaryrefslogtreecommitdiff
path: root/learn-you-some-erlang/m8ball/src
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2024-02-23 07:08:18 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2024-02-23 07:08:18 +0100
commit5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e (patch)
tree982ca2e7f9ac4e8c350dfb5c4f60bcfdfff5afaf /learn-you-some-erlang/m8ball/src
parent05ae56e5e89abf2993f84e6d52b250131f247c35 (diff)
downloaderlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.gz
erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.bz2
erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.xz
erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.zip
wip
Diffstat (limited to 'learn-you-some-erlang/m8ball/src')
-rw-r--r--learn-you-some-erlang/m8ball/src/m8ball.erl24
-rw-r--r--learn-you-some-erlang/m8ball/src/m8ball_server.erl46
-rw-r--r--learn-you-some-erlang/m8ball/src/m8ball_sup.erl16
3 files changed, 86 insertions, 0 deletions
diff --git a/learn-you-some-erlang/m8ball/src/m8ball.erl b/learn-you-some-erlang/m8ball/src/m8ball.erl
new file mode 100644
index 0000000..041601b
--- /dev/null
+++ b/learn-you-some-erlang/m8ball/src/m8ball.erl
@@ -0,0 +1,24 @@
+-module(m8ball).
+-behaviour(application).
+-export([start/2, stop/1]).
+-export([ask/1]).
+
+%%%%%%%%%%%%%%%%%
+%%% CALLBACKS %%%
+%%%%%%%%%%%%%%%%%
+
+%% start({failover, Node}, Args) is only called
+%% when a start_phase key is defined.
+start(normal, []) ->
+ m8ball_sup:start_link();
+start({takeover, _OtherNode}, []) ->
+ m8ball_sup:start_link().
+
+stop(_State) ->
+ ok.
+
+%%%%%%%%%%%%%%%%%
+%%% INTERFACE %%%
+%%%%%%%%%%%%%%%%%
+ask(Question) ->
+ m8ball_server:ask(Question).
diff --git a/learn-you-some-erlang/m8ball/src/m8ball_server.erl b/learn-you-some-erlang/m8ball/src/m8ball_server.erl
new file mode 100644
index 0000000..4e821ad
--- /dev/null
+++ b/learn-you-some-erlang/m8ball/src/m8ball_server.erl
@@ -0,0 +1,46 @@
+-module(m8ball_server).
+-behaviour(gen_server).
+-export([start_link/0, stop/0, ask/1]).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ code_change/3, terminate/2]).
+
+%%%%%%%%%%%%%%%%%
+%%% INTERFACE %%%
+%%%%%%%%%%%%%%%%%
+start_link() ->
+ gen_server:start_link({global, ?MODULE}, ?MODULE, [], []).
+
+stop() ->
+ gen_server:call({global, ?MODULE}, stop).
+
+ask(_Question) -> % the question doesn't matter!
+ gen_server:call({global, ?MODULE}, question).
+
+%%%%%%%%%%%%%%%%%
+%%% CALLBACKS %%%
+%%%%%%%%%%%%%%%%%
+init([]) ->
+ <<A:32, B:32, C:32>> = crypto:rand_bytes(12),
+ random:seed(A,B,C),
+ {ok, []}.
+
+handle_call(question, _From, State) ->
+ {ok, Answers} = application:get_env(m8ball, answers),
+ Answer = element(random:uniform(tuple_size(Answers)), Answers),
+ {reply, Answer, State};
+handle_call(stop, _From, State) ->
+ {stop, normal, ok, State};
+handle_call(_Call, _From, State) ->
+ {noreply, State}.
+
+handle_cast(_Cast, State) ->
+ {noreply, State}.
+
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+terminate(_Reason, _State) ->
+ ok.
diff --git a/learn-you-some-erlang/m8ball/src/m8ball_sup.erl b/learn-you-some-erlang/m8ball/src/m8ball_sup.erl
new file mode 100644
index 0000000..ca5e426
--- /dev/null
+++ b/learn-you-some-erlang/m8ball/src/m8ball_sup.erl
@@ -0,0 +1,16 @@
+-module(m8ball_sup).
+-behaviour(supervisor).
+-export([start_link/0, init/1]).
+
+start_link() ->
+ supervisor:start_link({global,?MODULE}, ?MODULE, []).
+
+init([]) ->
+ {ok, {{one_for_one, 1, 10},
+ [{m8ball,
+ {m8ball_server, start_link, []},
+ permanent,
+ 5000,
+ worker,
+ [m8ball_server]
+ }]}}.