| | 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 ValueObjects; |
| | 9 | |
|
| | 10 | | public class PeerModel |
| | 11 | | { |
| | 12 | | private PeerAddressInfo? _peerAddressInfo; |
| | 13 | |
|
| | 14 | | private IPeerService? _peerService; |
| | 15 | |
|
| 0 | 16 | | public CompactPubKey NodeId { get; } |
| 0 | 17 | | public string Host { get; } |
| 0 | 18 | | public uint Port { get; } |
| 60 | 19 | | public DateTime LastSeenAt { get; set; } |
| | 20 | |
|
| | 21 | | public PeerAddressInfo PeerAddressInfo |
| | 22 | | { |
| | 23 | | get |
| | 24 | | { |
| 0 | 25 | | _peerAddressInfo ??= new PeerAddressInfo($"{NodeId}@{Host}:{Port}"); |
| | 26 | |
|
| 0 | 27 | | return _peerAddressInfo.Value; |
| | 28 | | } |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | public ICollection<ChannelModel>? Channels { get; set; } |
| | 32 | |
|
| 60 | 33 | | public PeerModel(CompactPubKey nodeId, string host, uint port) |
| | 34 | | { |
| 60 | 35 | | NodeId = nodeId; |
| 60 | 36 | | Host = host; |
| 60 | 37 | | Port = port; |
| 60 | 38 | | } |
| | 39 | |
|
| | 40 | | public bool TryGetPeerService([MaybeNullWhen(false)] out IPeerService peerService) |
| | 41 | | { |
| 44 | 42 | | if (_peerService is not null) |
| | 43 | | { |
| 44 | 44 | | peerService = _peerService; |
| 44 | 45 | | return true; |
| | 46 | | } |
| | 47 | |
|
| 0 | 48 | | peerService = null; |
| 0 | 49 | | return false; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public void SetPeerService(IPeerService peerService) |
| | 53 | | { |
| 60 | 54 | | ArgumentNullException.ThrowIfNull(peerService); |
| | 55 | |
|
| 60 | 56 | | if (_peerService is not null) |
| 0 | 57 | | throw new InvalidOperationException("Peer service already set."); |
| | 58 | |
|
| 60 | 59 | | _peerService = peerService; |
| 60 | 60 | | } |
| | 61 | | } |