Search in sources :

Example 1 with BaseResponse

use of com.pogeyan.cmis.api.BaseResponse in project copper-cms by PogeyanOSS.

the class GatewayActor method onReceive.

@SuppressWarnings("unlikely-arg-type")
@Override
public void onReceive(Object message) throws Exception {
    if (message instanceof BaseMessage) {
        BaseMessage bm = (BaseMessage) message;
        if (bm.getTypeName().equals("entry")) {
            String senderName = this.getTypeName(this.getSender());
            if (LOG.isDebugEnabled()) {
                LOG.debug("entry message received for sender: {}", senderName);
            }
            MemberUpRequest memberUpRequest = (MemberUpRequest) bm.getMessageAsType(MemberUpRequest.class);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding {} with methods {} to ref list", senderName, memberUpRequest.getSelectors());
            }
            actorRefs.put(new ActorKey(senderName, memberUpRequest.getSelectors()), this.getSender());
            getContext().watch(getSender());
        } else if (bm.getMessageType() == MessageType.REQUEST) {
            if (!this.messageRefs.containsKey(bm.getMessageId())) {
                this.messageRefs.put(bm.getMessageId(), this.getSender());
                // get actor either with type name or action name
                ActorRef actorReference = this.getActorRefBy(bm.getTypeName(), bm.getActionName());
                if (actorReference != null) {
                    // get type name from actor reference
                    String typeName = this.getTypeName(actorReference);
                    bm.setTypeName(typeName);
                    actorReference.tell(bm, this.getSelf());
                    if (Helpers.isPerfMode()) {
                        this.requestMeter.mark();
                        Timer.Context timerContext = MetricsInputs.get().getTimer("TimerID_" + bm.getMessageId()).time();
                        perfTimerContext.put(bm.getMessageId(), timerContext);
                    }
                } else {
                    LOG.error("Actor not found for {}", bm.getTypeName());
                    BaseResponse errorResponse = BaseResponse.error("Actor not available for: " + bm.getMessageId() + ", using this actor: " + bm.getTypeName(), 500);
                    BaseMessage respMessage = BaseMessage.create("ERROR", "ERROR", errorResponse);
                    respMessage.setMessageType(MessageType.ERROR);
                    this.getSender().tell(respMessage, null);
                    if (Helpers.isPerfMode()) {
                        this.errorMeter.mark();
                    }
                }
            } else {
                BaseResponse errorResponse = BaseResponse.error("Message id already present --> " + bm.getMessageId(), 404);
                LOG.error("Message id already present: {}", bm.getMessageId());
                BaseMessage respMessage = BaseMessage.create("ERROR", "ERROR", errorResponse);
                respMessage.setMessageType(MessageType.ERROR);
                this.getSender().tell(respMessage, null);
                if (Helpers.isPerfMode()) {
                    this.errorMeter.mark();
                    this.baseResponseErrorMeter.mark();
                }
            }
        } else {
            if (this.messageRefs.containsKey((bm.getMessageId()))) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Reply back to servlet actor for message: {}", bm);
                }
                ActorRef responseActorRef = this.messageRefs.get(bm.getMessageId());
                this.messageRefs.remove(bm.getMessageId());
                responseActorRef.tell(bm, ActorRef.noSender());
                if (Helpers.isPerfMode()) {
                    this.responseMeter.mark();
                    Timer.Context timerContext = this.perfTimerContext.get(bm.getMessageId());
                    if (timerContext != null) {
                        timerContext.stop();
                        this.perfTimerContext.remove(bm.getMessageId());
                    }
                }
            } else {
                LOG.error("Unknown message in messageRefs : {}", bm.getMessageId());
                unhandled(bm);
            }
        }
    } else if (message instanceof Terminated) {
        Terminated terminated = (Terminated) message;
        String[] s = terminated.getActor().path().name().split(Pattern.quote("."));
        String terminatedActor = s[s.length - 1];
        LOG.info("Actor terminated: {}", terminatedActor);
        ActorRef tActor = actorRefs.remove(terminatedActor);
        getContext().unwatch(tActor);
    } else {
        LOG.error("Unknown message received: {}", message != null ? message.toString() : "");
        unhandled(message);
    }
}
Also used : BaseResponse(com.pogeyan.cmis.api.BaseResponse) BaseMessage(com.pogeyan.cmis.api.BaseMessage) ActorRef(akka.actor.ActorRef) MemberUpRequest(com.pogeyan.cmis.api.messages.MemberUpRequest) Terminated(akka.actor.Terminated)

Aggregations

ActorRef (akka.actor.ActorRef)1 Terminated (akka.actor.Terminated)1 BaseMessage (com.pogeyan.cmis.api.BaseMessage)1 BaseResponse (com.pogeyan.cmis.api.BaseResponse)1 MemberUpRequest (com.pogeyan.cmis.api.messages.MemberUpRequest)1