| | 1 | | using System.Net.Sockets; |
| | 2 | | using Microsoft.Extensions.Logging; |
| | 3 | | using Microsoft.Extensions.Options; |
| | 4 | | using NBitcoin; |
| | 5 | |
|
| | 6 | | namespace NLightning.Infrastructure.Node.Managers; |
| | 7 | |
|
| | 8 | | using Domain.Exceptions; |
| | 9 | | using Domain.Node.Options; |
| | 10 | | using Domain.ValueObjects; |
| | 11 | | using Infrastructure.Protocol.Models; |
| | 12 | | using Interfaces; |
| | 13 | | using Models; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Service for managing peers. |
| | 17 | | /// </summary> |
| | 18 | | /// <remarks> |
| | 19 | | /// This class is used to manage peers in the network. |
| | 20 | | /// </remarks> |
| | 21 | | /// <seealso cref="IPeerManager" /> |
| | 22 | | public sealed class PeerManager : IPeerManager |
| | 23 | | { |
| 12 | 24 | | private readonly Dictionary<ChannelId, PubKey> _channels = []; |
| | 25 | | private readonly ILogger<PeerManager> _logger; |
| | 26 | | private readonly IOptions<NodeOptions> _nodeOptions; |
| | 27 | | private readonly IPeerFactory _peerFactory; |
| 12 | 28 | | private readonly Dictionary<PubKey, Peer> _peers = []; |
| | 29 | |
|
| 12 | 30 | | public PeerManager(ILogger<PeerManager> logger, IOptions<NodeOptions> nodeOptions, IPeerFactory peerFactory) |
| | 31 | | { |
| 12 | 32 | | _logger = logger; |
| 12 | 33 | | _nodeOptions = nodeOptions; |
| 12 | 34 | | _peerFactory = peerFactory; |
| 12 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <inheritdoc /> |
| | 38 | | /// <exception cref="ConnectionException">Thrown when the connection to the peer fails.</exception> |
| | 39 | | public async Task ConnectToPeerAsync(PeerAddress peerAddress) |
| | 40 | | { |
| | 41 | | // Connect to the peer |
| 8 | 42 | | var tcpClient = new TcpClient(); |
| | 43 | | try |
| | 44 | | { |
| 8 | 45 | | await tcpClient.ConnectAsync(peerAddress.Host, peerAddress.Port, |
| 8 | 46 | | new CancellationTokenSource(_nodeOptions.Value.NetworkTimeout).Token); |
| 4 | 47 | | } |
| 0 | 48 | | catch (OperationCanceledException) |
| | 49 | | { |
| 0 | 50 | | throw new ConnectionException($"Timeout connecting to peer {peerAddress.Host}:{peerAddress.Port}"); |
| | 51 | | } |
| 4 | 52 | | catch (Exception e) |
| | 53 | | { |
| 4 | 54 | | throw new ConnectionException($"Failed to connect to peer {peerAddress.Host}:{peerAddress.Port}", e); |
| | 55 | | } |
| | 56 | |
|
| 4 | 57 | | var peer = await _peerFactory.CreateConnectedPeerAsync(peerAddress, tcpClient); |
| 4 | 58 | | peer.DisconnectEvent += (_, _) => |
| 4 | 59 | | { |
| 0 | 60 | | _peers.Remove(peerAddress.PubKey); |
| 0 | 61 | | _logger.LogInformation("Peer {Peer} disconnected", peerAddress.PubKey); |
| 4 | 62 | | }; |
| | 63 | |
|
| 4 | 64 | | _peers.Add(peerAddress.PubKey, peer); |
| 4 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <inheritdoc /> |
| | 68 | | /// <exception cref="ErrorException">Thrown when the connection to the peer fails.</exception> |
| | 69 | | public async Task AcceptPeerAsync(TcpClient tcpClient) |
| | 70 | | { |
| | 71 | | // Create the peer |
| 4 | 72 | | var peer = await _peerFactory.CreateConnectingPeerAsync(tcpClient); |
| 4 | 73 | | peer.DisconnectEvent += (_, _) => |
| 4 | 74 | | { |
| 0 | 75 | | _peers.Remove(peer.PeerAddress.PubKey); |
| 0 | 76 | | _logger.LogError("{Peer} disconnected", peer.PeerAddress.PubKey); |
| 4 | 77 | | }; |
| | 78 | |
|
| 4 | 79 | | _peers.Add(peer.PeerAddress.PubKey, peer); |
| 4 | 80 | | } |
| | 81 | |
|
| | 82 | | /// <inheritdoc /> |
| | 83 | | public void DisconnectPeer(PubKey pubKey) |
| | 84 | | { |
| 0 | 85 | | if (_peers.TryGetValue(pubKey, out var peer)) |
| | 86 | | { |
| 0 | 87 | | peer.Disconnect(); |
| | 88 | | } |
| | 89 | | else |
| | 90 | | { |
| 0 | 91 | | _logger.LogWarning("Peer {Peer} not found", pubKey); |
| | 92 | | } |
| 0 | 93 | | } |
| | 94 | | } |