/// <summary> /// The message processing logic for the store. /// <para>If the message passed as parameter is of type <c>RetrieveFromStore</c>, /// all messages of a given author are looked up and sent back to the client of the /// store. /// </para> /// <para>If the message passed as parameter is of type <c>AddLike</c>, a /// like is added to the given message if the message exists and has not /// already been liked by the given person. /// If the message passed as parameter is of type <c>UpdateMessageStore</c>, /// a message is stored if the message is new and if the same message has not already /// been stored by the same author. /// In case of success a OperationAck message is sent to the client, otherwise /// an OperationFailed message is sent. /// </para> /// </summary> /// <param name="m">message sent to the store</param> public override void Receive(Message m) { if (m is RetrieveFromStore) { RetrieveFromStore retrieve = (RetrieveFromStore)m; List <UserMessage> foundMessage = FindByAuthor(retrieve.Author); retrieve.StoreClient.Tell(new FoundMessages(foundMessage, retrieve.CommunicationId)); } else if (m is AddLike) { AddLike addLikeMessage = (AddLike)m; if (addLike(addLikeMessage.ClientName, addLikeMessage.MessageId)) { addLikeMessage.StoreClient.Tell(new OperationAck(addLikeMessage.CommunicationId)); } else { addLikeMessage.StoreClient.Tell(new OperationFailed(addLikeMessage.CommunicationId)); } } else if (m is AddDislike) { AddDislike addDislikeMessage = (AddDislike)m; if (addDislike(addDislikeMessage.ClientName, addDislikeMessage.MessageId)) { addDislikeMessage.StoreClient.Tell(new OperationAck(addDislikeMessage.CommunicationId)); } else { addDislikeMessage.StoreClient.Tell(new OperationFailed(addDislikeMessage.CommunicationId)); } } else if (m is UpdateMessageStore) { UpdateMessageStore updateMessage = (UpdateMessageStore)m; if (update(updateMessage.Message)) { updateMessage.StoreClient.Tell(new OperationAck(updateMessage.CommunicationId)); } else { updateMessage.StoreClient.Tell(new OperationFailed(updateMessage.CommunicationId)); } } }
/// <summary> /// Performs checks on a user message, which should be published. If the /// checks are passed, a worker helper is spawned, which communicates with /// the message store to store the new user message. /// New messages must have zero likes, must not have a message ID assigned /// and must not be (strictly) longer than 10 characters. /// Only 10 characters are allowed to to alleviate exercise 4. /// </summary> /// <param name="m">non-null message of type Publish</param> protected void processPublish(Message m) { Publish publish = (Publish)m; if (!ongoingCommunications.ContainsKey(publish.CommunicationId)) { throw new UnknownClientException("Unknown communication ID"); } SimulatedActor client = ongoingCommunications[publish.CommunicationId]; UserMessage userMessage = publish.Message; if (userMessage.Likes.Count > 0 || userMessage.Dislikes.Count > 0 || userMessage.MessageId != UserMessage.NEW || userMessage.Message.Length > 10) { client.Tell(new OperationFailed(publish.CommunicationId)); } else { MessageStoreMessage message = new UpdateMessageStore(userMessage, publish.CommunicationId); WorkerHelper helper = new WorkerHelper(messageStore, client, message, system); system.Spawn(helper); } }