async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            var messageString = Encoding.UTF8.GetString(message.Body);

            logger.LogInformation("Recieved new block");
            logger.LogInformation(messageString);

            var jsonstring = Encoding.UTF8.GetString(message.Body);
            var block      = JsonConvert.DeserializeObject <Block>(jsonstring);

            var lastBlock = blockchainRepository.GetLastBlock();


            if (block.index > lastBlock.index)
            {
                blockchainRepository.Add(block);
            }
        }
Exemple #2
0
        private async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            logger.LogInformation("Recieved a message of type: " + message.UserProperties["Type"].ToString());

            string messageTypeString = message.UserProperties["Type"].ToString();
            string sender            = message.UserProperties["Sender"].ToString();


            switch (Enum.Parse(typeof(MessageType), messageTypeString))
            {
            case MessageType.BlockchainSyncRequest:
                Console.WriteLine("Get sync request");

                var messageList = new List <Message>();
                foreach (Block b in blockchainRepository.Get())
                {
                    var objectString = JsonConvert.SerializeObject(b);
                    var m            = new Message(Encoding.UTF8.GetBytes(objectString));
                    m.UserProperties.Add("Sender", config["BlockchainNodeSettings:NodeName"]);
                    m.UserProperties.Add("Type", MessageType.BlockchainSyncResponse.ToString());
                    //m.MessageId = b.index.ToString();

                    messageList.Add(m);
                }

                IQueueClient queueClient = new QueueClient(config.GetConnectionString("AzureServiceBus"), sender);
                await queueClient.SendAsync(messageList);

                await queueClient.CloseAsync();

                break;

            case MessageType.BlockchainSyncResponse:
                Console.WriteLine(message.Body);

                var jsonstring = Encoding.UTF8.GetString(message.Body);

                var block = JsonConvert.DeserializeObject <Block>(jsonstring);
                blockchainRepository.Add(block);

                break;
            }
        }