Beispiel #1
0
        /// <summary>
        /// This method is called whenever the module is sent a message from the EdgeHub.
        /// It just pipe the messages without any change.
        /// It prints all the incoming messages.
        /// </summary>
        static async Task <MessageResponse> DecompressMessage(Message message, object userContext)
        {
            var moduleClient = userContext as ModuleClient;

            if (moduleClient == null)
            {
                throw new InvalidOperationException("UserContext doesn't contain " + "expected values");
            }

            await moduleClient.CompleteAsync(message);

            byte[] messageBytes = message.GetBytes();
            if (message.Properties.Contains(new System.Collections.Generic.KeyValuePair <string, string>("compression", "gzip")))
            {
                Console.WriteLine($"Received compressed message with body size: {messageBytes.Length}");

                if (messageBytes.Length > 0)
                {
                    var decompressedMessageData = CompressionClass.Decompress(messageBytes);
                    var outMessage = new Message(decompressedMessageData);
                    foreach (var prop in message.Properties)
                    {
                        outMessage.Properties.Add(prop.Key, prop.Value);
                    }
                    await moduleClient.SendEventAsync("decompressMessageOutput", outMessage);

                    Console.WriteLine($"Sent decompressed message sent with body size: {decompressedMessageData.Length}");
                }
                else
                {
                    //If message body is 0 then pass through with no changes
                    var outMessage = new Message(messageBytes);
                    foreach (var prop in message.Properties)
                    {
                        outMessage.Properties.Add(prop.Key, prop.Value);
                    }
                    await moduleClient.SendEventAsync("compressMessageOutput", outMessage);

                    Console.WriteLine("Message had no body and was passed as-is.");
                }
            }
            else
            {
                var outMessage = new Message(messageBytes);
                foreach (var prop in message.Properties)
                {
                    outMessage.Properties.Add(prop.Key, prop.Value);
                }
                await moduleClient.SendEventAsync("decompressMessageOutput", outMessage);

                Console.WriteLine("Message was not compressed and was passed as-is.");
            }
            return(MessageResponse.None);
        }
Beispiel #2
0
        /// <summary>
        /// This Compress message method is called whenever the module is sent a message from the EdgeHub.
        /// It just pipe the messages without any change.
        /// It prints all the incoming messages.
        /// </summary>
        static async Task <MessageResponse> CompressMessage(Message message, object userContext)
        {
            var moduleClient = userContext as ModuleClient;

            if (moduleClient == null)
            {
                throw new InvalidOperationException("UserContext doesn't contain expected values");
            }

            await moduleClient.CompleteAsync(message);

            byte[] messageBytes = message.GetBytes(); //returns the BodyStream of the message as bytes

            Console.WriteLine($"Received message with body size: {messageBytes.Length}");

            if (messageBytes.Length > 0)
            {
                var compressedMessageData = CompressionClass.Compress(messageBytes);
                var outMessage            = new Message(compressedMessageData);
                // copy properties from original message to new message
                foreach (var prop in message.Properties)
                {
                    outMessage.Properties.Add(prop.Key, prop.Value);
                }
                // add a new property indicating the message is compressed with gzip so the backend can handle compressed and non-compressed messages
                outMessage.Properties.Add("compression", "gzip");
                await moduleClient.SendEventAsync("compressMessageOutput", outMessage);

                Console.WriteLine($"Sent compressed message with body size: {compressedMessageData.Length}");
            }
            else
            {
                var outMessage = new Message(messageBytes);
                foreach (var prop in message.Properties)
                {
                    outMessage.Properties.Add(prop.Key, prop.Value);
                }
                await moduleClient.SendEventAsync("compressMessageOutput", outMessage);

                Console.WriteLine("Message had no body and was passed as-is.");
            }
            return(MessageResponse.None);
        }
 public static async Task Run(
     [IoTHubTrigger("messages/events", ConsumerGroup = "$Default", Connection = "IoTHubEventHubEndpoint")] EventData message,
     [Blob("test-out/{sys.randguid}.xml", FileAccess.Write, Connection = "OutputBlobConnectionString")] Stream output,
     ILogger log)
 {
     if (message.Properties.Contains(new System.Collections.Generic.KeyValuePair <string, object>("compression", "gzip")))
     {
         var fncResult = CompressionClass.Decompress(message.Body.ToArray());
         log.LogInformation($"Decompressed message: {Encoding.UTF8.GetString(fncResult)}");
         try
         {
             await output.WriteAsync(fncResult, 0, fncResult.Length);
         }
         catch (Exception e)
         {
             log.LogError(e.Message);
         }
     }
     else
     {
         log.LogInformation($"Received uncompressed message: {Encoding.UTF8.GetString(message.Body.ToArray())}");
         await output.WriteAsync(message.Body.ToArray(), 0, message.Body.Count);
     }
 }
Beispiel #4
0
 public void Decompress_External_GZip_Data_Returns_Original_Data()
 {
     Assert.Equal(decompressedContentData, CompressionClass.Decompress(compressedContentData));
 }
Beispiel #5
0
 public void Decompress_Empty_Data_Returns_Empty_Data()
 {
     Assert.Equal(emptyMsgData, CompressionClass.Decompress(emptyMsgData));
 }
Beispiel #6
0
        public void Compress_Decompress_Returns_Original_Data()
        {
            var compressedData = CompressionClass.Compress(decompressedContentData);

            Assert.Equal(decompressedContentData, CompressionClass.Decompress(compressedData));
        }
Beispiel #7
0
 public void Compress_Empty_Buffer_Returns_Empty_Buffer()
 {
     Assert.Equal(emptyMsgData, CompressionClass.Compress(emptyMsgData));
 }