Ejemplo n.º 1
0
        /// <summary>
        /// Sends the specified <paramref name="message" /> to the publisher identified by handle <paramref name="to" />.<br />
        /// This is used by subscribers to send messages directly to a certain publisher of an event.<br />
        /// However, it is not guaranteed, that the publisher is a) subscribed to application messages and b) can interpret this specific message type
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="to">The receiver identified by a subscription handle</param>
        /// <param name="sender">The sender instance.</param>
        public static bool Message(ApplicationMessage message, SubscriptionHandle to, object sender)
        {
            //TODO check if sender is subscriber
            _log.DebugFormat("New application message from {0} to {1}", message.Sender, to);
            message.Handle = to;

            /*Check if subscription is remote or local
             * if local: pass it to local module
             * if remote: serialize, wrap in Message, send
             */
            if (to is RemoteSubscriptionHandle)
            {
                _log.Debug("Sending message to remote receiver");
                int nodeId = EllaConfiguration.Instance.NodeId;

                RemoteSubscriptionHandle h = (RemoteSubscriptionHandle)to;
                if (h.PublisherNodeID == nodeId)
                {
                    message.Sender = EllaModel.Instance.GetPublisherId(sender);
                }
                else if (h.SubscriberNodeID == nodeId)
                {
                    message.Sender = EllaModel.Instance.GetSubscriberId(sender);
                }

                return(Networking.SendApplicationMessage(message, to as RemoteSubscriptionHandle));
            }
            else
            {
                int  publisherId       = EllaModel.Instance.GetPublisherId(sender);
                int  subscriberId      = EllaModel.Instance.GetSubscriberId(sender);
                bool senderIsPublisher = false;

                _log.Debug("Delivering message locally");

                //publisher sends msg to subscriber
                if (to.PublisherId == publisherId)
                {
                    message.Sender    = publisherId;
                    senderIsPublisher = true;
                }
                //subscriber sends msg to publisher
                else if (to.SubscriberId == subscriberId)
                {
                    message.Sender = subscriberId;
                }

                return(DeliverApplicationMessage(message, senderIsPublisher));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Replies to a previously received message
        /// </summary>
        /// <param name="reply">The reply message to be sent</param>
        /// <param name="inReplyTo">The message in reply to (which was originally received).</param>
        /// <param name="sender">The sender instance.</param>
        /// <returns></returns>
        public static bool Reply(ApplicationMessage reply, ApplicationMessage inReplyTo, object sender)
        {
            reply.Handle = inReplyTo.Handle;
            _log.DebugFormat("Delivering reply message {0} in reply to {1} from {2}", reply, inReplyTo, reply.Sender);

            _log.Debug("Delivering reply locally");

            if (inReplyTo.Handle.PublisherId == inReplyTo.Sender)
            {
                Publisher publisher = EllaModel.Instance.GetPublisher(inReplyTo.Sender);
                reply.Sender = EllaModel.Instance.GetSubscriberId(sender);

                if (inReplyTo.Handle is RemoteSubscriptionHandle)
                {
                    _log.Debug("Delivering reply to remote receiver");
                    return(Networking.SendApplicationMessage(reply, inReplyTo.Handle as RemoteSubscriptionHandle,
                                                             isReply: true));
                }
                else
                {
                    DeliverMessage(reply, publisher.Instance);
                }
            }
            else if (inReplyTo.Handle.SubscriberId == inReplyTo.Sender)
            {
                Object subscriber = EllaModel.Instance.GetSubscriber(inReplyTo.Sender);
                reply.Sender = EllaModel.Instance.GetPublisherId(sender);

                if (inReplyTo.Handle is RemoteSubscriptionHandle)
                {
                    _log.Debug("Delivering reply to remote receiver");
                    return(Networking.SendApplicationMessage(reply, inReplyTo.Handle as RemoteSubscriptionHandle,
                                                             isReply: true));
                }
                else
                {
                    DeliverMessage(reply, subscriber);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }