private void HandleKeyInstruct(KeyInstructMessage msg) { if (!msg.Instructions.ContainsKey(_config.ID)) { return; } var instructionSet = msg.Instructions[_config.ID]; foreach (var instruction in instructionSet) { int count = instruction.KeyCount; var keys = _processor.Get(count).ToList(); var srcAddress = IPAddress.Parse(_config.TCPServerAddress); var srcPort = Int32.Parse(_config.TCPServerPort); var dstAddress = IPAddress.Parse(instruction.Address); var dstPort = Int32.Parse(instruction.Port); using (var tcpSocket = new TCPSocket( srcAddress, srcPort, dstAddress, dstPort)) { var distMsg = new KeyDistributeMessage() { SourceID = _config.ID, DestinationID = Constants.NULL_DESTINATION, Keys = keys }; SendMessage(distMsg, tcpSocket); } } }
private void HandleKeyInstruct(KeyInstructMessage msg) { var srcAddress = IPAddress.Parse(_config.TCPServerAddress); var srcPort = Int32.Parse(_config.TCPServerPort); var instructionSet = msg.Instructions[_config.ID]; foreach (var instruction in instructionSet) { var dstAddress = IPAddress.Parse(instruction.Address); var dstPort = Int32.Parse(instruction.Port); using (var tcpSocket = new TCPSocket( srcAddress, srcPort, dstAddress, dstPort)) { var keys = _keys.Take(instruction.KeyCount).ToList(); _keys.RemoveRange(0, keys.Count); var keyDistribute = new KeyDistributeMessage() { SourceID = _config.ID, DestinationID = Constants.NULL_DESTINATION, Keys = keys }; SendMessage(keyDistribute, tcpSocket); } } _t.Stop(); _leaveFinished = true; }
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); }
private void HandleLeaveKeyRequestComplete() { var sum = (from msg in _responses select msg.KeyCount).Sum() + _leaveMsg.KeyCount; var avg = Math.Floor(sum / (double)(_responses.Count)); var instructions = new Dictionary <string, List <Instruction> >(); if (_leaveMsg.KeyCount > 0) { foreach (var msg in _responses) { int diff = (int)avg - msg.KeyCount; if (diff > 0) { if (!instructions.ContainsKey(_leaveMsg.SourceID)) { instructions.Add(_leaveMsg.SourceID, new List <Instruction>()); } var instruction = new Instruction() { Address = msg.Address, Port = msg.Port, KeyCount = diff }; instructions[_leaveMsg.SourceID].Add(instruction); } } // Because we're flooring the average, there may be unassigned keys // So just send whatever is left to the first node in the list var instructionSum = (from instructionSet in instructions.Values from instruction in instructionSet select instruction.KeyCount).Sum(); if (instructionSum < _leaveMsg.KeyCount) { var diff = _leaveMsg.KeyCount - instructionSum; instructions.Values.First().First().KeyCount += diff; } } if (instructions.Keys.Count > 0) { _leaveAcksReceived = 0; _leaveAcksExpected = 0; foreach (var set in instructions.Values) { _leaveAcksExpected += set.Count; } var instruct = new KeyInstructMessage() { SourceID = _config.ID, DestinationID = _leaveMsg.SourceID, Instructions = instructions }; SendMessage(instruct, _group); } }