Skip to content

Commit

Permalink
Merge pull request #99 from s2quake/refactor/improve-console
Browse files Browse the repository at this point in the history
Improve console
  • Loading branch information
s2quake authored Dec 1, 2024
2 parents a0cb7c3 + 7743823 commit e8ec9c3
Show file tree
Hide file tree
Showing 10 changed files with 426 additions and 312 deletions.
213 changes: 0 additions & 213 deletions src/console/LibplanetConsole.Console/BlockChain.cs

This file was deleted.

64 changes: 32 additions & 32 deletions src/console/LibplanetConsole.Console/ClientCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LibplanetConsole.Console;
internal sealed class ClientCollection(
IServiceProvider serviceProvider,
IApplicationOptions options)
: IEnumerable<Client>, IClientCollection
: ConsoleContentBase("clients"), IEnumerable<Client>, IClientCollection
{
private static readonly object LockObject = new();
private readonly List<Client> _clientList = new(options.Clients.Length);
Expand Down Expand Up @@ -119,37 +119,6 @@ public async Task AttachAsync(
await client.AttachAsync(cancellationToken);
}

public async Task StartAsync(CancellationToken cancellationToken)
{
try
{
for (var i = 0; i < _clientList.Capacity; i++)
{
var client = ClientFactory.CreateNew(serviceProvider, options.Clients[i]);
InsertClient(client);
}

Current = _clientList.FirstOrDefault();
}
catch (Exception e)
{
_logger.LogError(e, "An error occurred while starting clients.");
}

await Task.CompletedTask;
}

public async Task StopAsync(CancellationToken cancellationToken)
{
for (var i = _clientList.Count - 1; i >= 0; i--)
{
var client = _clientList[i]!;
client.Disposed -= Client_Disposed;
await ClientFactory.DisposeScopeAsync(client);
_logger.LogDebug("Disposed a client: {Address}", client.Address);
}
}

public async Task InitializeAsync(CancellationToken cancellationToken)
{
for (var i = 0; i < _clientList.Count; i++)
Expand Down Expand Up @@ -194,6 +163,37 @@ IEnumerator<IClient> IEnumerable<IClient>.GetEnumerator()
IEnumerator IEnumerable.GetEnumerator()
=> _clientList.GetEnumerator();

protected override async Task OnStartAsync(CancellationToken cancellationToken)
{
try
{
for (var i = 0; i < _clientList.Capacity; i++)
{
var client = ClientFactory.CreateNew(serviceProvider, options.Clients[i]);
InsertClient(client);
}

Current = _clientList.FirstOrDefault();
}
catch (Exception e)
{
_logger.LogError(e, "An error occurred while starting clients.");
}

await Task.CompletedTask;
}

protected override async Task OnStopAsync(CancellationToken cancellationToken)
{
for (var i = _clientList.Count - 1; i >= 0; i--)
{
var client = _clientList[i]!;
client.Disposed -= Client_Disposed;
await ClientFactory.DisposeScopeAsync(client);
_logger.LogDebug("Disposed a client: {Address}", client.Address);
}
}

private void Client_Disposed(object? sender, EventArgs e)
{
if (sender is Client client)
Expand Down
40 changes: 40 additions & 0 deletions src/console/LibplanetConsole.Console/ConsoleContentBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace LibplanetConsole.Console;

public abstract class ConsoleContentBase(string name) : IConsoleContent, IDisposable
{
private readonly string _name = name;
private bool _isDisposed;

public string Name => _name != string.Empty ? _name : GetType().Name;

public virtual IEnumerable<IConsoleContent> Dependencies => [];

void IDisposable.Dispose()
{
OnDispose(disposing: true);
GC.SuppressFinalize(this);
}

Task IConsoleContent.StartAsync(CancellationToken cancellationToken)
=> OnStartAsync(cancellationToken);

Task IConsoleContent.StopAsync(CancellationToken cancellationToken)
=> OnStopAsync(cancellationToken);

protected abstract Task OnStartAsync(CancellationToken cancellationToken);

protected abstract Task OnStopAsync(CancellationToken cancellationToken);

protected virtual void OnDispose(bool disposing)
{
if (!_isDisposed)
{
if (disposing)
{
// do nothing
}

_isDisposed = true;
}
}
}
Loading

0 comments on commit e8ec9c3

Please sign in to comment.