Last active
August 29, 2015 14:19
-
-
Save mopemope/07747075c9edb5dd6006 to your computer and use it in GitHub Desktop.
band supervisor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmodule band-supervisor | |
(export all) | |
(behavior 'supervisor)) | |
(defun start-link (type) | |
(supervisor:start_link `#(local ,(MODULE)) (MODULE) type)) | |
(defun start_link (type) | |
(start-link type)) | |
(defun init | |
(('lenient) | |
(init #(one_for_one 3 60))) | |
(('angry) | |
(init #(rest_for_one 2 60))) | |
(('jerk) | |
(init #(one_for_all 1 60))) | |
(('jamband) | |
`#(ok #(#(simple_one_for_one 3 60) | |
(#(jam_musian | |
#(musicians start-link ()) | |
temporary 1000 worker (musicians)))))) | |
((`#(,restart-strategy ,max-restart ,max-time)) | |
`#(ok #(#(,restart-strategy ,max-restart ,max-time) | |
(#(singer | |
#(musicians start-link (singer good)) | |
permanent 1000 worker (musicians)) | |
#(bass | |
#(musicians start-link (bass good)) | |
temporary 1000 worker (musicians)) | |
#(drum | |
#(musicians start-link (drum bad)) | |
transient 1000 worker (musicians)) | |
#(keytar | |
#(musicians start-link (keytar good)) | |
transient 1000 worker (musicians))))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmodule musicians | |
(export all) | |
(behavior 'gen_server)) | |
(defrecord state | |
(name "") | |
role | |
(skill 'good)) | |
(defmacro delay () 750) | |
(defun start-link (role skill) | |
(gen_server:start_link `#(local ,role) (MODULE) `(,role ,skill) '())) | |
(defun start_link (role skill) | |
(start-link role skill)) | |
(defun stop (role) | |
(gen_server:stop `#(,role stop))) | |
;; gen_server callback | |
(defun init | |
((`(,role ,skill)) | |
(process_flag 'trap_exit 'true) | |
(random:seed (now)) | |
(let ((time-to-play (random:uniform 3000)) | |
(name (pick-name)) | |
(str-role (atom_to_list role))) | |
(io:format "time ~p~n" `(,time-to-play)) | |
(io:format "Musician ~s, playing the ~s entered the room~n" `(,name ,role)) | |
`#(ok ,(make-state name name role str-role skill skill) ,time-to-play)))) | |
(defun pick-name () | |
(string:join `(,(lists:nth (random:uniform 10) (firstnames)) | |
,(lists:nth (random:uniform 10) (lastnames))) " ")) | |
(defun firstnames () | |
`("Valerie" "Arnold" "Carlos" "Dorothy" "Keesha" | |
"Phoebe" "Ralphie" "Tim" "Wanda" "Janet")) | |
(defun lastnames () | |
`("Frizzle" "Perlstein" "Ramon" "Ann" "Franklin" | |
"Terese" "Tennelli" "Jamal" "Li" "Perlstein")) | |
(defun handle_call | |
(('stop _from (= (match-state) s)) | |
`#(stop normal ok ,s)) | |
((_message _from s) | |
`#(noreply ,s ,(delay)))) | |
(defun handle_cast (_message s) | |
`#(noreply ,s ,(delay))) | |
(defun handle_info | |
(('timeout (= (match-state name n skill 'good) s)) | |
(io:format "~s produced sound!~n" `(,n)) | |
`#(noreply ,s ,(delay))) | |
(('timeout (= (match-state name n skill 'bad) s)) | |
(case (random:uniform 5) | |
(1 | |
(io:format "~s played a false note. Uh oh~n" `(,n)) | |
`#(stop bad-note ,s)) | |
(_ | |
(io:format "~s produced sound!~n" `(,n)) | |
`#(noreply ,s ,(delay))))) | |
((_message s) | |
`#(noreply ,s ,(delay)))) | |
(defun code_change (_oldvsn state _extra) | |
`#(ok ,state)) | |
(defun terminate | |
(('normal s) | |
(io:format "~s left the room (~s)~n" `(,(state-name s) ,(state-role s)))) | |
(('bad-note s) | |
(io:format "~s sucks! kicked that member out of the band! (~s)~n" `(,(state-name s) ,(state-role s)))) | |
(('shutdown s) | |
(io:format "The manager is mad and fired the whole band! ~s (~s) just got back to playing in the subway~n" `(,(state-name s) ,(state-role s)))) | |
((_reason s) | |
(io:format "~s has been kicked out (~s)~n" `(,(state-name s) ,(state-role s))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment