Ejemplo n.º 1
0
        /// <summary>
        /// Adds a new topic message for everyone with a specific topic and type
        /// </summary>
        /// <param name="senderId">The Id of the client that sent the message</param>
        /// <param name="type">The topic type of the message</param>
        /// <param name="content">The content of the message</param>
        /// <param name="expirationDate">The date at which the message should expire</param>
        /// <exception cref="ArgumentNullException">Thrown when the content of the message is null or empty</exception>
        /// <exception cref="ArgumentException">Thrown when the Id of the sender is invalid or not registered into the server</exception>
        public async Task AddTopicMessageAsync(Guid senderId, TopicMessageType type, string content, DateTime expirationDate)
        {
            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException(nameof(content), "Cannot add a message with null content to the queue");
            }

            lock (mClients)
            {
                if (mClients.All(client => client.Id != senderId))
                {
                    throw new ArgumentException($"Cannot send message from an inexistent client. There is no registered client with id: {senderId}", nameof(senderId));
                }
            }

            try
            {
                await Task.Run(() =>
                {
                    var message = new TopicMessage(senderId, type, content, expirationDate);

                    lock (mTopicMessages)
                    {
                        mTopicMessages.Add(message);
                    }
                }, mCancellationTokenSource.Token).ConfigureAwait(false);
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine($"Task {nameof(AddTopicMessageAsync)} cancelled");
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Retrieve all the available topic messages that have a certain type
 /// </summary>
 /// <param name="topicMessageType">The type of the topic messages to be fetched</param>
 /// <returns>A collection of topic messages that are of the given type</returns>
 public async Task <IList <TopicMessage> > GetTopicMessagesAsync(TopicMessageType topicMessageType)
 {
     try
     {
         return(await Task.Run(() =>
         {
             lock (mTopicMessages)
             {
                 return mTopicMessages.Where(message => message.Type == topicMessageType).ToList();
             }
         }, mCancellationTokenSource.Token).ConfigureAwait(false));
     }
     catch (TaskCanceledException)
     {
         Debug.WriteLine($"Task {nameof(GetTopicMessagesAsync)} cancelled");
         return(new List <TopicMessage>());
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Create a new topic message that has an exact topic and type
 /// </summary>
 /// <param name="senderId">The Id of the <see cref="Client"/> that created the message</param>
 /// <param name="type">The type of the message</param>
 /// <param name="content">The content of the message</param>
 /// <param name="expiresAt">The time at which the message should expire</param>
 public TopicMessage(Guid senderId, TopicMessageType type, string content, DateTime expiresAt) : base(senderId, content)
 {
     Type      = type;
     ExpiresAt = expiresAt;
 }