Exemplo n.º 1
0
        public void TestUnknownClientExceptionFinishCommunication()
        {
            //testing only the acks
            SimulatedActorSystem system     = new SimulatedActorSystem();
            Dispatcher           dispatcher = new Dispatcher(system, 2);

            system.Spawn(dispatcher);
            TestClient client = new TestClient();

            system.Spawn(client);

            dispatcher.Tell(new InitCommunication(client, 10));

            while (client.ReceivedMessages.Count == 0)
            {
                system.RunFor(1);
            }
            Message initAckMessage = client.ReceivedMessages.Dequeue();

            Assert.AreEqual(typeof(InitAck), initAckMessage.GetType());
            InitAck initAck = (InitAck)initAckMessage;

            Assert.AreEqual(10, initAck.CommunicationId);

            SimulatedActor worker = initAck.Worker;

            worker.Tell(new FinishCommunication(11));
            while (client.ReceivedMessages.Count == 0)
            {
                system.RunFor(1);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Constructs a new Worker object
 /// </summary>
 /// <param name="disp">the dispatcher</param>
 /// <param name="messageStore">the message store responsible for persistence</param>
 /// <param name="system">the actor system simulation</param>
 public Worker(SimulatedActor disp, SimulatedActor messageStore, SimulatedActorSystem system)
 {
     this.dispatcher            = disp;
     this.messageStore          = messageStore;
     this.ongoingCommunications = new Dictionary <long, SimulatedActor>();
     this.system   = system;
     this.stopping = false;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Finishes communication with a client and sends a FinishAck message to it.
        /// After that other ClientMessage messages, using the communication ID given
        /// in the received message, cannot be sent to this worker anymore.
        /// </summary>
        ///
        /// <param name="m">non-null message of type FinishCommunication</param>
        protected void processFinishCommunication(Message m)
        {
            FinishCommunication finC = (FinishCommunication)m;

            if (!ongoingCommunications.ContainsKey(finC.CommunicationId))
            {
                throw new UnknownClientException("Unknown communication ID");
            }
            SimulatedActor client = ongoingCommunications[finC.CommunicationId];

            ongoingCommunications.Remove(finC.CommunicationId);
            client.Tell(new FinishAck(finC.CommunicationId));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Spawns a worker helper which communicates with the message store to add a dislike
        /// to a user message given in the message passed as parameter.
        /// </summary>
        /// <param name="m">non-null message of type Dislike</param>
        protected void processDislike(Message m)
        {
            Dislike like = (Dislike)m;

            if (!ongoingCommunications.ContainsKey(like.CommunicationId))
            {
                throw new UnknownClientException("Unknown communication ID");
            }
            SimulatedActor      client  = ongoingCommunications[like.CommunicationId];
            MessageStoreMessage message = new AddDislike(like.ClientName, like.MessageId, like.CommunicationId);
            WorkerHelper        helper  = new WorkerHelper(messageStore, client, message, system);

            system.Spawn(helper);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Constructs a new WorkerHelper object.
        /// </summary>
        /// <param name="messageStore">message store which receives messages from helper</param>
        /// <param name="client">client to which the message from the store gets forwarded</param>
        /// <param name="message">the message to be sent to the message store</param>
        /// <param name="system">actor system used to stop the helper</param>
        public WorkerHelper(SimulatedActor messageStore, SimulatedActor client, MessageStoreMessage message, SimulatedActorSystem system)
        {
            this.message             = message;
            this.message.StoreClient = this;
            this.messageStore        = messageStore;
            this.client            = client;
            this.system            = system;
            this.timeSinceLastSent = 0;
            this.stopping          = false;
            this.retries           = 0;

            // good connection between WorkerHelper and MessageStore -> no delay
            this.channel = new DeterministicChannel(0);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Spawns a worker helper which communicates with the message store to retrieve
        /// messages of the author given in the message passed as parameter.
        /// </summary>
        /// <param name="m">non-null message of type RetrieveMessages</param>
        protected void processRetrieveMessages(Message m)
        {
            RetrieveMessages retrMessages = (RetrieveMessages)m;

            if (!ongoingCommunications.ContainsKey(retrMessages.CommunicationId))
            {
                throw new UnknownClientException("Unknown communication ID");
            }
            SimulatedActor client = ongoingCommunications[retrMessages.CommunicationId];

            MessageStoreMessage message = new RetrieveFromStore(retrMessages.Author, retrMessages.CommunicationId);
            WorkerHelper        helper  = new WorkerHelper(messageStore, client, message, system);

            system.Spawn(helper);
        }
Exemplo n.º 7
0
        public void TestCommunication()
        {
            //testing only the acks
            SimulatedActorSystem system     = new SimulatedActorSystem();
            Dispatcher           dispatcher = new Dispatcher(system, 2);

            system.Spawn(dispatcher);
            TestClient client = new TestClient();

            system.Spawn(client);
            // send request and run system until a response is received
            // communication id is chosen by clients
            dispatcher.Tell(new InitCommunication(client, 10));
            while (client.ReceivedMessages.Count == 0)
            {
                system.RunFor(1);
            }
            Message initAckMessage = client.ReceivedMessages.Dequeue();

            Assert.AreEqual(typeof(InitAck), initAckMessage.GetType());
            InitAck initAck = (InitAck)initAckMessage;

            Assert.AreEqual(10, initAck.CommunicationId);

            SimulatedActor worker = initAck.Worker;

            initAck.Worker.Tell(new FinishCommunication(10));
            while (client.ReceivedMessages.Count == 0)
            {
                system.RunFor(1);
            }

            Message finAckMessage = client.ReceivedMessages.Dequeue();

            Assert.AreEqual(typeof(FinishAck), finAckMessage.GetType());
            FinishAck finAck = (FinishAck)finAckMessage;

            Assert.AreEqual(10, finAck.CommunicationId);
            dispatcher.Tell(new Stop());

            // TODO run system until workers and dispatcher are stopped
            int endtime = system.currentTime + 20;

            system.RunUntil(endtime);
            Assert.AreEqual(system.currentTime, endtime + 1);
        }
Exemplo n.º 8
0
        public void test_setup()
        {
            //Testclient 1
            testclient_ = new TestClient();

            system_.Spawn(testclient_);

            dispatcher_.Tell(new InitCommunication(testclient_, 10));
            while (testclient_.ReceivedMessages.Count == 0)
            {
                system_.RunFor(1);
            }
            Message initAckMessage = testclient_.ReceivedMessages.Dequeue();
            InitAck initAck        = (InitAck)initAckMessage;

            worker_ = initAck.Worker;
        }
Exemplo n.º 9
0
        public void TestMessageDrop()
        {
            SimulatedActor[] workers = new SimulatedActor[10];
            TestClient[]     clients = new TestClient[10];
            for (int i = 0; i < 10; i++)
            {
                clients[i] = new TestClient();
                system_.Spawn(clients[i]);

                dispatcher_.Tell(new InitCommunication(clients[i], i + 12));

                while (clients[i].ReceivedMessages.Count == 0)
                {
                    system_.RunFor(1);
                }
                Message initAckMessage = clients[i].ReceivedMessages.Dequeue();
                Assert.AreEqual(typeof(InitAck), initAckMessage.GetType());
                InitAck initAck = (InitAck)initAckMessage;
                Assert.AreEqual(i + 12, initAck.CommunicationId);

                workers[i] = initAck.Worker;
            }

            int j = 0;

            foreach (SimulatedActor worker in workers)
            {
                worker.Tell(new Like("group-16", 12 + j, 1));
                j++;
            }

            for (int i = 0; i < 10; i++)
            {
                while (clients[i].ReceivedMessages.Count == 0)
                {
                    system_.RunFor(1);
                }
                Message failedMessage = clients[i].ReceivedMessages.Dequeue();
                if (failedMessage.GetType() != typeof(OperationAck))
                {
                    Assert.AreEqual(typeof(OperationFailed), failedMessage.GetType());
                }
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// In stopping mode, InitCommunication always fail, which is signal
 /// using an OperationFailed message sent to the client.
 /// In this mode, only StopAck-messages are expected and if all stop acknowledgements
 /// have been collected, the Dispatcher stop itself.
 /// </summary>
 /// <param name="m">non-null message received</param>
 private void Stopping(Message m)
 {
     if (m is InitCommunication)
     {
         InitCommunication initM = ((InitCommunication)m);
         initM.Client.Tell(new OperationFailed(initM.CommunicationId));
     }
     else if (m is StopAck)
     {
         SimulatedActor actor = ((StopAck)m).Sender;
         acksToCollect.Remove(actor.Id);
         system.Stop(actor);
         if (acksToCollect.Count == 0)
         {
             system.Stop(messageStore);
             system.Stop(this);
         }
     }
 }
Exemplo n.º 11
0
        /// <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);
            }
        }
Exemplo n.º 12
0
 public StopAck(SimulatedActor sender)
 {
     Sender = sender;
 }
Exemplo n.º 13
0
 public InitAck(SimulatedActor worker, long communicationId)
 {
     this.Worker          = worker;
     this.CommunicationId = communicationId;
 }
Exemplo n.º 14
0
 public InitCommunication(SimulatedActor client, long communicationId)
 {
     this.Client          = client;
     this.CommunicationId = communicationId;
 }