| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Domain.Node.Models; |
| | | 4 | | |
| | | 5 | | using Channels.Models; |
| | | 6 | | using Crypto.ValueObjects; |
| | | 7 | | using Interfaces; |
| | | 8 | | using Options; |
| | | 9 | | using ValueObjects; |
| | | 10 | | |
| | | 11 | | public class PeerModel |
| | | 12 | | { |
| | | 13 | | private PeerAddressInfo? _peerAddressInfo; |
| | | 14 | | |
| | | 15 | | private IPeerService? _peerService; |
| | 0 | 16 | | |
| | 0 | 17 | | public CompactPubKey NodeId { get; } |
| | 0 | 18 | | public string Host { get; } |
| | 0 | 19 | | public uint Port { get; } |
| | 0 | 20 | | public string Type { get; } |
| | 0 | 21 | | public DateTime LastSeenAt { get; set; } |
| | | 22 | | |
| | | 23 | | public FeatureSet Features |
| | | 24 | | { |
| | 0 | 25 | | get |
| | | 26 | | { |
| | 0 | 27 | | return _peerService is null |
| | 0 | 28 | | ? throw new NullReferenceException($"{nameof(PeerModel)}.{nameof(Features)} was null") |
| | 0 | 29 | | : _peerService.Features.GetNodeFeatures(); |
| | | 30 | | } |
| | 0 | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | public FeatureOptions NegotiatedFeatures |
| | | 34 | | { |
| | 0 | 35 | | get |
| | 0 | 36 | | { |
| | 0 | 37 | | return _peerService is null |
| | 0 | 38 | | ? throw new NullReferenceException($"{nameof(PeerModel)}.{nameof(Features)} was null") |
| | 0 | 39 | | : _peerService.Features; |
| | | 40 | | } |
| | | 41 | | } |
| | 0 | 42 | | |
| | | 43 | | public PeerAddressInfo PeerAddressInfo |
| | 0 | 44 | | { |
| | 0 | 45 | | get |
| | | 46 | | { |
| | 0 | 47 | | _peerAddressInfo ??= new PeerAddressInfo($"{NodeId}@{Host}:{Port}"); |
| | 0 | 48 | | |
| | 0 | 49 | | return _peerAddressInfo.Value; |
| | | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | public ICollection<ChannelModel>? Channels { get; set; } |
| | 0 | 54 | | |
| | 0 | 55 | | public PeerModel(CompactPubKey nodeId, string host, uint port, string type) |
| | 0 | 56 | | { |
| | 0 | 57 | | NodeId = nodeId; |
| | 0 | 58 | | Host = host; |
| | 0 | 59 | | Port = port; |
| | 0 | 60 | | Type = type; |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | public bool TryGetPeerService([MaybeNullWhen(false)] out IPeerService peerService) |
| | | 64 | | { |
| | 0 | 65 | | if (_peerService is not null) |
| | | 66 | | { |
| | 0 | 67 | | peerService = _peerService; |
| | 0 | 68 | | return true; |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | peerService = null; |
| | 0 | 72 | | return false; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public void SetPeerService(IPeerService peerService) |
| | | 76 | | { |
| | 0 | 77 | | ArgumentNullException.ThrowIfNull(peerService); |
| | | 78 | | |
| | 0 | 79 | | if (_peerService is not null) |
| | 0 | 80 | | throw new InvalidOperationException("Peer service already set."); |
| | | 81 | | |
| | 0 | 82 | | _peerService = peerService; |
| | 0 | 83 | | } |
| | | 84 | | } |