Current section
Files
Jump to
Current section
Files
src/ur/mod/clock.lfe
(defmodule ur.mod.clock
(behaviour gen_server)
;; gen_server implementation
(export
(start_link 1)
(stop 0))
;; callback implementation
(export
(init 1)
(handle_call 3)
(handle_cast 2)
(handle_info 2)
(terminate 2)
(code_change 3))
;; underack module API
(export
(default-opts 0)
(pid 0)
(echo 1)))
(include-lib "logjam/include/logjam.hrl")
;;; ----------------
;;; config functions
;;; ----------------
(defun SERVER () (MODULE))
(defun NAME () "underack clock module")
(defun initial-state () '#())
(defun genserver-opts () '())
(defun unknown-command () #(error "Unknown command."))
;;; -------------------------
;;; gen_server implementation
;;; -------------------------
(defun start_link (opts)
(log-info "Starting ~s ..." (list (NAME)))
(gen_server:start_link `#(local ,(SERVER))
(MODULE)
(initial-state)
(genserver-opts)))
(defun stop ()
(gen_server:call (SERVER) 'stop))
;;; -----------------------
;;; callback implementation
;;; -----------------------
(defun init (state)
(log-debug "Initialising ~s ..." `(,(NAME)))
`#(ok ,state))
(defun handle_cast
((msg state)
(log-info "Got unexpected cast message: ~p" (list msg))
`#(noreply ,state)))
(defun handle_call
(('stop _from state)
(log-notice "Stopping ~s ..." (list (NAME)))
`#(stop shutdown ok ,state))
((`#(echo ,msg) _from state)
`#(reply ,msg ,state))
((message _from state)
`#(reply ,(unknown-command) ,state)))
(defun handle_info
((`#(EXIT ,_from normal) state)
(logger:info "~s is exiting (normal)." (list (NAME)))
`#(noreply ,state))
((`#(EXIT ,_from shutdown) state)
(logger:info "~s is exiting (shutdown)." (list (NAME)))
`#(noreply ,state))
((`#(EXIT ,pid ,reason) state)
(io:format "Process ~p exited! (Reason: ~p)~n" `(,pid ,reason))
`#(noreply ,state))
((_msg state)
`#(noreply ,state)))
(defun terminate (_reason _state)
(log-notice "Terminating ~s ..." (list (NAME)))
'ok)
(defun code_change (_old-version state _extra)
`#(ok ,state))
;;; -------------------
;;; underack module API
;;; -------------------
(defun default-opts ()
#m(bpm 120))
(defun pid ()
(erlang:whereis (SERVER)))
(defun echo (msg)
(gen_server:call (SERVER) `#(echo ,msg)))