Beispiel #1
0
        static async Task MaxNumber(Channel channel)
        {
            WriteHeader("MaxNumber");
            var maxClient           = new CalculatorService.CalculatorServiceClient(channel);
            var maxStream           = maxClient.FindMax();
            var maxStreamReaderTask = Task.Run(async() =>
            {
                while (await maxStream.ResponseStream.MoveNext())
                {
                    Console.WriteLine($"Next max found: {maxStream.ResponseStream.Current.Max}");
                }
            });

            for (var i = 0; i < 100; i++)
            {
                var testValue = new Random().Next(1, 100);
                Console.WriteLine($"testing {testValue}");
                var request = new FindMaxRequest {
                    Value = testValue
                };
                await maxStream.RequestStream.WriteAsync(request);

                await Task.Delay(200);
            }
            await maxStream.RequestStream.CompleteAsync();

            await maxStreamReaderTask;
        }
Beispiel #2
0
        public static async Task FindMaxNumber(CalculatorService.CalculatorServiceClient client)
        {
            var stream = client.FindMaxNumber();

            var responseReaderTask = Task.Run(async() =>
            {
                while (await stream.ResponseStream.MoveNext())
                {
                    Console.WriteLine(stream.ResponseStream.Current.Max);
                }
            });

            List <int> numberList = new List <int>()
            {
                1, 5, 3, 6, 2, 20
            };

            foreach (var number in numberList)
            {
                await stream.RequestStream.WriteAsync(new FindMaxNumberRequest()
                {
                    Number = number
                });
            }

            await stream.RequestStream.CompleteAsync();

            await responseReaderTask;
        }
Beispiel #3
0
        static async Task Main(string[] args)
        {
            Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
            var     client  = new CalculatorService.CalculatorServiceClient(channel);

            var result = await client.AddAsync(new AddRequest
            {
                No1 = 1,
                No2 = 2,
            });

            var result1 = await client.AddAsync(new AddRequest
            {
                No1 = 2,
                No2 = 5,
            });

            var result2 = await client.AddAsync(new AddRequest
            {
                No1 = 5,
                No2 = 10,
            });

            Console.WriteLine(result);
            Console.WriteLine(result1);
            Console.WriteLine(result2);

            await channel.ShutdownAsync();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Channel channel = new Channel(target, ChannelCredentials.Insecure);

            channel.ConnectAsync().ContinueWith((task) =>
            {
                if (task.Status == TaskStatus.RanToCompletion)
                {
                    Console.WriteLine("The client connected successfully");
                }
            });

            var client = new CalculatorService.CalculatorServiceClient(channel);

            var request = new SumRequest()
            {
                A = 3,
                B = 10
            };

            var response = client.Sum(request);

            Console.WriteLine(response.Result);

            channel.ShutdownAsync().Wait();
            Console.ReadKey();
        }
Beispiel #5
0
        private static void Add(CalculatorService.CalculatorServiceClient calculatorClient)
        {
            var addRequest = new AddRequest()
            {
                A = 32, B = 23
            };
            var addResponse = calculatorClient.Add(addRequest);

            Console.WriteLine(addResponse.Result);
        }
Beispiel #6
0
        static async Task Sum(Channel channel)
        {
            WriteHeader("Sum");
            var client     = new CalculatorService.CalculatorServiceClient(channel);
            var sumRequest = new SumRequest {
                A = 10, B = 3
            };
            var response = await client.SumAsync(sumRequest);

            Console.WriteLine($"Response received: {response.Result}\r\n");
        }
Beispiel #7
0
        private static async Task PrimeNoDecomp(CalculatorService.CalculatorServiceClient calculatorClient)
        {
            var primeNoDecompRequest = new PrimeNoDecompRequest()
            {
                Number = 120
            };
            var PrimeNoDecompResponse = calculatorClient.PrimeNoDecomp(primeNoDecompRequest);

            while (await PrimeNoDecompResponse.ResponseStream.MoveNext())
            {
                Console.WriteLine(PrimeNoDecompResponse.ResponseStream.Current.Factor);
                await Task.Delay(200);
            }
        }
