コード例 #1
0
        public void TestZipUnzipRandomSucceeds()
        {
            //// ARRANGE

            var source = new byte[992];

            var r = new Random(42);

            r.NextBytes(source);

            //// ACT

            var deflatedSource = DeflateHelper.Zip(source);

            Assert.AreEqual(997, deflatedSource.Length);

            var unzipped = DeflateHelper.Unzip(deflatedSource);

            //// ASSERT

            Assert.AreEqual(992, unzipped.Length);

            for (int i = 0; i < 992; i++)
            {
                Assert.AreEqual(source[i], unzipped[i]);
            }
        }
コード例 #2
0
        public void TestZipUnzipSucceeds()
        {
            //// ARRANGE

            var source = new byte[992];

            for (int i = 0; i < source.Length; i++)
            {
                source[i] = 42;
            }

            //// ACT

            var deflatedSource = DeflateHelper.Zip(source);

            Assert.AreEqual(11, deflatedSource.Length);

            var unzipped = DeflateHelper.Unzip(deflatedSource);

            //// ASSERT

            Assert.AreEqual(992, unzipped.Length);

            for (int i = 0; i < 992; i++)
            {
                Assert.AreEqual(source[i], unzipped[i]);
            }
        }
コード例 #3
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>
        private static async Task <MessageResponse> PipeMessageInputOne(Message message, object userContext)
        {
            int counterValue = Interlocked.Increment(ref counter);

            var moduleClient = userContext as ModuleClient;

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

            var connectionDeviceId = message.ConnectionDeviceId;

            var connectionModuleId = message.ConnectionModuleId;

            byte[] messageBytes = message.GetBytes();

            if (message.ContentEncoding == "gzip" &&
                message.ContentType == "application/zip")
            {
                var zippedLength = messageBytes.Length;
                messageBytes = GZipHelper.Unzip(messageBytes);
                System.Console.WriteLine($"Uncompressed from GZIP {zippedLength} bytes to {messageBytes.Length} bytes");
            }

            if (message.ContentEncoding == "deflate" &&
                message.ContentType == "application/zip")
            {
                var zippedLength = messageBytes.Length;
                messageBytes = DeflateHelper.Unzip(messageBytes);
                System.Console.WriteLine($"Uncompressed from Deflate {zippedLength} bytes to {messageBytes.Length} bytes");
            }

            string messageString = Encoding.UTF8.GetString(messageBytes);

            Console.WriteLine($"-> Received echo message: {counterValue}, Body: '{messageString}'");

            if (!string.IsNullOrEmpty(messageString))
            {
                var messageBody = JsonConvert.DeserializeObject(messageString);

                var moduleOutput = _moduleOutputs.GetModuleOutput("output1");

                if (moduleOutput != null)
                {
                    moduleOutput.Properties.Clear();

                    foreach (var prop in message.Properties)
                    {
                        moduleOutput.Properties.Add(prop.Key, prop.Value);

                        Console.WriteLine($"Property added: key:'{prop.Key}' value:'{prop.Value}'");
                    }

                    if (!string.IsNullOrEmpty(connectionDeviceId))
                    {
                        Console.WriteLine($"connectionDeviceId: '{connectionDeviceId}'");
                    }

                    if (!string.IsNullOrEmpty(connectionModuleId))
                    {
                        Console.WriteLine($"ConnectionModuleId: '{connectionModuleId}'");
                    }

                    await moduleOutput.SendMessage(messageBody);

                    Console.WriteLine("Received message echoed");
                }
            }

            return(MessageResponse.Completed);
        }