Skip to content

Commit

Permalink
dhcp.node_server: support async message handlers.
Browse files Browse the repository at this point in the history
Use Promesa when binding the result of the message handler. This will
enable await behavior on the result if it is a Promise (otherwise the
prior sync behavior will continue to work).
  • Loading branch information
kanaka committed May 8, 2024
1 parent a8b4217 commit 57b5dac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
3 changes: 2 additions & 1 deletion shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
["src/" "test/"]

:dependencies
[[cljs-bean "1.9.0"]]
[[cljs-bean "1.9.0"]
[funcool/promesa "11.0.678"]]

:builds
{:simple-client
Expand Down
19 changes: 10 additions & 9 deletions src/dhcp/node_server.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

(ns dhcp.node-server
"Framework for creating DHCP server implementations."
(:require [clojure.string :as string]
(:require [promesa.core :as P]
[clojure.string :as string]
[cljs-bean.core :refer [->clj]]
[protocol.socket :as socket]
[dhcp.core :as dhcp]
Expand All @@ -22,16 +23,16 @@
get a response map, and then write/encode the response and send it
via `sock`."
[{:keys [log-msg sock message-handler disable-broadcast] :as cfg} buf rinfo]
(let [msg-map (dhcp/read-dhcp buf)
msg-type (:opt/msg-type msg-map)]
(P/let [msg-map (dhcp/read-dhcp buf)
msg-type (:opt/msg-type msg-map)]
(if (not msg-type)
(log-msg :error (str "Received invalid msg from " rinfo))
(let [resp-addr (if (and (not disable-broadcast)
(dhcp/MSG-TYPE-BCAST-LOOKUP msg-type))
"255.255.255.255"
(:address rinfo))
_ (log-msg :recv msg-map resp-addr)
resp-msg-map (message-handler cfg msg-map)]
(P/let [resp-addr (if (and (not disable-broadcast)
(dhcp/MSG-TYPE-BCAST-LOOKUP msg-type))
"255.255.255.255"
(:address rinfo))
_ (log-msg :recv msg-map resp-addr)
resp-msg-map (message-handler cfg msg-map)]
(if resp-msg-map
(send-message cfg resp-msg-map resp-addr (:port rinfo))
(log-msg :error "No msg-map from message-handler, ignoring"))))))
Expand Down

0 comments on commit 57b5dac

Please sign in to comment.