Beispiel #8
0
        //const string target = "127.0.0.1:50052";

        static async Task Main(string[] args)
        {
            var clientCertificate = File.ReadAllText("ssl/client.crt");
            var clientKey         = File.ReadAllText("ssl/client.key");
            var caCertificate     = File.ReadAllText("ssl/ca.crt");

            var channelCredentials = new SslCredentials(caCertificate, new KeyCertificatePair(clientCertificate, clientKey));

            //Channel channel = new Channel(target, channelCredentials);
            Channel channel = new Channel("localhost", 50052, ChannelCredentials.Insecure);

            await channel.ConnectAsync().ContinueWith((task) =>
            {
                if (task.Status == TaskStatus.RanToCompletion)
                {
                    Console.WriteLine("The client connected successfully");
                }
            });

            var dummyClient = new DummyService.DummyServiceClient(channel);

            var greetingClient = new GreetingService.GreetingServiceClient(channel);

            Greet(greetingClient);
            //await GreetMany(greetingClient);
            //await LongGreet(greetingClient);
            //await GreetEveryOne(greetingClient);
            //GreetWithDeadline(greetingClient);

            var calculatorClient = new CalculatorService.CalculatorServiceClient(channel);
            //Add(calculatorClient);
            //await PrimeNoDecomp(calculatorClient);
            //await ComputeAverage(calculatorClient);
            //await FindMaximum(calculatorClient);
            //Sqrt(calculatorClient);

            var blogClient = new BlogService.BlogServiceClient(channel);

            //var newBlog = CreateBlog(blogClient);
            //ReadBlog(blogClient);
            //UpdateBlog(blogClient, newBlog);
            //DeleteBlog(blogClient, newBlog);
            await ListBlog(blogClient);

            channel.ShutdownAsync().Wait();
            Console.ReadKey();
        }
Beispiel #9
0
        private static async Task ComputeAverage(CalculatorService.CalculatorServiceClient calculatorClient)
        {
            var averageStream = calculatorClient.ComputeAverage();

            foreach (var no in Enumerable.Range(1, 4))
            {
                await averageStream.RequestStream.WriteAsync(new AverageRequest()
                {
                    Input = no
                });
            }
            await averageStream.RequestStream.CompleteAsync();

            var averageResponse = await averageStream.ResponseAsync;

            Console.WriteLine(averageResponse.Average);
        }
Beispiel #10
0
        public static void Calc(CalculatorService.CalculatorServiceClient client)
        {
            var calc = new Calculator
            {
                FirstNumber  = 10,
                SecondNumber = 10
            };

            var calcRequest = new CalculatorRequest()
            {
                Calculator = calc
            };

            var responseCalc = client.Calc(calcRequest);

            Console.WriteLine(responseCalc.Result);
        }
        static void Main(string[] args)
        {
            using var grpcChannel = GrpcChannel.ForAddress("https://localhost:5001/");

            var client = new CalculatorService.CalculatorServiceClient(grpcChannel);

            SumRequest sumRequest = new()
            {
                FirstNumber  = 105600,
                SecondNumber = 89700
            };

            SumResponse sumResponse = client.Sum(sumRequest);

            Console.WriteLine(sumResponse.Result);
            Console.ReadKey();
        }
    }
Beispiel #12
0
 private static void Sqrt(CalculatorService.CalculatorServiceClient calculatorClient)
 {
     try
     {
         var response = calculatorClient.Sqrt(new SqrtRequest()
         {
             Square = 16
         });
         Console.WriteLine(response.SquareRoot);
     }
     catch (RpcException ex)
     {
         Console.WriteLine($"Error: {ex.Status.Detail}");
         Console.WriteLine($"Error Status: {ex.Status.StatusCode}, Error Status Code: {(int)ex.Status.StatusCode}");
         if (ex.Status.StatusCode == StatusCode.InvalidArgument)
         {
         }
     }
 }
