| | | 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.Node.Interfaces; |
| | | 9 | | using Interfaces; |
| | | 10 | | using Services.Ipc.Factories; |
| | | 11 | | using Transport.Ipc; |
| | | 12 | | using Transport.Ipc.Responses; |
| | | 13 | | |
| | | 14 | | internal class ListPeersIpcHandler : IIpcCommandHandler |
| | | 15 | | { |
| | | 16 | | private readonly IPeerManager _peerManager; |
| | | 17 | | private readonly ILogger<ListPeersIpcHandler> _logger; |
| | | 18 | | |
| | 0 | 19 | | public ClientCommand Command => ClientCommand.ListPeers; |
| | | 20 | | |
| | 0 | 21 | | public ListPeersIpcHandler(IPeerManager peerManager, ILogger<ListPeersIpcHandler> logger) |
| | | 22 | | { |
| | 0 | 23 | | _peerManager = peerManager; |
| | 0 | 24 | | _logger = logger; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public Task<IpcEnvelope> HandleAsync(IpcEnvelope envelope, CancellationToken ct) |
| | | 28 | | { |
| | | 29 | | try |
| | | 30 | | { |
| | 0 | 31 | | var resp = _peerManager.ListPeers(); |
| | 0 | 32 | | var ipcResp = new ListPeersIpcResponse(); |
| | | 33 | | |
| | 0 | 34 | | if (resp.Count > 0) |
| | | 35 | | { |
| | 0 | 36 | | ipcResp.Peers = new List<PeerInfoIpcResponse>(resp.Count); |
| | 0 | 37 | | foreach (var peer in resp) |
| | | 38 | | { |
| | 0 | 39 | | ipcResp.Peers.Add(new PeerInfoIpcResponse |
| | 0 | 40 | | { |
| | 0 | 41 | | Address = $"{peer.Host}:{peer.Port}", |
| | 0 | 42 | | Connected = true, |
| | 0 | 43 | | Features = peer.Features, |
| | 0 | 44 | | Id = peer.NodeId, |
| | 0 | 45 | | ChannelQty = (uint)(peer.Channels?.Count ?? 0) |
| | 0 | 46 | | }); |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | var payload = MessagePackSerializer.Serialize(ipcResp, cancellationToken: ct); |
| | 0 | 51 | | var responseEnvelope = new IpcEnvelope |
| | 0 | 52 | | { |
| | 0 | 53 | | Version = envelope.Version, |
| | 0 | 54 | | Command = envelope.Command, |
| | 0 | 55 | | CorrelationId = envelope.CorrelationId, |
| | 0 | 56 | | Kind = IpcEnvelopeKind.Response, |
| | 0 | 57 | | Payload = payload |
| | 0 | 58 | | }; |
| | 0 | 59 | | return Task.FromResult(responseEnvelope); |
| | | 60 | | } |
| | 0 | 61 | | catch (Exception e) |
| | | 62 | | { |
| | 0 | 63 | | _logger.LogError(e, "Error listing peers"); |
| | 0 | 64 | | return Task.FromResult(IpcErrorFactory.CreateErrorEnvelope(envelope, ErrorCodes.ServerError, e.Message)); |
| | | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | } |