Version 4, last updated by dpp at April 13, 2010 UTC

One of the things that differentials Stambecco from other Actor-style systems is an explicit, type-safe mechanism for asking a Worker for status without being or simulating a Worker.

From outside the Worker system:

OutsideOfWorker on inst ask MooFu("42") complete {foo => dog = foo}

This will send the MooFu message to the inst worker and execute the complete function when the Worker answers.

MooFu is defined:

case class MooFu(str: String) extends SimpleMsg with MsgWithResponse[QLong]

And the type of the function passed to complete is Box[QLong] => Unit.

By default, if the question is not answered within 10 seconds, the function will be applied to a Failure Box.

You can change the timeout to 20 milliseconds:

      OutsideOfWorker on inst withTimeout 20.millis ask
      MooDelay("42") complete {
        case ParamFailure(_, _, _, TimeExpired(_)) => gotTimeout = true
      }

Different actions can be taken on a question.  One can ask for a Future.  A Future is a Responder that will block until the answer has been provided.  This allows you to conveniently block the current thread until you get an answer and then continue to process your method:

      var dog: Long = 0

for {
        response <- OutsideOfWorker on inst ask MooFu2("42") future;
        value <- response
      } dog = value

println("We got the answer: "+dog)

You can also ask for a continuation.  A continuation will not block the current thread if an answer is not provided.  A continuation will continue processing on another thread.  This is useful if you're asking for answers from more than 1 Worker and can resume processing on another thread (e.g., using continuations in a web server):

        for {
          resp <- OutsideOfWorker on inst ask MooFu2("42") continue;
          resp2 <- OutsideOfWorker on inst ask MooFu2("42") continue;
          val1 <- resp
          val2 <- resp2
        } {
          println("val1 == val2? "+(val1 == val2))
        }

println("I'll probably be printed before the message in the for comprehension")

Workers can ask questions to each other.  The syntax is nearly identical... just omit the "OutsideOfWorker" part:

        for {
          resp <- on inst ask MooFu2("42") continue;
          resp2 <- on inst ask MooFu2("42") continue;
          val1 <- resp
          val2 <- resp2
        } {
          println("val1 == val2? "+(val1 == val2))
        }

It's advisable not to use Futures within Workers as Futures block threads which can cause thread starvation.