Beispiel #13
0
        static async Task Main(string[] args)
        {
            // Using Microsoft's Grpc
            using var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client = new CalculatorService.CalculatorServiceClient(channel);

            const int n1  = 34;
            const int n2  = 76;
            var       sum = client.Add(new CalculatorRequest {
                N1 = n1, N2 = n2
            });

            Console.WriteLine($"Called service: {n1} + {n2} = {sum.Result}");

            var difference = client.Subtract(new CalculatorRequest {
                N1 = n1, N2 = n2
            });

            Console.WriteLine($"Called service: {n1} - {n2} = {difference.Result}");

            var product = client.Multiply(new CalculatorRequest {
                N1 = n1, N2 = n2
            });

            Console.WriteLine($"Called service: {n1} * {n2} = {product.Result}");

            var division = client.Divide(new CalculatorRequest {
                N1 = n1, N2 = n2
            });

            Console.WriteLine($"Called service: {n1} / {n2} = {division.Result}");

            var divisionAsync = await client.DivideAsync(new CalculatorRequest { N1 = n1, N2 = n2 });

            Console.WriteLine($"Called service async: {n1} / {n2} = {divisionAsync.Result}");

            var divisionError = client.Divide(new CalculatorRequest {
                N1 = n1, N2 = 0
            });

            Console.WriteLine($"Called service with error: {n1} / {0} = {divisionError.Result}");
        }
Beispiel #14
0
        public static async Task GetPrimeNumbers(CalculatorService.CalculatorServiceClient client)
        {
            NumberForDecomposition numberForDecomposition = new NumberForDecomposition()
            {
                Number = 120
            };

            var primeNumberRequest = new PrimeNumberRequest()
            {
                NumberForDecomposition = numberForDecomposition
            };


            var responsePrimeNumbers = client.PrimeNumber(primeNumberRequest);

            while (await responsePrimeNumbers.ResponseStream.MoveNext())
            {
                Console.WriteLine(responsePrimeNumbers.ResponseStream.Current.Result);
                await Task.Delay(200);
            }
        }
Beispiel #15
0
        public static async Task GetAvg(CalculatorService.CalculatorServiceClient client)
        {
            List <int> providedNumberList = new List <int>()
            {
                1, 2, 3, 4
            };
            var stream = client.ComputeAvg();

            foreach (var number in providedNumberList)
            {
                await stream.RequestStream.WriteAsync(new ComputeAvgRequest()
                {
                    Number = number
                });
            }

            await stream.RequestStream.CompleteAsync();

            var resposne = await stream.ResponseAsync;

            Console.WriteLine(resposne.Avg);
        }
Beispiel #16
0
        static async Task CalcAverage(Channel channel)
        {
            WriteHeader("CalcAverage");
            var avgClient = new CalculatorService.CalculatorServiceClient(channel);
            var avgStream = avgClient.ComputeAverage();
            var rnd       = new Random();
            var avgList   = new List <int>();

            for (var i = 0; i < 10; i++)
            {
                var avgRequest = new ComputeAverageRequest {
                    Value = rnd.Next(11, 55)
                };
                avgList.Add(avgRequest.Value);
                await avgStream.RequestStream.WriteAsync(avgRequest);
            }
            await avgStream.RequestStream.CompleteAsync();

            var avgResponse = await avgStream.ResponseAsync;

            Console.WriteLine($"{avgResponse.Result} is an average for {String.Join(", ", avgList)}");
        }
Beispiel #17
0
        private static async Task FindMaximum(CalculatorService.CalculatorServiceClient calculatorClient)
        {
            var findMaxStream         = calculatorClient.FindMaximum();
            var findMaxResponseReader = Task.Run(async() =>
            {
                while (await findMaxStream.ResponseStream.MoveNext())
                {
                    Console.WriteLine(findMaxStream.ResponseStream.Current.Max);
                }
            });

            int[] inputNumbers = { 1, 5, 3, 6, 2, 20 };
            foreach (var inputNumber in inputNumbers)
            {
                await findMaxStream.RequestStream.WriteAsync(new FindMaxRequest()
                {
                    InputNumber = inputNumber
                });
            }
            await findMaxStream.RequestStream.CompleteAsync();

            await findMaxResponseReader;
        }
Beispiel #18
0
        private static async Task Test3()
        {
            Channel channel = new Channel(target, ChannelCredentials.Insecure);

            await channel.ConnectAsync();

            Console.WriteLine($"Conncted properly");

            var client = new CalculatorService.CalculatorServiceClient(channel);

            var response = await client.CalculateAsync(new CalculatorRequest
            {
                A = 12,
                B = 3
            });

            Console.WriteLine($"Here is the response : {response.Result}");

            await channel.ShutdownAsync();

            Console.WriteLine("Connection was finished");
            Console.ReadKey();
        }
