| | | 1 | | using MessagePack; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | |
| | | 4 | | namespace NLightning.Daemon.Ipc.Handlers; |
| | | 5 | | |
| | | 6 | | using Domain.Client.Constants; |
| | | 7 | | using Domain.Client.Enums; |
| | | 8 | | using Domain.Exceptions; |
| | | 9 | | using Domain.Node.Interfaces; |
| | | 10 | | using Interfaces; |
| | | 11 | | using Services.Ipc.Factories; |
| | | 12 | | using Transport.Ipc; |
| | | 13 | | using Transport.Ipc.Requests; |
| | | 14 | | using Transport.Ipc.Responses; |
| | | 15 | | |
| | | 16 | | internal sealed class ConnectPeerIpcHandler : IIpcCommandHandler |
| | | 17 | | { |
| | | 18 | | private readonly ILogger<ConnectPeerIpcHandler> _logger; |
| | | 19 | | private readonly IPeerManager _peerManager; |
| | | 20 | | |
| | 0 | 21 | | public ClientCommand Command => ClientCommand.ConnectPeer; |
| | | 22 | | |
| | 0 | 23 | | public ConnectPeerIpcHandler(ILogger<ConnectPeerIpcHandler> logger, IPeerManager peerManager) |
| | | 24 | | { |
| | 0 | 25 | | _peerManager = peerManager; |
| | 0 | 26 | | _logger = logger; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task<IpcEnvelope> HandleAsync(IpcEnvelope envelope, CancellationToken ct) |
| | | 30 | | { |
| | | 31 | | try |
| | | 32 | | { |
| | | 33 | | // Deserialize the request |
| | 0 | 34 | | var request = |
| | 0 | 35 | | MessagePackSerializer.Deserialize<ConnectPeerIpcRequest>(envelope.Payload, cancellationToken: ct); |
| | | 36 | | |
| | | 37 | | // Validate the address |
| | 0 | 38 | | if (string.IsNullOrWhiteSpace(request.Address.Address)) |
| | 0 | 39 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.InvalidAddress, |
| | 0 | 40 | | "Invalid address: address cannot be empty"); |
| | | 41 | | |
| | | 42 | | // Parse and connect to the peer |
| | 0 | 43 | | var peer = await _peerManager.ConnectToPeerAsync(request.Address); |
| | | 44 | | |
| | 0 | 45 | | _logger.LogInformation("Successfully connected to peer at {Address}", request.Address); |
| | | 46 | | |
| | | 47 | | // Create a success response |
| | 0 | 48 | | var response = new ConnectPeerIpcResponse |
| | 0 | 49 | | { |
| | 0 | 50 | | Id = peer.NodeId, |
| | 0 | 51 | | Features = peer.Features, |
| | 0 | 52 | | IsInitiator = true, |
| | 0 | 53 | | Address = peer.Host, |
| | 0 | 54 | | Type = peer.Type, |
| | 0 | 55 | | Port = peer.Port |
| | 0 | 56 | | }; |
| | | 57 | | |
| | 0 | 58 | | var payload = MessagePackSerializer.Serialize(response, 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 (FormatException fe) |
| | | 69 | | { |
| | 0 | 70 | | _logger.LogWarning(fe, "Invalid peer address format"); |
| | 0 | 71 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.InvalidAddress, |
| | 0 | 72 | | $"Invalid address format: {fe.Message}"); |
| | | 73 | | } |
| | 0 | 74 | | catch (InvalidOperationException oe) |
| | | 75 | | { |
| | 0 | 76 | | _logger.LogInformation(oe, "The operation could not be completed"); |
| | 0 | 77 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.InvalidOperation, |
| | 0 | 78 | | $"The operation could not be completed: {oe.Message}"); |
| | | 79 | | } |
| | 0 | 80 | | catch (ConnectionException ce) |
| | | 81 | | { |
| | 0 | 82 | | _logger.LogError(ce, "Failed to connect to peer"); |
| | 0 | 83 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.ConnectionError, |
| | 0 | 84 | | $"Connection failed: {ce.Message}"); |
| | | 85 | | } |
| | 0 | 86 | | catch (Exception e) |
| | | 87 | | { |
| | 0 | 88 | | _logger.LogError(e, "Error connecting to peer"); |
| | 0 | 89 | | return IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.ServerError, |
| | 0 | 90 | | $"Error connecting to peer: {e.Message}"); |
| | | 91 | | } |
| | 0 | 92 | | } |
| | | 93 | | } |