Beispiel #1
0
        /// <summary>
        /// Handles a message routed to the service by the MessageBroker.
        /// </summary>
        /// <param name="message">The message that should be handled by the service.</param>
        /// <returns>The result of the message processing.</returns>
        public override object ServiceMessage(IMessage message)
        {
            CommandMessage     commandMessage     = message as CommandMessage;
            MessageDestination messageDestination = GetDestination(message) as MessageDestination;

            if (commandMessage != null)
            {
                string             clientId           = commandMessage.clientId as string;
                MessageClient      messageClient      = messageDestination.SubscriptionManager.GetSubscriber(clientId);
                AcknowledgeMessage acknowledgeMessage = null;
                switch (commandMessage.operation)
                {
                case CommandMessage.SubscribeOperation:
                    if (messageClient == null)
                    {
                        if (clientId == null)
                        {
                            clientId = Guid.NewGuid().ToString("D");
                        }

                        if (log.IsDebugEnabled)
                        {
                            log.Debug(__Res.GetString(__Res.MessageServiceSubscribe, messageDestination.Id, clientId));
                        }

                        string endpointId = commandMessage.GetHeader(MessageBase.EndpointHeader) as string;
                        if (_messageBroker.GetEndpoint(endpointId) == null)
                        {
                            ServiceException serviceException = new ServiceException("Endpoint was not specified");
                            serviceException.FaultCode = "Server.Processing.MissingEndpoint";
                            throw serviceException;
                        }
                        commandMessage.clientId = clientId;

                        if (messageDestination.ServiceAdapter != null && messageDestination.ServiceAdapter.HandlesSubscriptions)
                        {
                            try
                            {
                                acknowledgeMessage = messageDestination.ServiceAdapter.Manage(commandMessage) as AcknowledgeMessage;
                            }
                            catch (MessageException me)
                            {
                                acknowledgeMessage = me.GetErrorMessage();
                                //Leave, do not subscribe
                                return(acknowledgeMessage);
                            }
                            catch (Exception ex)
                            {
                                //Guard against service adapter failure
                                acknowledgeMessage = ErrorMessage.GetErrorMessage(commandMessage, ex);
                                if (log.IsErrorEnabled)
                                {
                                    log.Error(__Res.GetString(__Res.ServiceAdapter_ManageFail, this.id, messageDestination.Id, commandMessage), ex);
                                }
                                //Leave, do not subscribe
                                return(acknowledgeMessage);
                            }
                        }

                        Subtopic subtopic = null;
                        Selector selector = null;
                        if (commandMessage.headers != null)
                        {
                            if (commandMessage.headers.ContainsKey(CommandMessage.SelectorHeader))
                            {
                                selector = Selector.CreateSelector(commandMessage.headers[CommandMessage.SelectorHeader] as string);
                            }
                            if (commandMessage.headers.ContainsKey(AsyncMessage.SubtopicHeader))
                            {
                                subtopic = new Subtopic(commandMessage.headers[AsyncMessage.SubtopicHeader] as string);
                            }
                        }
                        IClient client = FluorineContext.Current.Client;
                        client.Renew();
                        messageClient = messageDestination.SubscriptionManager.AddSubscriber(clientId, endpointId, subtopic, selector);
                        if (acknowledgeMessage == null)
                        {
                            acknowledgeMessage = new AcknowledgeMessage();
                        }
                        acknowledgeMessage.clientId = clientId;
                    }
                    else
                    {
                        acknowledgeMessage          = new AcknowledgeMessage();
                        acknowledgeMessage.clientId = clientId;
                    }
                    return(acknowledgeMessage);

                case CommandMessage.UnsubscribeOperation:
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(__Res.GetString(__Res.MessageServiceUnsubscribe, messageDestination.Id, clientId));
                    }

                    if (messageDestination.ServiceAdapter != null && messageDestination.ServiceAdapter.HandlesSubscriptions)
                    {
                        try
                        {
                            acknowledgeMessage = messageDestination.ServiceAdapter.Manage(commandMessage) as AcknowledgeMessage;
                        }
                        catch (MessageException me)
                        {
                            acknowledgeMessage = me.GetErrorMessage();
                        }
                        catch (Exception ex)
                        {
                            //Guard against service adapter failure
                            acknowledgeMessage = ErrorMessage.GetErrorMessage(commandMessage, ex);
                            if (log.IsErrorEnabled)
                            {
                                log.Error(__Res.GetString(__Res.ServiceAdapter_ManageFail, this.id, messageDestination.Id, commandMessage), ex);
                            }
                        }
                    }
                    if (messageClient != null)
                    {
                        messageDestination.SubscriptionManager.RemoveSubscriber(messageClient);
                    }
                    if (acknowledgeMessage == null)
                    {
                        acknowledgeMessage = new AcknowledgeMessage();
                    }
                    return(acknowledgeMessage);

                case CommandMessage.PollOperation:
                {
                    if (messageClient == null)
                    {
                        ServiceException serviceException = new ServiceException(string.Format("MessageClient is not subscribed to {0}", commandMessage.destination));
                        serviceException.FaultCode = "Server.Processing.NotSubscribed";
                        throw serviceException;
                    }
                    IClient client = FluorineContext.Current.Client;
                    client.Renew();
                    try
                    {
                        acknowledgeMessage = messageDestination.ServiceAdapter.Manage(commandMessage) as AcknowledgeMessage;
                    }
                    catch (MessageException me)
                    {
                        acknowledgeMessage = me.GetErrorMessage();
                    }
                    catch (Exception ex)
                    {
                        //Guard against service adapter failure
                        acknowledgeMessage = ErrorMessage.GetErrorMessage(commandMessage, ex);
                        if (log.IsErrorEnabled)
                        {
                            log.Error(__Res.GetString(__Res.ServiceAdapter_ManageFail, this.id, messageDestination.Id, commandMessage), ex);
                        }
                    }
                    if (acknowledgeMessage == null)
                    {
                        acknowledgeMessage = new AcknowledgeMessage();
                    }
                    return(acknowledgeMessage);
                }

                case CommandMessage.ClientPingOperation:
                    if (messageDestination.ServiceAdapter != null && messageDestination.ServiceAdapter.HandlesSubscriptions)
                    {
                        try
                        {
                            messageDestination.ServiceAdapter.Manage(commandMessage);
                        }
                        catch (MessageException)
                        {
                            return(false);
                        }
                        catch (Exception ex)
                        {
                            //Guard against service adapter failure
                            if (log.IsErrorEnabled)
                            {
                                log.Error(__Res.GetString(__Res.ServiceAdapter_ManageFail, this.id, messageDestination.Id, commandMessage), ex);
                            }
                            return(false);
                        }
                    }
                    return(true);

                default:
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(__Res.GetString(__Res.MessageServiceUnknown, commandMessage.operation, messageDestination.Id));
                    }
                    try
                    {
                        acknowledgeMessage = messageDestination.ServiceAdapter.Manage(commandMessage) as AcknowledgeMessage;
                    }
                    catch (MessageException me)
                    {
                        acknowledgeMessage = me.GetErrorMessage();
                    }
                    catch (Exception ex)
                    {
                        //Guard against service adapter failure
                        acknowledgeMessage = ErrorMessage.GetErrorMessage(commandMessage, ex);
                        if (log.IsErrorEnabled)
                        {
                            log.Error(__Res.GetString(__Res.ServiceAdapter_ManageFail, this.id, messageDestination.Id, commandMessage), ex);
                        }
                    }
                    if (acknowledgeMessage == null)
                    {
                        acknowledgeMessage = new AcknowledgeMessage();
                    }
                    return(acknowledgeMessage);
                }
            }
            else
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug(__Res.GetString(__Res.MessageServiceRoute, messageDestination.Id, message.clientId));
                }

                if (FluorineContext.Current != null && FluorineContext.Current.Client != null)//Not set when user code initiates push
                {
                    IClient client = FluorineContext.Current.Client;
                    client.Renew();
                }
                object result = messageDestination.ServiceAdapter.Invoke(message);
                return(result);
            }
        }
 public SubscriptionInfo(Selector selector, Subtopic subtopic)
 {
     _selector = selector;
     _subtopic = subtopic;
 }
 internal void AddSubscription(Selector selector, Subtopic subtopic)
 {
     _subscriptions.Add(new SubscriptionInfo(selector, subtopic));
 }
 internal void RemoveSubscription(Selector selector, Subtopic subtopic)
 {
     _subscriptions.Remove(new SubscriptionInfo(selector, subtopic));
 }
