Exemplo n.º 1
0
 /// <summary>
 /// Matches the passed subtopic against this subtopic.
 ///
 /// If neither subtopic contains a wildcard they must literally match.
 /// If one or the other contains a wildcard they may match.
 ///
 /// "chatrooms.*" will match "chatrooms.lobby" or "chatrooms.us.ca" but will not match "chatrooms" (assuming a subtopic separator of ".").
 /// "chatrooms.*.ca" will match "chatrooms.us.ca" but not "chatrooms.us.ma".
 /// </summary>
 /// <param name="subtopic">Subtopic object to match against this subtopic.</param>
 /// <returns>True if subtopics match.</returns>
 public bool Matches(Subtopic subtopic)
 {
     if (this.Value == subtopic.Value)
     {
         return(true);
     }
     string[] parts1 = this.SubtopicItems;
     string[] parts2 = subtopic.SubtopicItems;
     for (int i = 0; i < parts1.Length; i++)
     {
         string part1 = parts1[i];
         if (part1 == SubtopicWildcard)
         {
             continue;
         }
         if (i >= parts2.Length)
         {
             return(true);
         }
         string part2 = parts2[i];
         if (part1 != part2)
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 2
0
 public int CompareTo(object obj)
 {
     if (obj is Subtopic)
     {
         Subtopic other = (Subtopic)obj;
         return(string.Equals(other.Value, _subtopic) ? 0 : -1);
     }
     return(-1);
 }
Exemplo n.º 3
0
        public IList GetSubscribers(IMessage message, bool evalSelector)
        {
            lock (_objLock) {
                bool filter = true;
                if (message.headers == null || message.headers.Count == 0)
                {
                    filter = false;
                }

                if (!filter)
                {
                    return(GetSubscribers());
                }
                else
                {
                    Subtopic subtopic = null;
                    if (message.headers.ContainsKey(AsyncMessage.SubtopicHeader))
                    {
                        subtopic = new Subtopic(message.headers[AsyncMessage.SubtopicHeader] as string);
                        MessagingAdapter messagingAdapter = _messageDestination.ServiceAdapter as MessagingAdapter;
                        if (messagingAdapter != null)
                        {
                            if (!messagingAdapter.AllowSend(subtopic))
                            {
                                return(null);
                            }
                        }
                    }

                    ArrayList subscribers = new ArrayList();
                    foreach (MessageClient messageClient in _subscribers.Values)
                    {
                        bool include = true;
                        if (subtopic != null && messageClient.Subtopic != null)
                        {
                            include = include && subtopic.Matches(messageClient.Subtopic);
                        }
                        if (messageClient.Selector != null && evalSelector)
                        {
                            include = include && messageClient.Selector.Evaluate(null, message.headers);
                        }

                        if (include)
                        {
                            subscribers.Add(messageClient.ClientId);
                        }
                    }
                    return(subscribers);
                }
            }
        }
Exemplo n.º 4
0
 public MessageClient AddSubscriber(string clientId, string endpointId, Subtopic subtopic, Selector selector)
 {
     lock (_objLock) {
         if (subtopic != null)
         {
             MessagingAdapter messagingAdapter = _messageDestination.ServiceAdapter as MessagingAdapter;
             if (messagingAdapter != null)
             {
                 if (!messagingAdapter.AllowSubscribe(subtopic))
                 {
                     ASObject aso = new ASObject();
                     aso["subtopic"] = subtopic.Value;
                     throw new MessageException(aso);
                 }
             }
         }
         if (!_subscribers.Contains(clientId))
         {
             //Set in RtmpService
             MessageClient messageClient = new MessageClient(clientId, _messageDestination, endpointId);
             messageClient.Subtopic = subtopic;
             messageClient.Selector = selector;
             messageClient.AddSubscription(selector, subtopic);
             AddSubscriber(messageClient);
             messageClient.NotifyCreatedListeners();
             return(messageClient);
         }
         else
         {
             MessageClient messageClient = _subscribers[clientId] as MessageClient;
             IClient       client        = FluorineContext.Current.Client;
             if (client != null && !client.Id.Equals(messageClient.Client.Id))
             {
                 throw new MessageException("Duplicate subscriptions from multiple Flex Clients");
             }
             //Reset the endpoint push state for the subscription to make sure its current because a resubscribe could be arriving over a new endpoint or a new session.
             messageClient.ResetEndpoint(endpointId);
             return(messageClient);
         }
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Invoked before a client subscribe request is processed.
 /// </summary>
 /// <param name="subtopic">The subtopic the client is attempting to subscribe to.</param>
 /// <returns>true to allow the subscription, false to prevent it.</returns>
 public bool AllowSubscribe(Subtopic subtopic)
 {
     return(true);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Invoked before a client message is sent to a subtopic.
 /// </summary>
 /// <param name="subtopic">The subtopic the client is attempting to send a message to.</param>
 /// <returns>true to allow the message to be sent, false to prevent it.</returns>
 public bool AllowSend(Subtopic subtopic)
 {
     return(true);
 }
Exemplo n.º 7
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;
			}
		}
Exemplo n.º 8
0
		public SubscriptionInfo(Selector selector, Subtopic subtopic) {
			_selector = selector;
			_subtopic = subtopic;
		}
Exemplo n.º 9
0
		internal void RemoveSubscription(Selector selector, Subtopic subtopic) {
			_subscriptions.Remove(new SubscriptionInfo(selector, subtopic));
		}
Exemplo n.º 10
0
		internal void AddSubscription(Selector selector, Subtopic subtopic) {
			_subscriptions.Add(new SubscriptionInfo(selector, subtopic));
		}
Exemplo n.º 11
0
		/// <summary>
		/// Matches the passed subtopic against this subtopic. 
		/// 
		/// If neither subtopic contains a wildcard they must literally match.
		/// If one or the other contains a wildcard they may match. 
		/// 
		/// "chatrooms.*" will match "chatrooms.lobby" or "chatrooms.us.ca" but will not match "chatrooms" (assuming a subtopic separator of "."). 
		/// "chatrooms.*.ca" will match "chatrooms.us.ca" but not "chatrooms.us.ma". 
		/// </summary>
		/// <param name="subtopic">Subtopic object to match against this subtopic.</param>
		/// <returns>True if subtopics match.</returns>
		public bool Matches(Subtopic subtopic) {
			if (this.Value == subtopic.Value)
				return true;
			string[] parts1 = this.SubtopicItems;
			string[] parts2 = subtopic.SubtopicItems;
			for (int i = 0; i < parts1.Length; i++) {
				string part1 = parts1[i];
				if (part1 == SubtopicWildcard)
					continue;
				if (i >= parts2.Length)
					return true;
				string part2 = parts2[i];
				if (part1 != part2)
					return false;
			}
			return true;
		}