package com.droidkit.actors; import com.droidkit.actors.mailbox.Mailbox; import com.droidkit.actors.messages.DeadLetter; import com.droidkit.actors.messages.NamedMessage; import com.droidkit.actors.tasks.*; import com.droidkit.actors.tasks.messages.TaskError; import com.droidkit.actors.tasks.messages.TaskResult; import com.droidkit.actors.tasks.messages.TaskTimeout; import java.lang.reflect.Array; import java.util.UUID; /** * Actor object * * @author Stepan Ex3NDR Korshakov (me@ex3ndr.com) */ public class Actor { private UUID uuid; private String path; private ActorContext context; private Mailbox mailbox; private ActorAskImpl askPattern; public Actor() { } /** *
INTERNAL API
* Initialization of actor * * @param uuid uuid of actor * @param path path of actor * @param context context of actor * @param mailbox mailbox of actor */ public final void initActor(UUID uuid, String path, ActorContext context, Mailbox mailbox) { this.uuid = uuid; this.path = path; this.context = context; this.mailbox = mailbox; this.askPattern = new ActorAskImpl(self()); } /** * Actor System * * @return Actor System */ public final ActorSystem system() { return context.getSystem(); } /** * Self actor reference * * @return self reference */ public final ActorRef self() { return context.getSelf(); } /** * Actor context * * @return context */ protected final ActorContext context() { return context; } /** * Sender of last received message * * @return sender's ActorRef */ public final ActorRef sender() { return context.sender(); } /** * Actor UUID * * @return uuid */ protected final UUID getUuid() { return uuid; } /** * Actor path * * @return path */ protected final String getPath() { return path; } /** * Actor mailbox * * @return mailbox */ public final Mailbox getMailbox() { return mailbox; } /** * Called before first message receiving */ public void preStart() { } public final void onReceiveGlobal(Object message) { if (message instanceof DeadLetter) { if (askPattern.onDeadLetter((DeadLetter) message)) { return; } } else if (message instanceof TaskResult) { if (askPattern.onTaskResult((TaskResult) message)) { return; } } else if (message instanceof TaskTimeout) { if (askPattern.onTaskTimeout((TaskTimeout) message)) { return; } } else if (message instanceof TaskError) { if (askPattern.onTaskError((TaskError) message)) { return; } } onReceive(message); } /** * Receiving of message * * @param message message */ public void onReceive(Object message) { } /** * Called after actor shutdown */ public void postStop() { } /** * Reply message to sender of last message * * @param message reply message */ public void reply(Object message) { if (context.sender() != null) { context.sender().send(message, self()); } } public AskFuture combine(AskFuture... futures) { return askPattern.combine(futures); } public AskFuture combine(AskCallback