In a library for communicating with networked programs and devices
using Open Sound Control (OSC) messages, I use promises in combination
with futures to provide synchronous callbacks with an optional
timeout.  For example, you might send a status request message to a
device and you expect an immediate response as long as the device is
live.  If the device is off or it has crashed then you don't want to
hang forever waiting for a response.  The promise/deliver pair acts as
the communication mechanism between a handler that gets called
asynchronously by another thread listening on a socket and the
original thread that is waiting for the response.

In the function below osc-handle registers a handler function for a
specific OSC path, which acts as a basic message dispatch.  All the
handler does is deliver the promise and remove itself.  The future is
used because they have a timeout feature when using the .get method.

(defn osc-recv
  "Receive a single message on an osc path (node) with an optional
timeout."
  [peer path & [timeout]]
  (let [p (promise)]
    (osc-handle peer path (fn [msg]
                           (deliver p msg)
                            (osc-remove-handler)))
    (let [res (try
                (if timeout
                  (.get (future @p) timeout TimeUnit/MILLISECONDS)
                  @p)
                (catch TimeoutException t
                  nil))]
      res)))


The library is available on github, in case anyone wants to talk OSC:
http://github.com/rosejn/osc-clj

-Jeff

On Jan 21, 12:19 pm, Baishampayan Ghose <b.gh...@ocricket.com> wrote:
> Hello,
>
> I am trying to understand the use-cases of the new promise/deliver
> feature in Clojure. I have tried using them, and they seem to be
> pretty straight-forward to use, but unfortunately I haven't been able
> to understand its use-cases.
>
> It would be great if someone pointed out some example usage of 
> promise/deliver.
>
> Regards,
> BG
>
> --
> Baishampayan Ghose

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to