| | | 1 | | using MessagePack; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | namespace NLightning.Daemon.Ipc.Handlers; |
| | | 6 | | |
| | | 7 | | using Daemon.Handlers; |
| | | 8 | | using Daemon.Interfaces; |
| | | 9 | | using Domain.Client.Constants; |
| | | 10 | | using Domain.Client.Enums; |
| | | 11 | | using Domain.Client.Exceptions; |
| | | 12 | | using Domain.Client.Requests; |
| | | 13 | | using Domain.Client.Responses; |
| | | 14 | | using Domain.Exceptions; |
| | | 15 | | using Interfaces; |
| | | 16 | | using Services.Ipc.Factories; |
| | | 17 | | using Transport.Ipc; |
| | | 18 | | using Transport.Ipc.Requests; |
| | | 19 | | using Transport.Ipc.Responses; |
| | | 20 | | |
| | | 21 | | public class OpenChannelSubscriptionIpcHandler : IIpcCommandHandler |
| | | 22 | | { |
| | | 23 | | private readonly ILogger<OpenChannelSubscriptionIpcHandler> _logger; |
| | | 24 | | private readonly IServiceProvider _serviceProvider; |
| | | 25 | | |
| | 0 | 26 | | public ClientCommand Command => ClientCommand.OpenChannelSubscription; |
| | | 27 | | |
| | 0 | 28 | | public OpenChannelSubscriptionIpcHandler(ILogger<OpenChannelSubscriptionIpcHandler> logger, |
| | 0 | 29 | | IServiceProvider serviceProvider) |
| | | 30 | | { |
| | 0 | 31 | | _logger = logger; |
| | 0 | 32 | | _serviceProvider = serviceProvider; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public async Task<IpcEnvelope> HandleAsync(IpcEnvelope envelope, CancellationToken ct) |
| | | 36 | | { |
| | | 37 | | try |
| | | 38 | | { |
| | | 39 | | // Deserialize the request |
| | 0 | 40 | | var request = |
| | 0 | 41 | | MessagePackSerializer.Deserialize<OpenChannelSubscriptionIpcRequest>( |
| | 0 | 42 | | envelope.Payload, cancellationToken: ct); |
| | | 43 | | |
| | | 44 | | // Get the client handler |
| | 0 | 45 | | using var scope = _serviceProvider.CreateScope(); |
| | 0 | 46 | | var openChannelClientSubscriptionHandler = |
| | 0 | 47 | | scope.ServiceProvider.GetService( |
| | 0 | 48 | | typeof(IClientCommandHandler<OpenChannelClientSubscriptionRequest, |
| | 0 | 49 | | OpenChannelClientSubscriptionResponse>)) as |
| | 0 | 50 | | OpenChannelClientSubscriptionHandler ?? |
| | 0 | 51 | | throw new InvalidOperationException( |
| | 0 | 52 | | $"Unable to get service {nameof(OpenChannelClientSubscriptionHandler)}"); |
| | | 53 | | |
| | 0 | 54 | | var clientResponse = await openChannelClientSubscriptionHandler.HandleAsync(request.ToClientRequest(), ct); |
| | | 55 | | |
| | 0 | 56 | | var payload = MessagePackSerializer.Serialize( |
| | 0 | 57 | | OpenChannelSubscriptionIpcResponse.FromClientResponse(clientResponse), |
| | 0 | 58 | | cancellationToken: ct); |
| | 0 | 59 | | return new IpcEnvelope |
| | 0 | 60 | | { |
| | 0 | 61 | | Version = envelope.Version, |
| | 0 | 62 | | Command = envelope.Command, |
| | 0 | 63 | | CorrelationId = envelope.CorrelationId, |
| | 0 | 64 | | Kind = IpcEnvelopeKind.Response, |
| | 0 | 65 | | Payload = payload |
| | 0 | 66 | | }; |
| | | 67 | | } |
| | 0 | 68 | | catch (ClientException ce) |
| | | 69 | | { |
| | 0 | 70 | | _logger.LogError(ce, "Error while handling OpenChannelSubscription"); |
| | 0 | 71 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ce.Message, ce.Message); |
| | | 72 | | } |
| | 0 | 73 | | catch (InvalidOperationException oe) |
| | | 74 | | { |
| | 0 | 75 | | _logger.LogError(oe, "The operation could not be completed"); |
| | 0 | 76 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.InvalidOperation, |
| | 0 | 77 | | $"The operation could not be completed: {oe.Message}"); |
| | | 78 | | } |
| | 0 | 79 | | catch (ConnectionException ce) |
| | | 80 | | { |
| | 0 | 81 | | _logger.LogError(ce, "Failed to connect to peer"); |
| | 0 | 82 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.ConnectionError, |
| | 0 | 83 | | $"Connection failed: {ce.Message}"); |
| | | 84 | | } |
| | 0 | 85 | | catch (ChannelErrorException cee) |
| | | 86 | | { |
| | 0 | 87 | | _logger.LogError(cee, "Error opening Channel"); |
| | 0 | 88 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.ConnectionError, |
| | 0 | 89 | | $"Channel Error: {cee.Message}"); |
| | | 90 | | } |
| | 0 | 91 | | catch (Exception e) |
| | | 92 | | { |
| | 0 | 93 | | _logger.LogError(e, "Error opening channel"); |
| | 0 | 94 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.ServerError, |
| | 0 | 95 | | $"Error opening channel: {e.Message}"); |
| | | 96 | | } |
| | 0 | 97 | | } |
| | | 98 | | } |