public virtual void SendV1 <T>(IEnvelopeV1 <T> envelope) { if (!envelope.HasNextHop()) { if (this.Guid == envelope.HopsToDestination.First()) { this.ReceiveV1(envelope); } else { throw new InvalidOperationException("malformed envelope"); } } IDipNode nextHop; if (!m_peersByGuid.TryGetValue(envelope.GetNextHopGuid(), out nextHop)) { throw new InvalidOperationException("attempted to send envelope to non-connected peer"); } var nextEnvelope = envelope.GetEnvelopeForNextHop(); nextHop.Receive(nextEnvelope); }
public override void ReceiveV1 <T>(IEnvelopeV1 <T> envelope) { if (envelope.RecipientId.Guid == this.Guid) { throw new NotImplementedException("Message routing to Local Network"); } RouteEnvelope(envelope); }
public static IEnvelopeV1 <T> GetReroutedEnvelope <T>(this IEnvelopeV1 <T> envelope, Guid[] route) { return(new EnvelopeV1 <T>( envelope.SenderId, envelope.RecipientId, route, envelope.TimeSent, envelope.TimeReceived, envelope.Message )); }
public void Setup() { testObj = new LocalTerminal(configuration.Object); succeededPeeringResult = new PeeringResult(PeeringState.Connected, testObj); failedPeeringResult = new PeeringResult(PeeringState.Disconnected, testObj); otherTerminal.Setup((t) => t.PeerChildAsync(testObj)).Returns(Task.FromResult(succeededPeeringResult)); otherTerminal.Setup((t) => t.Guid).Returns(otherTerminalGuid); emptyMessageTimeSent = DateTime.UtcNow; emptyMessageTimeReceived = emptyMessageTimeSent + TimeSpan.FromSeconds(5); emptyMessageFromTestObj = new Message <object>(new object()); emptyMessageFromTestObjEnvelope = new EnvelopeV1 <object>(testObj.Identifier, otherTerminalIdentifier, new[] { otherTerminalGuid }, emptyMessageTimeSent, emptyMessageTimeReceived, emptyMessageFromTestObj); }
public override void ReceiveV1 <T>(IEnvelopeV1 <T> envelope) { if (envelope.RecipientId.Guid != this.Guid) { #if DEBUG throw new InvalidOperationException("Local Terminals shouldn't relay messages!"); #else RouteEnvelope(envelope); #endif } else { EnqueueMessage(envelope.Message); } }
protected void RouteEnvelope <T>(IEnvelopeV1 <T> envelope) { IDipNode nextHop; if (envelope.RecipientId.Guid == this.Identifier.Guid) { m_messageQueue.Add(envelope.Message); } else if (!envelope.HasNextHop() || !m_peersByGuid.TryGetValue(envelope.GetNextHopGuid(), out nextHop)) { RerouteEnvelope(envelope); } else { nextHop.Receive(envelope.GetEnvelopeForNextHop()); } }
private void RerouteEnvelope <T>(IEnvelopeV1 <T> envelope) { var thisCrumbs = this.Identifier.Breadcrumbs; var envelopeCrumbs = envelope.RecipientId.Breadcrumbs; // Find common ancestor between breadcrumbms int thisIndexResult = -1; int recipientIndexResult = -1; for (var thisIndex = thisCrumbs.Length - 1; thisIndex >= 0; thisIndex--) { for (var recipientIndex = 0; recipientIndex < envelopeCrumbs.Length; recipientIndex++) { if (thisCrumbs[thisIndex] == envelopeCrumbs[recipientIndex]) { thisIndexResult = thisIndex; recipientIndexResult = recipientIndex; goto loop_exit; } } } loop_exit: if (thisIndexResult == -1 || recipientIndexResult == -1) { return; // drop packet } var newRoute = new Guid[(thisCrumbs.Length - thisIndexResult - 1) + (envelopeCrumbs.Length - recipientIndexResult - 1) + 1]; var newRouteIndex = 0; for (var i = thisCrumbs.Length - 1; i >= thisIndexResult; i--) { newRoute[newRouteIndex++] = thisCrumbs[i]; } for (var i = recipientIndexResult + 1; i < envelopeCrumbs.Length; i++) { newRoute[newRouteIndex++] = envelopeCrumbs[i]; } RouteEnvelope(envelope.GetReroutedEnvelope(newRoute)); }
public static bool HasNextHop(this IEnvelopeV1 envelope) { return(envelope.HopsToDestination != null && envelope.HopsToDestination.Length > 1); }
public static IEnvelopeV1 <T> GetEnvelopeForNextHop <T>(this IEnvelopeV1 <T> envelope) { return(envelope.GetReroutedEnvelope(envelope.HopsToDestination.SubArray(1))); }
public static Guid GetNextHopGuid(this IEnvelopeV1 envelope) { return(envelope.HopsToDestination[1]); }
public abstract void ReceiveV1 <T>(IEnvelopeV1 <T> envelope);
public void Receive <T>(IEnvelopeV1 <T> envelope) { ReceiveV1(envelope); }
public void Send <T>(IEnvelopeV1 <T> envelope) { SendV1(envelope); }
public override void ReceiveV1 <T>(IEnvelopeV1 <T> envelope) { this.RouteEnvelope(envelope); }
public override void SendV1 <T>(IEnvelopeV1 <T> envelope) { this.RouteEnvelope(envelope); }