public void EtpExtensions_CreateMessageKey_Creates_Discovery_Key() { long expectedKey, actualKey; expectedKey = (3L << 32) + 1; actualKey = EtpExtensions.CreateMessageKey((int)v11.Protocols.Discovery, (int)v11.MessageTypes.Discovery.GetResources); Assert.AreEqual(expectedKey, actualKey); expectedKey = (3L << 32) + 1; actualKey = EtpExtensions.CreateMessageKey((int)v12.Protocols.Discovery, (int)v12.MessageTypes.Discovery.GetResources); Assert.AreEqual(expectedKey, actualKey); expectedKey = 1000L; actualKey = EtpExtensions.CreateMessageKey((int)v11.Protocols.Discovery, (int)v11.MessageTypes.Core.ProtocolException); Assert.AreEqual(expectedKey, actualKey); expectedKey = 1000L; actualKey = EtpExtensions.CreateMessageKey((int)v12.Protocols.Discovery, (int)v12.MessageTypes.Core.ProtocolException); Assert.AreEqual(expectedKey, actualKey); expectedKey = 1001L; actualKey = EtpExtensions.CreateMessageKey((int)v11.Protocols.Discovery, (int)v11.MessageTypes.Core.Acknowledge); Assert.AreEqual(expectedKey, actualKey); expectedKey = 1001L; actualKey = EtpExtensions.CreateMessageKey((int)v12.Protocols.Discovery, (int)v12.MessageTypes.Core.Acknowledge); Assert.AreEqual(expectedKey, actualKey); }
/// <summary> /// Overrides an existing message decoder so that it can return a generic interface. /// </summary> /// <typeparam name="TInterface">The type of the message body interface.</typeparam> /// <typeparam name="TBody">The type of the decoded message.</typeparam> /// <param name="protocol"></param> /// <param name="messageType"></param> protected void RegisterMessageDecoderOverride <TInterface, TBody>(object protocol, object messageType) where TInterface : IEtpMessageBody where TBody : class, TInterface, new() { var messageKey = EtpExtensions.CreateMessageKey(Convert.ToInt32(protocol), Convert.ToInt32(messageType)); MessageDecoders[messageKey] = (d, h, x) => CreateMessage <TInterface, TBody>(h, d.DecodeAvroObject <TBody>(), extension: x); MessageDecodersByType[typeof(TInterface)] = MessageDecoders[messageKey]; }
public void EtpExtensions_CreateMessageKey_Creates_Core_Key() { long expectedKey, actualKey; expectedKey = 1L; actualKey = EtpExtensions.CreateMessageKey((int)v11.Protocols.Core, (int)v11.MessageTypes.Core.RequestSession); Assert.AreEqual(expectedKey, actualKey); expectedKey = 1L; actualKey = EtpExtensions.CreateMessageKey((int)v12.Protocols.Core, (int)v12.MessageTypes.Core.RequestSession); Assert.AreEqual(expectedKey, actualKey); expectedKey = 1000L; actualKey = EtpExtensions.CreateMessageKey((int)v11.Protocols.Core, (int)v11.MessageTypes.Core.ProtocolException); Assert.AreEqual(expectedKey, actualKey); expectedKey = 1000L; actualKey = EtpExtensions.CreateMessageKey((int)v12.Protocols.Core, (int)v12.MessageTypes.Core.ProtocolException); Assert.AreEqual(expectedKey, actualKey); expectedKey = 1001L; actualKey = EtpExtensions.CreateMessageKey((int)v11.Protocols.Core, (int)v11.MessageTypes.Core.Acknowledge); Assert.AreEqual(expectedKey, actualKey); expectedKey = 1001L; actualKey = EtpExtensions.CreateMessageKey((int)v12.Protocols.Core, (int)v12.MessageTypes.Core.Acknowledge); Assert.AreEqual(expectedKey, actualKey); }
/// <summary> /// Registers a function that decodes a particular message type. /// </summary> /// <typeparam name="T">The type of the decoded message.</typeparam> /// <param name="protocol">The message's protocol.</param> /// <param name="messageType">The type of the message.</param> /// <exception cref="ArgumentException">More than one decoder for the same protocol message is added.</exception> public void RegisterMessageDecoder <T>(object protocol, object messageType) where T : class, IEtpMessageBody, new() { var messageKey = EtpExtensions.CreateMessageKey(Convert.ToInt32(protocol), Convert.ToInt32(messageType)); if (MessageDecoders.ContainsKey(messageKey)) { throw new ArgumentException($"Duplicate decoder: Protocol: {protocol}; Message Type: {messageType}", "messageType"); } RegisterMessageDecoderOverride <T, T>(protocol, messageType); }
/// <summary> /// Registers a function that decodes a particular message type. /// </summary> /// <typeparam name="T">The type of the decoded message.</typeparam> /// <param name="protocol">The message's protocol.</param> /// <param name="messageType">The type of the message.</param> /// <exception cref="ArgumentException">More than one decoder for the same protocol message is added.</exception> public void RegisterMessageDecoder <T>(object protocol, object messageType) where T : ISpecificRecord { var messageKey = EtpExtensions.CreateMessageKey(Convert.ToInt32(protocol), Convert.ToInt32(messageType)); if (MessageDecoders.ContainsKey(messageKey)) { throw new ArgumentException($"Duplicate decoder: Protocol: {protocol}; Message Type: {messageType}", "messageType"); } MessageDecoders[messageKey] = (d, b) => EtpExtensions.Decode <T>(d, b); MessageDecodersByType[typeof(T)] = MessageDecoders[messageKey]; }
/// <summary> /// Decodes the message type from the specified protocol. /// </summary> /// <param name="protocol">The message's protocol.</param> /// <param name="messageType">The type of the message.</param> /// <param name="decoder">The decoder with binary message data.</param> /// <param name="body">The string with json message data.</param> /// <returns>The message.</returns> public ISpecificRecord DecodeMessage(int protocol, int messageType, Decoder decoder, string body) { var messageKey = EtpExtensions.CreateMessageKey(protocol, messageType); Func <Decoder, string, ISpecificRecord> messageDecoder; if (!MessageDecoders.TryGetValue(messageKey, out messageDecoder)) { return(null); } return(messageDecoder(decoder, body)); }
/// <summary> /// Decodes the message. /// </summary> /// <param name="header">The message header</param> /// <param name="extension">The header extension</param> /// <param name="decoder">The decoder with binary message data.</param> /// <returns>The message if successful; <c>null</c> otherwise.</returns> public EtpMessage DecodeMessage(IMessageHeader header, IMessageHeaderExtension extension, IAvroDecoder decoder) { var messageKey = EtpExtensions.CreateMessageKey(header.Protocol, header.MessageType); Func <IAvroDecoder, IMessageHeader, IMessageHeaderExtension, EtpMessage> messageDecoder; if (!MessageDecoders.TryGetValue(messageKey, out messageDecoder)) { return(null); } return(messageDecoder(decoder, header, extension)); }
/// <summary> /// Invokes the message handler for the specified message type. /// </summary> /// <param name="header">The message header.</param> /// <param name="body">The message body.</param> /// <returns><c>true</c> if a handler for the message was invoked; <c>false</c> otherwise.</returns> public bool InvokeMessageHandler(IMessageHeader header, ISpecificRecord body) { if (header == null) { return(false); } var messageKey = EtpExtensions.CreateMessageKey(header.Protocol, header.MessageType); Action <IMessageHeader, ISpecificRecord> messageHandler; if (!MessageHandlers.TryGetValue(messageKey, out messageHandler)) { return(false); } messageHandler(header, body); return(true); }
/// <summary> /// Checks whether a decoder is registered for the specified protocol message. /// </summary> /// <param name="protocol">The message protocol.</param> /// <param name="messageType">The message type.</param> /// <returns><c>true</c> if there is a message decoder registered; <c>false</c> otherwise.</returns> public bool IsMessageDecoderRegistered(object protocol, object messageType) { var messageKey = EtpExtensions.CreateMessageKey(Convert.ToInt32(protocol), Convert.ToInt32(messageType)); return(MessageDecoders.ContainsKey(messageKey)); }
/// <summary> /// Registers a handler for the specific protocol message. /// </summary> /// <typeparam name="T">The message type.</typeparam> /// <param name="protocol">The message protocol.</param> /// <param name="messageType">The type of the message.</param> /// <param name="messageHandler">The protocol message handler.</param> /// <remarks>If more than one handler is registered for the same protocol message, the last registered handler will be used.</remarks> public void RegisterMessageHandler <T>(object protocol, object messageType, Action <IMessageHeader, T> messageHandler) where T : ISpecificRecord { var messageKey = EtpExtensions.CreateMessageKey(Convert.ToInt32(protocol), Convert.ToInt32(messageType)); MessageHandlers[messageKey] = (h, b) => messageHandler(h, (T)b); }