DBAccess.cs 1.93 KB
Newer Older
1

2
using Grpc.Net.Client;
3 4 5

namespace GrpcMessageNode.DataBaseAccess
{
6
    public class DBAccess
7
    {
8

9
        /// <summary>
10 11
        /// check message for tag , Authentication , Quota perhaps from service validator with address
        /// passed as a parameter to the function
12
        /// </summary>
13 14 15 16 17 18 19
        /// <param name="message" > the message we received to check</param>
        /// <param name="validatorAddress"> The validator service address which as a mongodb service </param>
        /// <returns> validator reply which is ok if we can proceed with account priority</returns>
        public static int getPriority(ref Message message , string validatorAddress)
        {
            //Console.WriteLine("Validating NOW IN GRPC !! ");
            ValidatorReply reply = ValidateAsync(message , validatorAddress);
20

21 22 23 24 25 26 27 28 29 30 31 32 33
            if (reply == null)
            {
                return -1;
            }
            if (!reply.ReplyCode.Contains("ok"))
            {
                return -1;
            }

            return reply.AccountPriority;
        }

        private static ValidatorReply ValidateAsync(Message message, string validatorAddress)
34
        {
35 36 37 38 39 40 41 42 43 44
            using var channel = GrpcChannel.ForAddress(validatorAddress);
            var client = new Validate.ValidateClient(channel);
            MessageMetaData messageMeta = extractMetaData(message);
            //Console.WriteLine("Calling GRPC for address = " + validatorAddress);
            var reply =  client.ValidateMessageAsync(messageMeta);
            
            var ans = reply.GetAwaiter().GetResult();
            
            return ans;

45 46
        }

47 48 49 50 51 52 53 54
        private static MessageMetaData extractMetaData(Message message)
        {
            MessageMetaData metaData = new MessageMetaData();
            metaData.Tag = message.Tag;
            metaData.ApiKey = message.ApiKey;
            metaData.ClientID = message.ClientID;
            return metaData;
        }
55 56
    }
}