-
Notifications
You must be signed in to change notification settings - Fork 3
[Communication] PR1: Added Base Files for Mailbox #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cdf913d
53cd702
0ff1f92
7e407e5
5814aa6
ae73009
90490c0
96cabf4
ebff605
a7438b5
668fe70
9960ee9
b180ffa
fa84cde
640c5aa
0a03fe4
9f05642
771e3f6
93808a9
d3de35d
70e14d3
f2d5195
fa16266
2b73870
11a25d8
43f2e16
c712399
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Interface for a message payload. | ||
|
|
||
| public interface IMessagePayload {} | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // The message is a base class for the different types of messages being sent and received among the | ||
| // agents. It always carries a sender, a receiver, a message type, and a payload. | ||
|
|
||
| using System; | ||
|
|
||
| // Message type enumeration that defines the message payload. | ||
| public enum MessageType { | ||
| AssignTargetRequest, | ||
| AssignTargetResponse, | ||
| ReassignTargetRequest, | ||
| } | ||
|
|
||
| public abstract class Message { | ||
| public IAgent Sender { get; } | ||
| public IAgent Receiver { get; } | ||
| public MessageType Type { get; } | ||
|
|
||
| public abstract IMessagePayload Payload { get; } | ||
|
|
||
| protected Message(IAgent sender, IAgent receiver, MessageType type) { | ||
| Sender = sender ?? throw new ArgumentNullException(nameof(sender)); | ||
| Receiver = receiver ?? throw new ArgumentNullException(nameof(receiver)); | ||
| Type = type; | ||
| } | ||
| } | ||
|
|
||
| // Generic message that stores a payload. The generic type refers to the payloa type. | ||
| public abstract class Message<TPayload> : Message | ||
| where TPayload : class, IMessagePayload { | ||
| public TPayload PayloadData { get; } | ||
| public sealed override IMessagePayload Payload => PayloadData; | ||
| protected Message(IAgent sender, IAgent receiver, MessageType type, TPayload payload) | ||
| : base(sender, receiver, type) { | ||
| PayloadData = payload ?? throw new ArgumentNullException(nameof(payload)); | ||
| } | ||
| } | ||
|
|
||
| // This message is sent upwards to a parent interceptor or IADS when a sub-interceptor has no target | ||
| // and is requesting a new target. | ||
| public sealed class AssignTargetRequestMessage : Message<AssignTargetRequestPayload> { | ||
| public AssignTargetRequestMessage(IAgent sender, IAgent receiver, IInterceptor subInterceptor) | ||
| : base(sender, receiver, MessageType.AssignTargetRequest, | ||
| new AssignTargetRequestPayload(subInterceptor)) {} | ||
| } | ||
|
|
||
| // This message is sent downwards from the IADS or a parent interceptor to inform the | ||
| // sub-interceptor of a new target. | ||
| public sealed class AssignTargetResponseMessage : Message<AssignTargetResponsePayload> { | ||
| public AssignTargetResponseMessage(IAgent sender, IAgent receiver, IHierarchical target) | ||
| : base(sender, receiver, MessageType.AssignTargetResponse, | ||
| new AssignTargetResponsePayload(target)) {} | ||
| } | ||
|
|
||
| // This message is sent upwards to a parent interceptor or IADS when a sub-interceptor can no longer | ||
| // pursue the current target and is requesting the parent interceptor or IADS to reassign that | ||
| // target elsewhere. | ||
| public sealed class ReassignTargetRequestMessage : Message<ReassignTargetRequestPayload> { | ||
| public ReassignTargetRequestMessage(IAgent sender, IAgent receiver, IHierarchical target) | ||
| : base(sender, receiver, MessageType.ReassignTargetRequest, | ||
| new ReassignTargetRequestPayload(target)) {} | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // The message payload defines different types of payloads carried by messages. | ||
|
|
||
| using System; | ||
|
|
||
| // The payload carries the sub-interceptor that is requesting a parent interceptor or IADS for a new | ||
| // target assignment. | ||
| public sealed class AssignTargetRequestPayload : IMessagePayload { | ||
| public IInterceptor SubInterceptor { get; } | ||
|
|
||
| public AssignTargetRequestPayload(IInterceptor subInterceptor) { | ||
| SubInterceptor = subInterceptor ?? throw new ArgumentNullException(nameof(subInterceptor)); | ||
| } | ||
| } | ||
|
|
||
| // The payload carries the target that a parent interceptor or IADS has selected for a requesting | ||
| // sub-interceptor. | ||
| public sealed class AssignTargetResponsePayload : IMessagePayload { | ||
| public IHierarchical Target { get; } | ||
|
|
||
| public AssignTargetResponsePayload(IHierarchical target) { | ||
| Target = target ?? throw new ArgumentNullException(nameof(target)); | ||
| } | ||
| } | ||
|
|
||
| // The payload carries the target that a sub-interceptor is requesting its parent interceptor or | ||
| // IADS to reassign. | ||
| public sealed class ReassignTargetRequestPayload : IMessagePayload { | ||
| public IHierarchical Target { get; } | ||
|
|
||
| public ReassignTargetRequestPayload(IHierarchical target) { | ||
| Target = target ?? throw new ArgumentNullException(nameof(target)); | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // The pending message is the mailbox's internal queue item. It stores the message object and the | ||
| // scheduled delivery time in simulation seconds. The mailbox dequeues this item once the simulation | ||
| // time reaches the deliver at time. | ||
|
|
||
| using System; | ||
|
|
||
| public readonly struct PendingMessage : IComparable<PendingMessage> { | ||
| public Message Message { get; } | ||
|
|
||
| // Absolute simulation time in seconds when the mailbox should deliver this message. | ||
| public float DeliverAt { get; } | ||
|
|
||
| public IAgent Sender => Message?.Sender; | ||
|
Joseph0120 marked this conversation as resolved.
|
||
| public IAgent Receiver => Message?.Receiver; | ||
|
Joseph0120 marked this conversation as resolved.
|
||
|
|
||
| public PendingMessage(Message message, float deliverAt) { | ||
| if (float.IsNaN(deliverAt) || float.IsInfinity(deliverAt) || deliverAt < 0f) { | ||
| throw new ArgumentOutOfRangeException(nameof(deliverAt), deliverAt, | ||
| "DeliverAt must be finite and non-negative."); | ||
| } | ||
| Message = message ?? throw new ArgumentNullException(nameof(message)); | ||
| DeliverAt = deliverAt; | ||
| } | ||
|
|
||
| // Pending messages are sorted based on the deliver at time. | ||
| public int CompareTo(PendingMessage other) => DeliverAt.CompareTo(other.DeliverAt); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,4 +33,4 @@ | |
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.