Exemplo n.º 1
0
        public void WriteMembers(ISwimMessage memberdataMessage, Member[] membersList)
        {
            List <MemberDataItem> list = new List <MemberDataItem>();

            var totalMembers = new List <Member> {
                Self
            };

            if (membersList != null)
            {
                totalMembers.AddRange(membersList);
            }

            // TODO - don't just iterate over the members, optimize make intelligent

            list.AddRange(totalMembers.Select(m => new MemberDataItem
            {
                State       = m.State,
                Address     = m.Address,
                Generation  = m.Generation,
                Service     = m.Service,
                ServicePort = m.ServicePort
            }));

            memberdataMessage.MemberData = list.ToArray();
        }
Exemplo n.º 2
0
        private void UpdateMembers(ISwimMessage message)
        {
            foreach (var m in message.MemberData)
            {
                var    memberState = m.State;
                var    address     = m.Address;
                var    generation  = m.Generation;
                byte   service     = memberState == MemberState.Alive ? m.Service : (byte)0;
                ushort servicePort = memberState == MemberState.Alive ? m.ServicePort : (ushort)0;

                // we don't add ourselves to the member list
                if (address.Value != Self.Address.Value)
                {
                    lock (memberLocker)
                    {
                        if (members.TryGetValue(address.Value, out var member) &&
                            (member.IsLaterGeneration(generation) ||
                             member.Generation == generation && member.IsStateSuperseded(memberState)))
                        {
                            RemoveAwaitingAck(member.Address); // stops dead claim escalation

                            member.Update(memberState, generation, service, servicePort);

                            logger.LogInformation("{LocalAddress} member state changed {member}", Self.Address, member);
                        }

                        else if (member == null)
                        {
                            member = new Member
                            {
                                State       = memberState,
                                Address     = address,
                                Generation  = generation,
                                ServicePort = servicePort,
                                Service     = service
                            };

                            members.Add(address.Value, member);
                            logger.LogInformation("{LocalAddress} member added {member}", Self.Address, member);
                        }
                    }
                }

                // handle any state claims about ourselves
                else if (Self.IsLaterGeneration(generation) || memberState != MemberState.Alive && generation == Self.Generation)
                {
                    Self.Generation = (byte)(generation + 1);
                }
            }
        }