On Thu, Oct 8, 2009 at 1:06 PM, Laurent PETIT <laurent.pe...@gmail.com>wrote:

> So I don't think you need this message-queue at all (or maybe I haven't
> understood what it is or I'm mislead by its name), send directly your order
> to the agent as what you want to change in its state.


You might be able to do better than that, and dispense entirely with the
separate polling thread.

Break the state involved in the polling into a variable and a constant part.
Constant might be a pocket on a socket on a port, variable a
polling/non-polling flag and some other stuff. Wrap the latter in a
structure such as a list or a map and stuff it in an agent. Create some
functions that close over the former, some to change the agent state and one
to do the polling.

The polling one should check the are-we-polling flag of the state, return
the state if we're not polling, and otherwise poll, sleep however-long, and
(send-off *agent* poll), then return the state. The one to turn on polling
also needs to end with (send-off *agent* poll) and then return the modified
version of the state (with the we're-polling flag assoc'd true). The effect
of all this should be:

If the agent is told to start polling, it gets enqueued with a poll
send-off, and immediately after the last send returns and sets the new agent
state, the poll function is then executed. Every however long this repeats.

If a state change is sent to a polling agent, it probably arrives during the
poll function's sleep. This state change message is thus queued by the agent
ahead of the poll message it sends itself at the end of the sleep. So the
state change is done next, then the poll, and for that poll the state change
should be in effect.

If the state change included stop-polling, then since the poll function
checks the we're-polling flag and aborts if we're not polling, that next
poll is a no-op; in particular, the agent does not send itself another poll
message, and will be completely quiescent until sent a start-polling
message.

--~--~---------~--~----~------------~-------~--~----~
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