using System; using PepperDash.Core; namespace PepperDash_Essentials_Core.Queues { /// /// IBasicCommunication Message for IQueue /// public class ComsMessage : IQueueMessage { private readonly byte[] _bytes; private readonly IBasicCommunication _coms; private readonly string _string; private readonly bool _isByteMessage; /// /// Constructor for a string message /// /// IBasicCommunication to send the message /// Message to send public ComsMessage(IBasicCommunication coms, string message) { Validate(coms, message); _coms = coms; _string = message; } /// /// Constructor for a byte message /// /// IBasicCommunication to send the message /// Message to send public ComsMessage(IBasicCommunication coms, byte[] message) { Validate(coms, message); _coms = coms; _bytes = message; _isByteMessage = true; } private void Validate(IBasicCommunication coms, object message) { if (_coms == null) throw new ArgumentNullException("coms"); if (message == null) throw new ArgumentNullException("message"); } /// /// Dispatchs the string/byte[] to the IBasicCommunication specified /// public void Dispatch() { if (_isByteMessage) { _coms.SendBytes(_bytes); } else { _coms.SendText(_string); } } /// /// Shows either the byte[] or string to be sent /// public override string ToString() { return _bytes != null ? _bytes.ToString() : _string; } } }