public async Task <Response> SendRequestAsync(Request request, string clientDisplayName = "") { try { //LogRequest(request); Metadata metadata = null; if (string.IsNullOrWhiteSpace(clientDisplayName)) { metadata = new Metadata { { "client_tag", clientDisplayName } }; } InnerRequest innerRequest = request.Convert(); // Send request and wait for response InnerResponse innerResponse = await GetWarpGrpcClient().SendRequestAsync(innerRequest, metadata); // convert InnerResponse to Response and return response to end user return(new Response(innerResponse)); } catch (RpcException ex) { logger.LogError($"Grpc Exception in SendRequestAsync. Status: {ex.Status}"); throw new RpcException(ex.Status); } catch (Exception ex) { logger.LogError(ex, "Exception in Initiator.SendRequestAsync"); return(null); } }
/// <summary> /// Publish a single request using the KubeMQ , response will return in the passed handler. /// </summary> /// <param name="handler">Method that will be activated once receiving response.</param> /// <param name="request">The KubeMQ.SDK.csharp.RequestReply.LowLevel.request that will be sent to the kubeMQ.</param> /// <returns>A task that represents the request that was sent using the SendRequest.</returns> public async Task SendRequest(Request request, HandleResponseDelegate handler) { try { //LogRequest(request); InnerRequest innerRequest = request.Convert(); // Send request and wait for response InnerResponse innerResponse = await GetKubeMQClient().SendRequestAsync(innerRequest, _metadata); // convert InnerResponse to Response and return response to end user Response response = new Response(innerResponse); // send the response to the end-user response handler handler(response); } catch (RpcException ex) { logger.LogError($"Grpc Exception in SendRequest. Status: {ex.Status}"); throw new RpcException(ex.Status); } catch (Exception ex) { logger.LogError(ex, "Exception in Initiator.SendRequest"); throw ex; } }
/// <summary> /// Publish a single request using the KubeMQ. /// </summary> /// <param name="request"></param> /// <returns>KubeMQ KubeMQ.SDK.csharp.RequestReply.Response.</returns> public Response SendRequest(Request request) { try { //LogRequest(request); InnerRequest innerRequest = request.Convert(); // Send request and wait for response InnerResponse innerResponse = GetKubeMQClient().SendRequest(innerRequest, _metadata); // convert InnerResponse to Response and return response to end user return(new Response(innerResponse)); } catch (RpcException ex) { logger.LogError($"Grpc Exception in SendRequestAsync. Status: {ex.Status}"); throw new RpcException(ex.Status); } catch (Exception ex) { logger.LogError(ex, "Exception in Initiator.SendRequestAsync"); return(null); } }
private async void CommunicationWithEndUser(RespondDelegate handler) { while (true) { // send requests to end-user and post his response to queue try { // await for Request from queue InnerRequest innerRequest = await _RecivedRequests.ReceiveAsync(); // Convert KubeMQ.Grpc.Request to outter Request Request request = new Request(innerRequest); // Activate end-user request handler and receive the response Response response = handler(request); // Convert InnerResponse innerResponse = response.Convert(); // Send response - Add (Post) response to queue _ResponsesToSend.Post(innerResponse); } catch (Exception ex) { //throw ex; } } }
internal Response(InnerResponse inner) { RequestID = inner.RequestID; ReplyChannel = inner.ReplyChannel; Metadata = inner.Metadata; Body = inner.Body.ToByteArray(); CacheHit = inner.CacheHit; }
internal Response(InnerResponse inner) { ClientID = inner.ClientID ?? string.Empty; RequestID = inner.RequestID; ReplyChannel = inner.ReplyChannel; Metadata = inner.Metadata ?? string.Empty; Body = inner.Body.ToByteArray(); CacheHit = inner.CacheHit; Timestamp = Tools.Converter.FromUnixTime(inner.Timestamp); Executed = inner.Executed; Error = inner.Error; }
private async Task GrpcListenAndRespondAsync(string channel, string group, string clientDisplayName) { // metadata to pass over GRPC var metadata = new Metadata { { "client_tag", clientDisplayName }, { "channel", channel }, { "group", group } }; // Open bidirectional streaming over GRPC using (var call = GetWarpGrpcClient().RequestResponseStream(metadata)) { // write response from queue to GRPC stream var responseWriterTask = Task.Run(async() => { while (true) { // await for response in queue InnerResponse response = await _ResponsesToSend.ReceiveAsync(); LogResponse(response); // write response to GRPC stream await call.RequestStream.WriteAsync(response); } }); // await for requests form GRPC stream. while (await call.ResponseStream.MoveNext()) { // Recived requests form GRPC stream. InnerRequest request = call.ResponseStream.Current; if (request.Body.IsEmpty) { // ignore 'keep alive' (empty) requests continue; } LogRequest(request); // Add (Post) request to queue _RecivedRequests.Post(request); } } }
private void LogResponse(InnerResponse response) { logger.LogTrace($"Responder InnerResponse. ID:'{response.RequestID}', ReplyChannel:'{response.ReplyChannel}'"); }