Ejemplo n.º 1
0
        public async override Task <Response> ComputeAverageCalculation(IAsyncStreamReader <Request> requestStream, ServerCallContext context)
        {
            List <Request> input = await requestStream.ToListAsync();

            return(new Response {
                Result = new NumberData {
                    Number = input.Sum(x => x.Argument.Number) / input.Count
                }
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// ¥s¥ÎUtils¤¤ªºmethod¨Ó¶]loop
        /// https://github.com/grpc/grpc/blob/master/src/csharp/Grpc.Core/Utils/AsyncStreamExtensions.cs
        /// </summary>
        /// <param name="requestStream"></param>
        /// <param name="responseStream"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task GetStudentListExtention(IAsyncStreamReader <StudentRequest> requestStream, IServerStreamWriter <Student> responseStream, ServerCallContext context)
        {
            var request = await requestStream.ToListAsync();

            var students = request.Select(x => new Student
            {
                Gender       = x.Gender,
                Id           = x.Id,
                Name         = x.Name,
                ResponseTime = GetDateTimeNowString()
            });
            await responseStream.WriteAllAsync <Student>(students);
        }
Ejemplo n.º 3
0
        public override Task <HelloReply> Test(IAsyncStreamReader <HelloRequest> requestStream, ServerCallContext context)
        {
            var r = requestStream.ToListAsync().Result;

            foreach (var i in r)
            {
                Console.WriteLine($"request msg :: {i.Name}");
            }

            return(Task.FromResult(new HelloReply {
                Message = $"Hello {requestStream.Current.Name}"
            }));
        }
Ejemplo n.º 4
0
        public override async Task ExecuteFunction(IAsyncStreamReader <BundledRows> requestStream, IServerStreamWriter <BundledRows> responseStream, ServerCallContext context)
        {
            if (Logger.IsTraceEnabled)
            {
                Logger.Trace("-- ExecuteFunction --");

                TraceServerCallContext(context);
            }
            else
            {
                Logger.Debug("ExecuteFunction called");
            }

            var functionRequestHeaderStream = context.RequestHeaders.SingleOrDefault(header => header.Key == "qlik-functionrequestheader-bin");

            if (functionRequestHeaderStream == null)
            {
                throw new Exception("ExecuteFunction called without Function Request Header in Request Headers.");
            }

            var functionRequestHeader = new FunctionRequestHeader();

            functionRequestHeader.MergeFrom(new CodedInputStream(functionRequestHeaderStream.ValueBytes));

            Logger.Trace($"FunctionRequestHeader.FunctionId String : {(FunctionConstant)functionRequestHeader.FunctionId}");

            switch (functionRequestHeader.FunctionId)
            {
            case (int)FunctionConstant.Add42:
            {
                while (await requestStream.MoveNext())
                {
                    var resultBundle = new BundledRows();
                    foreach (var row in requestStream.Current.Rows)
                    {
                        var resultRow = new Row();
                        resultRow.Duals.Add(new Dual {
                                NumData = row.Duals[0].NumData + 42.0
                            });
                        resultBundle.Rows.Add(resultRow);
                    }
                    await responseStream.WriteAsync(resultBundle);
                }

                break;
            }

            case (int)FunctionConstant.SumOfAllNumbers:
            {
                double sum = 0.0;

                while (await requestStream.MoveNext())
                {
                    foreach (var row in requestStream.Current.Rows)
                    {
                        sum = sum + row.Duals.Select(d => d.NumData).Sum();
                    }
                }

                var resultBundle = new BundledRows();
                var resultRow    = new Row();
                resultRow.Duals.Add(new Dual {
                        NumData = sum
                    });
                resultBundle.Rows.Add(resultRow);
                await responseStream.WriteAsync(resultBundle);

                break;
            }

            case (int)FunctionConstant.Concatenate:
            {
                var requestAsList = await requestStream.ToListAsync();

                var concatenatedStrings = String.Join(", ",
                                                      requestAsList.SelectMany(bundledRows => bundledRows.Rows).SelectMany(row => row.Duals)
                                                      .Select(dual => dual.StrData));

                var resultBundle = new BundledRows();
                var resultRow    = new Row();
                resultRow.Duals.Add(new Dual {
                        StrData = concatenatedStrings
                    });
                resultBundle.Rows.Add(resultRow);
                await responseStream.WriteAsync(resultBundle);

                break;
            }

            case (int)FunctionConstant.CallCounter:
            {
                var currentIncrement = Interlocked.Increment(ref _callCounter);

                while (await requestStream.MoveNext())
                {
                    var resultBundle = new BundledRows();
                    foreach (var row in requestStream.Current.Rows)
                    {
                        var resultRow = new Row();
                        resultRow.Duals.Add(new Dual {
                                NumData = currentIncrement
                            });
                        resultBundle.Rows.Add(resultRow);
                    }
                    await responseStream.WriteAsync(resultBundle);
                }
                break;
            }

            case (int)FunctionConstant.CallCounterNoCache:
            {
                context.ResponseTrailers.Add("qlik-cache", "no-store");
                var currentIncrement = Interlocked.Increment(ref _callCounter);

                while (await requestStream.MoveNext())
                {
                    var resultBundle = new BundledRows();
                    foreach (var row in requestStream.Current.Rows)
                    {
                        var resultRow = new Row();
                        resultRow.Duals.Add(new Dual {
                                NumData = currentIncrement
                            });
                        resultBundle.Rows.Add(resultRow);
                    }
                    await responseStream.WriteAsync(resultBundle);
                }
                break;
            }

            default:
                break;
            }

            Logger.Trace("-- (ExecuteFunction) --");
        }