/// <summary>
        /// Creates a new group and adds it to our list of groups
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        private WebSocketGroup CreateGroup(WebSocketGroupQuery query)
        {
            WebSocketGroup g = new WebSocketGroup(query);

            groups.Add(g);
            return(g);
        }
        /// <summary>
        /// Adds a client to a group
        /// </summary>
        /// <param name="query"></param>
        public WebSocketGroup AddClient(GroupWebSocketService client, WebSocketGroupQuery query)
        {
            //Get a group for this client
            WebSocketGroup g = GetClientGroup(query);

            //Add client to group
            g.AddClient(client);

            return(g);
        }
 /// <summary>
 /// Searches to see if a query can find a group. May return null.
 /// </summary>
 /// <param name="query"></param>
 /// <returns></returns>
 public WebSocketGroup FindGroup(WebSocketGroupQuery query)
 {
     foreach (var g in groups)
     {
         if (g.CheckIfAuthorized(query))
         {
             return(g);
         }
     }
     return(null);
 }
        /// <summary>
        /// Gets or creates a group for a client
        /// </summary>
        /// <param name="query"></param>
        public WebSocketGroup GetClientGroup(WebSocketGroupQuery query)
        {
            lock (groups)
            {
                //Search for groups that will accept this
                WebSocketGroup g = FindGroup(query);
                if (g != null)
                {
                    return(g);
                }

                //We'll need to create a group
                return(CreateGroup(query));
            }
        }
        /// <summary>
        /// Sends a message to a query and returns if a group was found for it
        /// </summary>
        /// <param name="query"></param>
        /// <param name="data"></param>
        /// <param name="length"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task <bool> DistributeMessage(WebSocketGroupQuery query, byte[] data, int length, WebSocketMessageType type)
        {
            //Try to find a valid group
            WebSocketGroup g = FindGroup(query);

            if (g == null)
            {
                return(false);
            }

            //Send message
            await g.SendDistributedMessage(data, length, type, new List <GroupWebSocketService>());

            return(true);
        }
 /// <summary>
 /// Checks if a new client should be added to this group. Checks against the identifier
 /// </summary>
 /// <param name="q"></param>
 /// <returns></returns>
 public bool CheckIfAuthorized(WebSocketGroupQuery q)
 {
     return(identifier.CheckIfAuthorized(q));
 }
 public WebSocketGroup(WebSocketGroupQuery identifier)
 {
     this.identifier = identifier;
     this.clients    = new List <GroupWebSocketService>();
 }
 /// <summary>
 /// Checks if a requested query matches this query
 /// </summary>
 /// <param name="request"></param>
 public abstract bool CheckIfAuthorized(WebSocketGroupQuery request);