Beispiel #19
0
        static async Task Main(string[] args)
        {
            Channel channel = new Channel(target, ChannelCredentials.Insecure);

            await channel.ConnectAsync().ContinueWith((task) =>
            {
                if (task.Status == TaskStatus.RanToCompletion)
                {
                    Console.WriteLine("The client connected successfully");
                }
            });

            //var client = new DummyService.DummyServiceClient(channel);
            var greetingClient = new GreetingService.GreetingServiceClient(channel);

            var greeting = new Greeting()
            {
                FirstName = "Shai",
                LastName  = "Blaustien"
            };

            var request = new GreetingRequest()
            {
                Greeting = greeting
            };

            var response = greetingClient.Greet(request);

            Console.WriteLine(response.Result);


            var calculatorService = new CalculatorService.CalculatorServiceClient(channel);
            var sumRequest        = new CalRequest {
                A = 15, B = 28
            };
            var mulRequest = new CalRequest {
                A = 5, B = 3
            };

            var sumResponse = calculatorService.Sum(sumRequest);

            Console.WriteLine(sumResponse.Result);

            var mulResponse = calculatorService.Mul(mulRequest);

            Console.WriteLine(mulResponse.Result);

            //var requestForMultiGreet = new GreetManyTimesRequest() { Greeting = greeting };
            //var manyGreetingsResponse = greetingClient.GreetManyTimes(requestForMultiGreet);
            //while(await manyGreetingsResponse.ResponseStream.MoveNext())
            //{
            //    Console.WriteLine(manyGreetingsResponse.ResponseStream.Current.Result);
            //    await Task.Delay(200);
            //}

            var requestForPrimeNumbers = new PrimeRequest()
            {
                Number = 135
            };
            var PrimeResponse = calculatorService.Prime(requestForPrimeNumbers);

            while (await PrimeResponse.ResponseStream.MoveNext())
            {
                Console.WriteLine(PrimeResponse.ResponseStream.Current.Result);
                await Task.Delay(1500);
            }
            channel.ShutdownAsync().Wait();
            Console.ReadKey();
        }
Beispiel #20
0
        //const string target = "127.0.0.1:50051";

        static async Task Main(string[] args)
        {
            var clientCert         = File.ReadAllText("ssl/client.crt");
            var clientKey          = File.ReadAllText("ssl/client.key");
            var caCrt              = File.ReadAllText("ssl/ca.crt");
            var channelCredentials = new SslCredentials(caCrt, new KeyCertificatePair(clientCert, clientKey));

            Channel channel = new Channel("localhost", 50051, channelCredentials);

            await channel.ConnectAsync().ContinueWith((task) =>
            {
                if (task.Status == TaskStatus.RanToCompletion)
                {
                    Console.WriteLine("The client connected successfuly");
                }
            });

            // Greeting
            var client     = new GreetingService.GreetingServiceClient(channel);
            var calcClient = new CalculatorService.CalculatorServiceClient(channel);

            var greeting = new Greeting()
            {
                FirstName = "Johan",
                LastName  = "Clementes"
            };

            #region "Unary"
            DoSimpleGreet(client, new GreetingRequest()
            {
                Greeting = greeting
            });
            #endregion

            #region "Server streaming"
            //DoManyGreetings(client, greeting);
            #endregion

            #region "Calculator"
            //Calc(calcClient);
            #endregion

            #region "Number to Prime Numbers"
            //await GetPrimeNumbers(calcClient);
            #endregion

            #region "Client streaming"
            //await DoLongGreet(client, greeting);
            #endregion

            #region "Client streaming Avg"
            //await GetAvg(calcClient);
            #endregion

            #region "Bi-Di streaming"
            //await DoGreetEveryone(client);
            #endregion

            #region "Find Max Number"
            //await FindMaxNumber(calcClient);
            #endregion

            #region "Errors in gRPC"
            //CalcSqrt(new SqrtService.SqrtServiceClient(channel));
            #endregion

            #region "Deadlines in gRPC"
            //GreetClientDeadlines(new GreetDeadlinesService.GreetDeadlinesServiceClient(channel));
            #endregion

            channel.ShutdownAsync().Wait();
            Console.ReadKey();
        }