/// <summary> /// Initiates communication with a client and sends an InitAck message to it, /// which contains a reference to <c>this</c>. /// After that other ClientMessage messages can be sent to this worker /// using the communication ID given in the received message. /// </summary> /// <param name="m">non-null message of type InitCommunication</param> protected void processInitCommunication(Message m) { InitCommunication initC = (InitCommunication)m; ongoingCommunications[initC.CommunicationId] = initC.Client; initC.Client.Tell(new InitAck(this, initC.CommunicationId)); }
/// <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); } } }
/// <summary> /// In normal operation messages are forwarded to workers. /// A InitCommunication-messages are forwarded to one worker /// which is selected based on the communication id set in the message. /// The selection scheme is (if workers are numbered from 0 to n - 1) /// selectworkernumber = communication % n, where a % b is the non-negative /// remainder of the integer division a/b. /// If a Stop message is sent, it is broadcast to all workers and the mode /// is switched to STOPPING. /// </summary> /// <param name="m">non-null message received</param> private void NormalOperation(Message m) { if (m is Stop) { foreach (Worker w in workers) { acksToCollect.Add(w.Id); w.Tell(new Stop()); } mode = Mode.STOPPING; } else if (m is InitCommunication) { // decide upon id for now, maybe switch to login credentials TODO InitCommunication initC = ((InitCommunication)m); int rnd = new Random((int)initC.CommunicationId).Next(); int index = (((rnd % workers.Count) + workers.Count) % workers.Count); Worker w = workers[index]; w.Tell(m); } }