Beispiel #5
0
        public override object ServiceMessage(IMessage message)
        {
            CommandMessage     commandMessage     = message as CommandMessage;
            MessageDestination messageDestination = base.GetDestination(message) as MessageDestination;

            if (commandMessage == null)
            {
                if (log.get_IsDebugEnabled())
                {
                    log.Debug(__Res.GetString("MessageServiceRoute", new object[] { messageDestination.Id, message.clientId }));
                }
                if ((FluorineContext.Current != null) && (FluorineContext.Current.Client != null))
                {
                    FluorineContext.Current.Client.Renew();
                }
                return(messageDestination.ServiceAdapter.Invoke(message));
            }
            string        clientId   = commandMessage.clientId as string;
            MessageClient subscriber = messageDestination.SubscriptionManager.GetSubscriber(clientId);

            switch (commandMessage.operation)
            {
            case 0:
                if (subscriber == null)
                {
                    if (clientId == null)
                    {
                        clientId = Guid.NewGuid().ToString("D");
                    }
                    if (log.get_IsDebugEnabled())
                    {
                        log.Debug(__Res.GetString("MessageServiceSubscribe", new object[] { messageDestination.Id, clientId }));
                    }
                    string header = commandMessage.GetHeader("DSEndpoint") as string;
                    commandMessage.clientId = clientId;
                    if ((messageDestination.ServiceAdapter != null) && messageDestination.ServiceAdapter.HandlesSubscriptions)
                    {
                        messageDestination.ServiceAdapter.Manage(commandMessage);
                    }
                    Subtopic subtopic = null;
                    Selector selector = null;
                    if (commandMessage.headers != null)
                    {
                        if (commandMessage.headers.ContainsKey(CommandMessage.SelectorHeader))
                        {
                            selector = Selector.CreateSelector(commandMessage.headers[CommandMessage.SelectorHeader] as string);
                        }
                        if (commandMessage.headers.ContainsKey("DSSubtopic"))
                        {
                            subtopic = new Subtopic(commandMessage.headers["DSSubtopic"] as string);
                        }
                    }
                    IClient client = FluorineContext.Current.Client;
                    client.Renew();
                    subscriber = messageDestination.SubscriptionManager.AddSubscriber(client, clientId, header, messageDestination, subtopic, selector);
                }
                return(new AcknowledgeMessage {
                    clientId = clientId
                });

            case 1:
                if (log.get_IsDebugEnabled())
                {
                    log.Debug(__Res.GetString("MessageServiceUnsubscribe", new object[] { messageDestination.Id, clientId }));
                }
                if ((messageDestination.ServiceAdapter != null) && messageDestination.ServiceAdapter.HandlesSubscriptions)
                {
                    messageDestination.ServiceAdapter.Manage(commandMessage);
                }
                if (subscriber != null)
                {
                    subscriber.Unsubscribe();
                }
                return(new AcknowledgeMessage());

            case 2:
                if (subscriber == null)
                {
                    ServiceException exception = new ServiceException(string.Format("MessageClient is not subscribed to {0}", commandMessage.destination))
                    {
                        FaultCode = "Server.Processing.NotSubscribed"
                    };
                    throw exception;
                }
                FluorineContext.Current.Client.Renew();
                messageDestination.ServiceAdapter.Manage(commandMessage);
                return(new AcknowledgeMessage());

            case 5:
                if ((messageDestination.ServiceAdapter != null) && messageDestination.ServiceAdapter.HandlesSubscriptions)
                {
                    messageDestination.ServiceAdapter.Manage(commandMessage);
                }
                return(true);
            }
            if (log.get_IsDebugEnabled())
            {
                log.Debug(__Res.GetString("MessageServiceUnknown", new object[] { commandMessage.operation, messageDestination.Id }));
            }
            messageDestination.ServiceAdapter.Manage(commandMessage);
            return(new AcknowledgeMessage());
        }