2009-05-25 14:48:43 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 Scalable Solutions.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-10-08 19:01:04 +02:00
|
|
|
package se.scalablesolutions.akka.dispatch
|
2009-05-25 14:48:43 +02:00
|
|
|
|
2009-07-28 10:45:41 +02:00
|
|
|
import java.util.List
|
2009-10-06 00:07:27 +02:00
|
|
|
|
|
|
|
|
import se.scalablesolutions.akka.util.HashCode
|
|
|
|
|
import se.scalablesolutions.akka.stm.Transaction
|
|
|
|
|
import se.scalablesolutions.akka.actor.Actor
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
2009-07-06 23:45:15 +02:00
|
|
|
|
|
|
|
|
trait MessageQueue {
|
|
|
|
|
def append(handle: MessageInvocation)
|
|
|
|
|
def prepend(handle: MessageInvocation)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait MessageInvoker {
|
|
|
|
|
def invoke(message: MessageInvocation)
|
2009-06-05 22:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
2009-05-25 14:48:43 +02:00
|
|
|
trait MessageDispatcher {
|
2009-12-11 06:44:59 +01:00
|
|
|
def dispatch(invocation: MessageInvocation)
|
2009-07-06 23:45:15 +02:00
|
|
|
def registerHandler(key: AnyRef, handler: MessageInvoker)
|
2009-05-25 14:48:43 +02:00
|
|
|
def unregisterHandler(key: AnyRef)
|
2009-11-25 14:51:05 +01:00
|
|
|
def canBeShutDown: Boolean
|
2009-06-05 22:08:53 +02:00
|
|
|
def start
|
2009-05-25 14:48:43 +02:00
|
|
|
def shutdown
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait MessageDemultiplexer {
|
|
|
|
|
def select
|
2009-07-28 10:45:41 +02:00
|
|
|
def acquireSelectedInvocations: List[MessageInvocation]
|
|
|
|
|
def releaseSelectedInvocations
|
2009-05-25 14:48:43 +02:00
|
|
|
def wakeUp
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
class MessageInvocation(val receiver: Actor,
|
2009-12-08 08:51:10 +01:00
|
|
|
val message: Any,
|
2009-07-06 23:45:15 +02:00
|
|
|
val future: Option[CompletableFutureResult],
|
2009-11-21 19:34:42 +01:00
|
|
|
val sender: Option[Actor],
|
2009-07-06 23:45:15 +02:00
|
|
|
val tx: Option[Transaction]) {
|
2009-10-06 00:07:27 +02:00
|
|
|
if (receiver == null) throw new IllegalArgumentException("receiver is null")
|
|
|
|
|
if (message == null) throw new IllegalArgumentException("message is null")
|
2009-05-25 14:48:43 +02:00
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
private [akka] val nrOfDeliveryAttempts = new AtomicInteger(0)
|
|
|
|
|
|
|
|
|
|
def send = synchronized {
|
2009-12-11 06:44:59 +01:00
|
|
|
receiver.dispatcher.dispatch(this)
|
2009-10-06 00:07:27 +02:00
|
|
|
nrOfDeliveryAttempts.incrementAndGet
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def hashCode(): Int = synchronized {
|
2009-05-25 15:18:18 +02:00
|
|
|
var result = HashCode.SEED
|
2009-10-06 00:07:27 +02:00
|
|
|
result = HashCode.hash(result, receiver)
|
2009-05-25 15:18:18 +02:00
|
|
|
result = HashCode.hash(result, message)
|
|
|
|
|
result
|
2009-05-25 14:48:43 +02:00
|
|
|
}
|
2009-05-25 15:18:18 +02:00
|
|
|
|
2009-10-06 00:07:27 +02:00
|
|
|
override def equals(that: Any): Boolean = synchronized {
|
2009-05-25 15:18:18 +02:00
|
|
|
that != null &&
|
2009-07-06 23:45:15 +02:00
|
|
|
that.isInstanceOf[MessageInvocation] &&
|
2009-10-06 00:07:27 +02:00
|
|
|
that.asInstanceOf[MessageInvocation].receiver == receiver &&
|
2009-07-28 17:48:11 +02:00
|
|
|
that.asInstanceOf[MessageInvocation].message == message
|
2009-10-06 00:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def toString(): String = synchronized {
|
2009-12-08 16:17:22 +01:00
|
|
|
"MessageInvocation[" +
|
|
|
|
|
"\n\tmessage = " + message +
|
|
|
|
|
"\n\treceiver = " + receiver +
|
|
|
|
|
"\n\tsender = " + sender +
|
|
|
|
|
"\n\tfuture = " + future +
|
|
|
|
|
"\n\ttx = " + tx +
|
|
|
|
|
"\n]"
|
2009-10-06 00:07:27 +02:00
|
|
|
}
|
2009-05-25 14:48:43 +02:00
|
|
|
}
|