| | 1 | | using System.Net; |
| | 2 | | using System.Net.Sockets; |
| | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | 4 | | using Microsoft.Extensions.Logging; |
| | 5 | | using Microsoft.Extensions.Options; |
| | 6 | |
|
| | 7 | | namespace NLightning.Infrastructure.Node.Factories; |
| | 8 | |
|
| | 9 | | using Domain.Crypto.ValueObjects; |
| | 10 | | using Domain.Exceptions; |
| | 11 | | using Domain.Node.Interfaces; |
| | 12 | | using Domain.Node.Options; |
| | 13 | | using Domain.Protocol.Interfaces; |
| | 14 | | using Services; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Factory for creating peer services. |
| | 18 | | /// </summary> |
| | 19 | | public class PeerServiceFactory : IPeerServiceFactory |
| | 20 | | { |
| | 21 | | private readonly ILoggerFactory _loggerFactory; |
| | 22 | | private readonly IMessageFactory _messageFactory; |
| | 23 | | private readonly IMessageServiceFactory _messageServiceFactory; |
| | 24 | | private readonly ISecureKeyManager _secureKeyManager; |
| | 25 | | private readonly ITransportServiceFactory _transportServiceFactory; |
| | 26 | | private readonly IServiceProvider _serviceProvider; |
| | 27 | | private readonly NodeOptions _nodeOptions; |
| | 28 | |
|
| 0 | 29 | | public PeerServiceFactory(ILoggerFactory loggerFactory, IMessageFactory messageFactory, |
| 0 | 30 | | IMessageServiceFactory messageServiceFactory, ISecureKeyManager secureKeyManager, |
| 0 | 31 | | ITransportServiceFactory transportServiceFactory, IOptions<NodeOptions> nodeOptions, |
| 0 | 32 | | IServiceProvider serviceProvider) |
| | 33 | | { |
| 0 | 34 | | _loggerFactory = loggerFactory; |
| 0 | 35 | | _messageFactory = messageFactory; |
| 0 | 36 | | _messageServiceFactory = messageServiceFactory; |
| 0 | 37 | | _secureKeyManager = secureKeyManager; |
| 0 | 38 | | _transportServiceFactory = transportServiceFactory; |
| 0 | 39 | | _serviceProvider = serviceProvider; |
| 0 | 40 | | _nodeOptions = nodeOptions.Value; |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <inheritdoc /> |
| | 44 | | /// <exception cref="ConnectionException">Thrown when the connection to the peer fails.</exception> |
| | 45 | | public async Task<IPeerService> CreateConnectedPeerAsync(CompactPubKey peerPubKey, TcpClient tcpClient) |
| | 46 | | { |
| | 47 | | // Create a specific logger for the communication service |
| 0 | 48 | | var commLogger = _loggerFactory.CreateLogger<PeerCommunicationService>(); |
| 0 | 49 | | var appLogger = _loggerFactory.CreateLogger<PeerService>(); |
| | 50 | |
|
| | 51 | | // Create and Initialize the transport service |
| 0 | 52 | | var key = _secureKeyManager.GetNodeKeyPair(); |
| 0 | 53 | | var transportService = |
| 0 | 54 | | _transportServiceFactory.CreateTransportService(true, key.PrivKey, peerPubKey, tcpClient); |
| | 55 | |
|
| | 56 | | try |
| | 57 | | { |
| 0 | 58 | | await transportService.InitializeAsync(); |
| 0 | 59 | | } |
| 0 | 60 | | catch (Exception ex) |
| | 61 | | { |
| 0 | 62 | | throw new ConnectionException($"Error connecting to peer {peerPubKey}", ex); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | // Create the message service |
| 0 | 66 | | var messageService = _messageServiceFactory.CreateMessageService(transportService); |
| | 67 | |
|
| | 68 | | // Create the ping pong service |
| 0 | 69 | | var pingPongService = _serviceProvider.GetRequiredService<IPingPongService>(); |
| | 70 | |
|
| | 71 | | // Create the communication service |
| 0 | 72 | | var communicationService = |
| 0 | 73 | | new PeerCommunicationService(commLogger, messageService, _messageFactory, peerPubKey, pingPongService, |
| 0 | 74 | | _serviceProvider); |
| | 75 | |
|
| | 76 | | // Create the service |
| 0 | 77 | | return new PeerService(communicationService, _nodeOptions.Features, appLogger, _nodeOptions.NetworkTimeout); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | /// <inheritdoc /> |
| | 81 | | /// <exception cref="ConnectionException">Thrown when the connection to the peer fails.</exception> |
| | 82 | | public async Task<IPeerService> CreateConnectingPeerAsync(TcpClient tcpClient) |
| | 83 | | { |
| | 84 | | // Create loggers |
| 0 | 85 | | var commLogger = _loggerFactory.CreateLogger<PeerCommunicationService>(); |
| 0 | 86 | | var appLogger = _loggerFactory.CreateLogger<PeerService>(); |
| | 87 | |
|
| 0 | 88 | | var remoteEndPoint = |
| 0 | 89 | | (IPEndPoint)(tcpClient.Client.RemoteEndPoint ?? throw new Exception("Failed to get remote endpoint")); |
| 0 | 90 | | var ipAddress = remoteEndPoint.Address.ToString(); |
| 0 | 91 | | var port = remoteEndPoint.Port; |
| | 92 | |
|
| | 93 | | // Create and Initialize the transport service |
| 0 | 94 | | var key = _secureKeyManager.GetNodeKeyPair(); |
| 0 | 95 | | var transportService = |
| 0 | 96 | | _transportServiceFactory.CreateTransportService(false, key.PrivKey, key.CompactPubKey, tcpClient); |
| | 97 | | try |
| | 98 | | { |
| 0 | 99 | | await transportService.InitializeAsync(); |
| 0 | 100 | | } |
| 0 | 101 | | catch (Exception ex) |
| | 102 | | { |
| 0 | 103 | | throw new ConnectionException($"Error establishing connection to peer {ipAddress}:{port}", ex); |
| | 104 | | } |
| | 105 | |
|
| 0 | 106 | | if (transportService.RemoteStaticPublicKey is null) |
| | 107 | | { |
| 0 | 108 | | throw new ErrorException("Failed to get remote static public key"); |
| | 109 | | } |
| | 110 | |
|
| | 111 | | // Create the message service |
| 0 | 112 | | var messageService = _messageServiceFactory.CreateMessageService(transportService); |
| | 113 | |
|
| | 114 | | // Create the ping pong service |
| 0 | 115 | | var pingPongService = _serviceProvider.GetRequiredService<IPingPongService>(); |
| | 116 | |
|
| | 117 | | // Create the communication service (infrastructure layer) |
| 0 | 118 | | var communicationService = new PeerCommunicationService(commLogger, messageService, _messageFactory, |
| 0 | 119 | | transportService.RemoteStaticPublicKey.Value, |
| 0 | 120 | | pingPongService, _serviceProvider); |
| | 121 | |
|
| | 122 | | // Create the application service (application layer) |
| 0 | 123 | | return new PeerService(communicationService, _nodeOptions.Features, appLogger, _nodeOptions.NetworkTimeout); |
| 0 | 124 | | } |
| | 125 | | } |