Using Scala 2.8 RC1 or newer, what is the best (easiest and/or most direct) method to \"peek\" at the waiting messages in an actor\'s mailbox (from within the same actor\'s
This sounds like a dangerous operation in general, since if there are critical messages, the actor processing them might check and find none, but then before exiting might be given another from some other thread.
If you know for certain that this can't happen, and you don't need lots of incredibly fast message switches, I'd probably write a guard-actor that counts and keeps track of critical messages but otherwise just passes them on to another actor for handling.
If you don't want to do that, then keep in mind that the details of the internals should change and you may have to go through the source code for Actor, Reactor, MessageQueue, etc. in order to get what you want. For now, something like this should work (warning, untested):
package scala.actors
package myveryownstuff
trait CriticalActor extends Actor {
def criticalAwaits(p: Any => Boolean) = {
synchronized {
drainSendBuffer(mailbox)
mailbox.get(0)(p).isDefined
}
}
}
Note that we have to place the extended trait in the scala.actors package, because all the mailbox internals are declared private to the scala.actors package. (This is a good warning that you should be careful before messing with the internals.) We then add a new method that takes a function that can test for a critical message and look for it using the built in mailbox.get(n)
method that returns the n
th message fulfilling some predicate.