Ejemplo n.º 1
0
        private void _group_OnReceiveMessage(object sender, ReceiveMessageEventArgs e)
        {
            if (e.Message.Type != MessageType.JoinAck &&
                e.Message.Type != MessageType.JoinEnd)
            {
                return; // Only care about these messages
            }
            LogBroker.Log(string.Format("RX {0} from {1}",
                                        e.Message.Type,
                                        e.Message.SourceID));

            if (e.Message.DestinationID != _config.ID && e.Message.DestinationID != Constants.NULL_DESTINATION)
            {
                return;
            }

            switch (e.Message.Type)
            {
            case MessageType.JoinAck:
                _joinAck = e.Message as JoinAckMessage;
                break;

            case MessageType.JoinEnd:
                _t.Stop();
                _nodeCount     = (e.Message as JoinEndMessage).NodeCount;
                _joinSucceeded = true;
                _joinFinished  = true;
                break;
            }
        }
Ejemplo n.º 2
0
        private void HandleJoinKeyRequestComplete()
        {
            var sum = (from msg in _responses select msg.KeyCount).Sum();
            var avg = Math.Floor(sum / (_responses.Count + 1d)); // + 1 is for the joiner

            var instructions = new Dictionary <string, List <Instruction> >();

            if (avg > _responses.Count + 1) // + 1 is for the joiner
            {
                foreach (var msg in _responses)
                {
                    int diff = msg.KeyCount - (int)avg;
                    if (diff > 0)
                    {
                        if (!instructions.ContainsKey(msg.SourceID))
                        {
                            instructions.Add(msg.SourceID, new List <Instruction>());
                        }

                        var instruction = new Instruction()
                        {
                            Address  = _joinMsg.Address,
                            Port     = _joinMsg.Port,
                            KeyCount = diff
                        };

                        instructions[msg.SourceID].Add(instruction);
                    }
                }
            }

            if (instructions.Keys.Count > 0)
            {
                var distMsg = new KeyInstructMessage()
                {
                    SourceID      = _config.ID,
                    DestinationID = Constants.NULL_DESTINATION,
                    Instructions  = instructions
                };

                SendMessage(distMsg, _group);
            }

            var joinAck = new JoinAckMessage()
            {
                SourceID          = _config.ID,
                DestinationID     = _joinMsg.SourceID,
                ExpectedResponses = instructions.Keys.Count
            };

            SendMessage(joinAck, _group);
        }