internal void Confirm( string?correlatingMessageId, AttributeSet set, TrackedAttribute tracked, ApplicationMessageType type, Node toOriginalSource) { switch (type) { case ApplicationMessageType.AddAttribute: var confirmAdd = ConfirmAttribute.From(correlatingMessageId, toOriginalSource, set, tracked, ApplicationMessageType.ConfirmAddAttribute); _outbound.Application(ApplicationSays.From(_node.Id, _node.Name, confirmAdd.ToPayload()), toOriginalSource.Collected); _application.InformAttributeAdded(set.Name !, tracked.Attribute?.Name); break; case ApplicationMessageType.RemoveAttribute: var confirmRemove = ConfirmAttribute.From(correlatingMessageId, toOriginalSource, set, tracked, ApplicationMessageType.ConfirmRemoveAttribute); _outbound.Application(ApplicationSays.From(_node.Id, _node.Name, confirmRemove.ToPayload()), toOriginalSource.Collected); _application.InformAttributeRemoved(set.Name !, tracked.Attribute?.Name); break; case ApplicationMessageType.ReplaceAttribute: var confirmReplace = ConfirmAttribute.From(correlatingMessageId, toOriginalSource, set, tracked, ApplicationMessageType.ConfirmReplaceAttribute); _outbound.Application(ApplicationSays.From(_node.Id, _node.Name, confirmReplace.ToPayload()), toOriginalSource.Collected); _application.InformAttributeReplaced(set.Name !, tracked.Attribute?.Name); break; default: throw new InvalidOperationException("Cannot confirm unknown ApplicationMessageType."); } }
internal void ConfirmRemove(string?correlatingMessageId, AttributeSet set, Node toOriginalSource) { var confirm = new ConfirmRemoveAttributeSet(correlatingMessageId, _node, set); _outbound.Application(ApplicationSays.From(_node.Id, _node.Name, confirm.ToPayload()), toOriginalSource.Collected); _application.InformAttributeSetRemoved(set.Name); }
private RawMessage RawMessageFor(Id id, Name name, ApplicationMessage message) { var messageBuffer = new MemoryStream(4096); var says = ApplicationSays.From(id, name, message.ToPayload()); MessageConverters.MessageToBytes(says, messageBuffer); return(Converters.ToRawMessage(Id.Of(1).Value, messageBuffer)); }
public void Application(ApplicationSays says, IEnumerable <Node> unconfirmedNodes) { var buffer = _outbound.PooledByteBuffer(); MessageConverters.MessageToBytes(says, buffer.AsStream()); var message = _node.Id.Value.ToRawMessage(buffer.AsStream()); _outbound.Broadcast(unconfirmedNodes, _outbound.BytesFrom(message, buffer)); }
public static OperationalMessage?MessageFrom(string content) { if (content.StartsWith(APP)) { return(ApplicationSays.From(content)); } if (content.StartsWith(DIR)) { return(Directory.From(content)); } if (content.StartsWith(ELECT)) { return(Elect.From(content)); } if (content.StartsWith(JOIN)) { return(Join.From(content)); } if (content.StartsWith(LEADER)) { return(Leader.From(content)); } if (content.StartsWith(LEAVE)) { return(Leave.From(content)); } if (content.StartsWith(PING)) { return(Ping.From(content)); } if (content.StartsWith(PULSE)) { return(Pulse.From(content)); } if (content.StartsWith(SPLIT)) { return(Split.From(content)); } if (content.StartsWith(VOTE)) { return(Vote.From(content)); } return(null); }
internal void DistributeTo(AttributeSet set, IEnumerable <Node> nodes) { var create = new CreateAttributeSet(_node, set); var confirmable = _confirmables.UnconfirmedFor(create, nodes); _outbound.Application(ApplicationSays.From(_node.Id, _node.Name, create.ToPayload()), confirmable.UnconfirmedNodes); _application.InformAttributeSetCreated(set.Name); foreach (var tracked in set.All) { DistributeTo(set, tracked, ApplicationMessageType.AddAttribute, nodes); } }
internal void RedistributeUnconfirmed() { foreach (var confirmable in _confirmables.AllRedistributable) { if (confirmable.HasUnconfirmedNodes) { _logger.Trace($"REDIST ATTR: {confirmable}"); _outbound.Application(ApplicationSays.From( _node.Id, _node.Name, confirmable.Message?.ToPayload()), confirmable.UnconfirmedNodes); } } }
public static void MessageToBytes(ApplicationSays app, Stream buffer) { var builder = new StringBuilder(OperationalMessage.APP).Append("\n"); builder .Append("id=").Append(app.Id.Value) .Append(" nm=").Append(app.Name.Value) .Append(" si=").Append(app.SaysId) .Append("\n").Append(app.Payload); var bytes = Converters.TextToBytes(builder.ToString()); buffer.Write(bytes, 0, bytes.Length); }
public void TestGenerateApplicationSaidMessage() { var id = Id.Of(1); var name = new Name("node1"); var payload = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sagittis risus quis nulla blandit, a euismod massa egestas. Vivamus facilisis."; var app = ApplicationSays.From(id, name, payload); var raw = $"{OperationalMessage.APP}\nid=1 nm=node1 si={app.SaysId}\n{payload}"; _expectedBuffer.Write(Converters.TextToBytes(raw)); MessageConverters.MessageToBytes(app, _messageBuffer); Assert.Equal(_expectedBuffer.ToArray(), _messageBuffer.ToArray()); Assert.Equal(app, ApplicationSays.From(raw)); }
internal void DistributeRemoveTo(AttributeSet set, IEnumerable <Node> nodes) { // remove attributes first, then the set var allNodes = nodes as Node[] ?? nodes.ToArray(); foreach (var untracked in set.All) { DistributeTo(set, untracked, ApplicationMessageType.RemoveAttribute, allNodes); } var removeSet = new RemoveAttributeSet(_node, set); var confirmable = _confirmables.UnconfirmedFor(removeSet, allNodes); _outbound.Application(ApplicationSays.From(_node.Id, _node.Name, removeSet.ToPayload()), confirmable.UnconfirmedNodes); _application.InformAttributeSetRemoved(set.Name); }
public void Application(ApplicationSays says, IEnumerable <Node> unconfirmedNodes) { if (!actor.IsStopped) { Action <IOperationalOutboundStream> consumer = __ => __.Application(says, unconfirmedNodes); if (mailbox.IsPreallocated) { mailbox.Send(actor, consumer, null, ApplicationRepresentation2); } else { mailbox.Send(new LocalMessage <IOperationalOutboundStream>(actor, consumer, ApplicationRepresentation2)); } } else { actor.DeadLetters.FailedDelivery(new DeadLetter(actor, ApplicationRepresentation2)); } }
internal void DistributeTo( AttributeSet set, TrackedAttribute tracked, ApplicationMessageType type, IEnumerable <Node> nodes) { switch (type) { case ApplicationMessageType.AddAttribute: var add = AddAttribute.From(_node, set, tracked); var addConfirmable = _confirmables.UnconfirmedFor(add, nodes); _outbound.Application(ApplicationSays.From(_node.Id, _node.Name, add.ToPayload()), addConfirmable.UnconfirmedNodes); _application.InformAttributeAdded(set.Name !, tracked.Attribute?.Name); break; case ApplicationMessageType.RemoveAttribute: var remove = RemoveAttribute.From(_node, set, tracked); var removeConfirmable = _confirmables.UnconfirmedFor(remove, nodes); _outbound.Application(ApplicationSays.From(_node.Id, _node.Name, remove.ToPayload()), removeConfirmable.UnconfirmedNodes); _application.InformAttributeRemoved(set.Name !, tracked.Attribute?.Name); break; case ApplicationMessageType.RemoveAttributeSet: var removeSet = RemoveAttributeSet.From(_node, set); var removeSetConfirmable = _confirmables.UnconfirmedFor(removeSet, nodes); _outbound.Application(ApplicationSays.From(_node.Id, _node.Name, removeSet.ToPayload()), removeSetConfirmable.UnconfirmedNodes); _application.InformAttributeSetRemoved(set.Name); break; case ApplicationMessageType.ReplaceAttribute: var replace = ReplaceAttribute.From(_node, set, tracked); var replaceConfirmable = _confirmables.UnconfirmedFor(replace, nodes); _outbound.Application(ApplicationSays.From(_node.Id, _node.Name, replace.ToPayload()), replaceConfirmable.UnconfirmedNodes); _application.InformAttributeReplaced(set.Name !, tracked.Attribute?.Name); break; default: throw new InvalidOperationException("Cannot distribute unknown ApplicationMessageType."); } }
private Dictionary <string, string> ParsePayload(RawMessage message) { var map = new Dictionary <string, string>(); var says = ApplicationSays.From(message.AsTextMessage()); map.Add(SourceNodeIdKey, says.Id.ValueString()); map.Add(SourceNodeNameKey, says.Name.Value); var parsed = says.Payload.Split('\n'); map.Add(ClassOfMessageKey, parsed[0]); switch (parsed[0]) { case "ConfirmCreateAttributeSet": map.Add(CorrelatingMessageIdKey, parsed[1]); map.Add(TrackingIdKey, parsed[2]); map.Add(MessageTypeKey, parsed[3]); map.Add(AttributeSetNameKey, parsed[4]); break; case "ConfirmRemoveAttributeSet": map.Add(CorrelatingMessageIdKey, parsed[1]); map.Add(TrackingIdKey, parsed[2]); map.Add(MessageTypeKey, parsed[3]); map.Add(AttributeSetNameKey, parsed[4]); break; case "ConfirmAttribute": map.Add(CorrelatingMessageIdKey, parsed[1]); map.Add(TrackingIdKey, parsed[2]); map.Add(MessageTypeKey, parsed[3]); map.Add(AttributeSetNameKey, parsed[4]); map.Add(AttributeNameKey, parsed[5]); break; case "CreateAttributeSet": map.Add(TrackingIdKey, parsed[1]); map.Add(MessageTypeKey, parsed[2]); map.Add(AttributeSetNameKey, parsed[3]); break; case "RemoveAttributeSet": map.Add(TrackingIdKey, parsed[1]); map.Add(MessageTypeKey, parsed[2]); map.Add(AttributeSetNameKey, parsed[3]); break; case "AddAttribute": case "RemoveAttribute": case "ReplaceAttribute": map.Add(CorrelatingMessageIdKey, parsed[1]); map.Add(TrackingIdKey, parsed[2]); map.Add(MessageTypeKey, parsed[3]); map.Add(AttributeSetNameKey, parsed[4]); map.Add(AttributeNameKey, parsed[5]); map.Add(AttributeTypeKey, parsed[6]); map.Add(AttributeValueKey, parsed[7]); break; } return(map); }