Skip to content

Commit

Permalink
refactor: Endpoint to port
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Oct 13, 2024
1 parent 0b15963 commit d9deafa
Show file tree
Hide file tree
Showing 36 changed files with 199 additions and 134 deletions.
3 changes: 2 additions & 1 deletion src/client/LibplanetConsole.Client.Executable/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal sealed class Application

public Application(ApplicationOptions options, object[] instances)
{
var (_, port) = EndPointUtility.GetHostAndPort(options.EndPoint);
var port = options.Port;
var services = _builder.Services;
foreach (var instance in instances)
{
Expand All @@ -27,6 +27,7 @@ public Application(ApplicationOptions options, object[] instances)
_builder.WebHost.ConfigureKestrel(options =>
{
options.ListenLocalhost(port, o => o.Protocols = HttpProtocols.Http2);
options.ListenLocalhost(port + 1, o => o.Protocols = HttpProtocols.Http1AndHttp2);
});

if (options.LogPath != string.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace LibplanetConsole.Client.Executable;
internal sealed record class ApplicationSettings
{
[CommandProperty]
[CommandSummary("Indicates the EndPoint on which the client will run. " +
"If omitted, a random endpoint is used.")]
[EndPoint]
public string EndPoint { get; init; } = string.Empty;
[CommandSummary("Indicates the port on which the client will run. " +
"If omitted, a random port is used.")]
[NonNegative]
public int Port { get; init; }

[CommandProperty]
[CommandSummary("Indicates the private key of the client. " +
Expand Down Expand Up @@ -47,9 +47,9 @@ internal sealed record class ApplicationSettings

public ApplicationOptions ToOptions()
{
var endPoint = EndPointUtility.ParseOrNext(EndPoint);
var port = Port;
var privateKey = PrivateKeyUtility.ParseOrRandom(PrivateKey);
return new ApplicationOptions(endPoint, privateKey)
return new ApplicationOptions(port, privateKey)
{
ParentProcessId = ParentProcessId,
NodeEndPoint = EndPointUtility.ParseOrDefault(NodeEndPoint),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public InitializeCommand()
public string PrivateKey { get; init; } = string.Empty;

[CommandProperty]
[CommandSummary("The endpoint of the client. " +
[CommandSummary("The port of the client. " +
"If omitted, a random endpoint is used.")]
[EndPoint]
public string EndPoint { get; set; } = string.Empty;
[NonNegative]
public int Port { get; set; }

[CommandProperty]
[CommandSummary("Indicates the file path to save logs.")]
Expand All @@ -44,12 +44,12 @@ public InitializeCommand()
protected override void OnExecute()
{
var outputPath = Path.GetFullPath(OutputPath);
var endPoint = EndPointUtility.ParseOrNext(EndPoint);
var port = Port == 0 ? PortUtility.NextPort() : Port;
var privateKey = PrivateKeyUtility.ParseOrRandom(PrivateKey);
var logPath = Path.Combine(outputPath, LogPath.Fallback("log"));
var repository = new Repository
{
EndPoint = endPoint,
Port = port,
PrivateKey = privateKey,
LogPath = logPath,
};
Expand Down
9 changes: 3 additions & 6 deletions src/client/LibplanetConsole.Client.Executable/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ public sealed record class Repository
public const string SettingsFileName = "client-settings.json";
public const string SettingsSchemaFileName = "client-settings-schema.json";

public required EndPoint EndPoint { get; init; }
public required int Port { get; init; }

public required PrivateKey PrivateKey { get; init; }

public EndPoint? NodeEndPoint { get; init; }

public string LogPath { get; init; } = string.Empty;

public string Source { get; private set; } = string.Empty;

public static Repository Load(string settingsPath)
{
if (Path.IsPathRooted(settingsPath) is false)
Expand All @@ -37,10 +35,9 @@ public static Repository Load(string settingsPath)

return new()
{
EndPoint = EndPointUtility.Parse(applicationSettings.EndPoint),
Port = applicationSettings.Port,
PrivateKey = new PrivateKey(applicationSettings.PrivateKey),
LogPath = Path.GetFullPath(applicationSettings.LogPath, directoryName),
Source = settingsPath,
NodeEndPoint = EndPointUtility.ParseOrDefault(applicationSettings.NodeEndPoint),
};
}
Expand Down Expand Up @@ -80,7 +77,7 @@ public dynamic Save(string repositoryPath)
Schema = SettingsSchemaFileName,
Application = new ApplicationSettings
{
EndPoint = EndPointUtility.ToString(EndPoint),
Port = Port,
PrivateKey = PrivateKeyUtility.ToString(privateKey),
LogPath = GetRelativePathFromDirectory(repositoryPath, LogPath),
NodeEndPoint = EndPointUtility.ToString(NodeEndPoint),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ void IDisposable.Dispose()

private void Client_Started(object? sender, EventArgs e)
{
var endPoint = _options.EndPoint;
var endPoint = _options.Port;
var message = $"BlockChain has been started.: {endPoint}";
Console.Out.WriteColoredLine(message, TerminalColorType.BrightGreen);
}

private void Client_Stopped(object? sender, EventArgs e)
{
var endPoint = _options.EndPoint;
var endPoint = _options.Port;
var message = $"BlockChain has been stopped.: {endPoint}";
Console.Out.WriteColoredLine(message, TerminalColorType.BrightGreen);
}
Expand Down
3 changes: 1 addition & 2 deletions src/client/LibplanetConsole.Client/ApplicationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace LibplanetConsole.Client;

public readonly record struct ApplicationInfo
{
[JsonConverter(typeof(EndPointJsonConverter))]
public required EndPoint EndPoint { get; init; }
public required int Port { get; init; }

[JsonConverter(typeof(EndPointJsonConverter))]
public EndPoint? NodeEndPoint { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public ApplicationInfoProvider(ApplicationOptions options)
{
_info = new()
{
EndPoint = options.EndPoint,
Port = options.Port,
NodeEndPoint = options.NodeEndPoint,
LogPath = options.LogPath,
};
Expand Down
6 changes: 3 additions & 3 deletions src/client/LibplanetConsole.Client/ApplicationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ namespace LibplanetConsole.Client;

public sealed record class ApplicationOptions
{
public ApplicationOptions(EndPoint endPoint, PrivateKey privateKey)
public ApplicationOptions(int port, PrivateKey privateKey)
{
EndPoint = endPoint;
Port = port;
PrivateKey = privateKey;
}

public EndPoint EndPoint { get; }
public int Port { get; }

public PrivateKey PrivateKey { get; }

Expand Down
5 changes: 5 additions & 0 deletions src/common/LibplanetConsole.Common/EndPointUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public static (string Host, int Port) GetHostAndPort(EndPoint endPoint)
};
}

public static int GetPort(EndPoint endPoint)
=> GetHostAndPort(endPoint).Port;

public static DnsEndPoint GetLocalHost(int port) => new("localhost", port);

public static string ToString(EndPoint? endPoint)
{
if (endPoint is DnsEndPoint dnsEndPoint)
Expand Down
2 changes: 1 addition & 1 deletion src/common/LibplanetConsole.Common/PortUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class PortUtility
private static readonly List<int> UsedPortList = [];
private static readonly object LockObject = new();

public static int GetPort()
public static int NextPort()
{
lock (LockObject)
{
Expand Down
7 changes: 4 additions & 3 deletions src/common/LibplanetConsole.Seed/SeedNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Libplanet.Net.Transports;
using LibplanetConsole.Common;
using Serilog;
using static LibplanetConsole.Common.EndPointUtility;

namespace LibplanetConsole.Seed;

Expand All @@ -30,7 +31,7 @@ public static readonly AppProtocolVersion AppProtocolVersion
public PeerCollection Peers { get; } = new(seedOptions);

public BoundPeer BoundPeer { get; } = new(
seedOptions.PrivateKey.PublicKey, (DnsEndPoint)seedOptions.EndPoint);
seedOptions.PrivateKey.PublicKey, GetLocalHost(seedOptions.Port));

public async Task StartAsync(CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -107,8 +108,8 @@ private static async Task<NetMQTransport> CreateTransport(SeedOptions seedOption
AppProtocolVersion = appProtocolVersion,
TrustedAppProtocolVersionSigners = [],
};
var (host, port) = EndPointUtility.GetHostAndPort(seedOptions.EndPoint);
var hostOptions = new HostOptions(host, [], port);
var port = seedOptions.Port;
var hostOptions = new HostOptions("localhost", [], port);
return await NetMQTransport.Create(privateKey, appProtocolVersionOptions, hostOptions);
}

Expand Down
6 changes: 2 additions & 4 deletions src/common/LibplanetConsole.Seed/SeedOptions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Net;

namespace LibplanetConsole.Seed;
namespace LibplanetConsole.Seed;

public sealed record class SeedOptions
{
public required PrivateKey PrivateKey { get; init; }

public required EndPoint EndPoint { get; init; }
public required int Port { get; init; }

public TimeSpan RefreshInterval { get; init; } = TimeSpan.FromSeconds(5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal sealed class Application

public Application(ApplicationOptions options, object[] instances)
{
var (_, port) = EndPointUtility.GetHostAndPort(options.EndPoint);
var port = options.Port;
var services = _builder.Services;

foreach (var instance in instances)
Expand All @@ -32,6 +32,7 @@ public Application(ApplicationOptions options, object[] instances)
_builder.WebHost.ConfigureKestrel(options =>
{
options.ListenLocalhost(port, o => o.Protocols = HttpProtocols.Http2);
options.ListenLocalhost(port + 1, o => o.Protocols = HttpProtocols.Http1AndHttp2);
});

if (options.LogPath != string.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
using System.Text.Json.Serialization;
using JSSoft.Commands;
using LibplanetConsole.Common;
using LibplanetConsole.Common.DataAnnotations;
using LibplanetConsole.DataAnnotations;
using LibplanetConsole.Framework;
using static LibplanetConsole.Common.EndPointUtility;

namespace LibplanetConsole.Console.Executable;

[ApplicationSettings(IsRequired = true)]
internal sealed record class ApplicationSettings
{
[CommandProperty]
[CommandSummary("The endpoint of the libplanet-console. " +
"If omitted, a random endpoint is used.")]
[EndPoint]
public string EndPoint { get; init; } = string.Empty;
[CommandSummary("The port of the libplanet-console. " +
"If omitted, a random port is used.")]
[NonNegative]
public int Port { get; init; }

#if DEBUG
[CommandProperty(InitValue = 1)]
Expand Down Expand Up @@ -91,12 +91,13 @@ internal sealed record class ApplicationSettings

public ApplicationOptions ToOptions()
{
var endPoint = EndPointUtility.ParseOrNext(EndPoint);
var port = Port == 0 ? PortUtility.NextPort() : Port;
var endPoint = GetLocalHost(port);
var nodeOptions = GetNodeOptions(endPoint, GetNodes());
var clientOptions = GetClientOptions(nodeOptions, GetClients());
var repository = new Repository(endPoint, nodeOptions, clientOptions);
var repository = new Repository(port, nodeOptions, clientOptions);
var genesis = TryGetGenesis(out var g) == true ? g : repository.Genesis;
return new ApplicationOptions(endPoint)
return new ApplicationOptions(port)
{
LogPath = GetFullPath(LogPath),
Nodes = repository.Nodes,
Expand Down Expand Up @@ -133,7 +134,7 @@ private static NodeOptions[] GetNodeOptions(
{
return [.. nodePrivateKeys.Select(key => new NodeOptions
{
EndPoint = EndPointUtility.NextEndPoint(),
EndPoint = NextEndPoint(),
PrivateKey = key,
SeedEndPoint = endPoint,
})];
Expand All @@ -144,7 +145,7 @@ private static ClientOptions[] GetClientOptions(
{
return [.. clientPrivateKeys.Select(key => new ClientOptions
{
EndPoint = EndPointUtility.NextEndPoint(),
EndPoint = NextEndPoint(),
NodeEndPoint = Random(nodeOptions).EndPoint,
PrivateKey = key,
})];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal sealed class ClientRepositoryProcess : ClientProcessBase
{
public required PrivateKey PrivateKey { get; init; }

public required EndPoint EndPoint { get; init; }
public required int Port { get; init; }

public string OutputPath { get; set; } = string.Empty;

Expand All @@ -16,7 +16,7 @@ internal sealed class ClientRepositoryProcess : ClientProcessBase
OutputPath,
"--private-key",
PrivateKeyUtility.ToString(PrivateKey),
"--end-point",
EndPointUtility.ToString(EndPoint),
"--port",
$"{Port}",
];
}
Loading

0 comments on commit d9deafa

Please sign in to comment.