Reformat/cleanup
All checks were successful
NuGet Package Publish / nuget (push) Successful in 1m10s
All checks were successful
NuGet Package Publish / nuget (push) Successful in 1m10s
This commit is contained in:
@@ -1,37 +1,33 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ZB.MOM.WW.CBDDC.Core;
|
||||
using Serilog.Context;
|
||||
using ZB.MOM.WW.CBDDC.Core.Cache;
|
||||
using ZB.MOM.WW.CBDDC.Core.Diagnostics;
|
||||
using ZB.MOM.WW.CBDDC.Core.Sync;
|
||||
using ZB.MOM.WW.CBDDC.Core.Storage;
|
||||
using ZB.MOM.WW.CBDDC.Network;
|
||||
using ZB.MOM.WW.CBDDC.Persistence.BLite;
|
||||
using Microsoft.Extensions.DependencyInjection; // For IServiceProvider if needed
|
||||
using Serilog.Context;
|
||||
using ZB.MOM.WW.CBDDC.Sample.Console;
|
||||
using ZB.MOM.WW.CBDDC.Core.Network;
|
||||
using ZB.MOM.WW.CBDDC.Core.Sync;
|
||||
using ZB.MOM.WW.CBDDC.Network;
|
||||
using ZB.MOM.WW.CBDDC.Network.Security;
|
||||
// For IServiceProvider if needed
|
||||
|
||||
namespace ZB.MOM.WW.CBDDC.Sample.Console;
|
||||
|
||||
public class ConsoleInteractiveService : BackgroundService
|
||||
{
|
||||
private readonly ILogger<ConsoleInteractiveService> _logger;
|
||||
private readonly SampleDbContext _db;
|
||||
private readonly ICBDDCNode _node;
|
||||
private readonly IHostApplicationLifetime _lifetime;
|
||||
|
||||
|
||||
// Auxiliary services for status/commands
|
||||
private readonly IDocumentCache _cache;
|
||||
private readonly IOfflineQueue _queue;
|
||||
private readonly ICBDDCHealthCheck _healthCheck;
|
||||
private readonly ISyncStatusTracker _syncTracker;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IPeerNodeConfigurationProvider _configProvider;
|
||||
private readonly SampleDbContext _db;
|
||||
private readonly ICBDDCHealthCheck _healthCheck;
|
||||
private readonly IHostApplicationLifetime _lifetime;
|
||||
private readonly ILogger<ConsoleInteractiveService> _logger;
|
||||
private readonly ICBDDCNode _node;
|
||||
private readonly IOfflineQueue _queue;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ISyncStatusTracker _syncTracker;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConsoleInteractiveService"/> class.
|
||||
/// Initializes a new instance of the <see cref="ConsoleInteractiveService" /> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger used by the interactive service.</param>
|
||||
/// <param name="db">The sample database context.</param>
|
||||
@@ -72,7 +68,7 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
{
|
||||
var config = await _configProvider.GetConfiguration();
|
||||
|
||||
System.Console.WriteLine($"--- Interactive Console ---");
|
||||
System.Console.WriteLine("--- Interactive Console ---");
|
||||
System.Console.WriteLine($"Node ID: {config.NodeId}");
|
||||
PrintHelp();
|
||||
|
||||
@@ -85,7 +81,7 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
continue;
|
||||
}
|
||||
|
||||
var input = System.Console.ReadLine();
|
||||
string? input = System.Console.ReadLine();
|
||||
if (string.IsNullOrEmpty(input)) continue;
|
||||
|
||||
try
|
||||
@@ -118,42 +114,53 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
System.Console.WriteLine(" [n]ew (auto), [s]pam (5x), [c]ount, [t]odos");
|
||||
System.Console.WriteLine(" [h]ealth, cac[h]e");
|
||||
System.Console.WriteLine(" [r]esolver [lww|merge], [demo] conflict");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private async Task HandleInput(string input)
|
||||
{
|
||||
var config = await _configProvider.GetConfiguration();
|
||||
if (input.StartsWith("n"))
|
||||
{
|
||||
var ts = DateTime.Now.ToString("HH:mm:ss.fff");
|
||||
var user = new User { Id = Guid.NewGuid().ToString(), Name = $"User-{ts}", Age = new Random().Next(18, 90), Address = new Address { City = "AutoCity" } };
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(), Name = $"User-{ts}", Age = new Random().Next(18, 90),
|
||||
Address = new Address { City = "AutoCity" }
|
||||
};
|
||||
await _db.Users.InsertAsync(user);
|
||||
await _db.SaveChangesAsync();
|
||||
System.Console.WriteLine($"[+] Created {user.Name} with Id: {user.Id}...");
|
||||
}
|
||||
else if (input.StartsWith("s"))
|
||||
{
|
||||
for (int i = 0; i < 5; i++)
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
var ts = DateTime.Now.ToString("HH:mm:ss.fff");
|
||||
var user = new User { Id = Guid.NewGuid().ToString(), Name = $"User-{ts}", Age = new Random().Next(18, 90), Address = new Address { City = "SpamCity" } };
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(), Name = $"User-{ts}", Age = new Random().Next(18, 90),
|
||||
Address = new Address { City = "SpamCity" }
|
||||
};
|
||||
await _db.Users.InsertAsync(user);
|
||||
System.Console.WriteLine($"[+] Created {user.Name} with Id: {user.Id}...");
|
||||
await Task.Delay(100);
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
else if (input.StartsWith("c"))
|
||||
{
|
||||
var userCount = _db.Users.FindAll().Count();
|
||||
var todoCount = _db.TodoLists.FindAll().Count();
|
||||
int userCount = _db.Users.FindAll().Count();
|
||||
int todoCount = _db.TodoLists.FindAll().Count();
|
||||
System.Console.WriteLine($"Collection 'Users': {userCount} documents");
|
||||
System.Console.WriteLine($"Collection 'TodoLists': {todoCount} documents");
|
||||
}
|
||||
else if (input.StartsWith("p"))
|
||||
{
|
||||
var alice = new User { Id = Guid.NewGuid().ToString(), Name = "Alice", Age = 30, Address = new Address { City = "Paris" } };
|
||||
var bob = new User { Id = Guid.NewGuid().ToString(), Name = "Bob", Age = 25, Address = new Address { City = "Rome" } };
|
||||
var alice = new User
|
||||
{ Id = Guid.NewGuid().ToString(), Name = "Alice", Age = 30, Address = new Address { City = "Paris" } };
|
||||
var bob = new User
|
||||
{ Id = Guid.NewGuid().ToString(), Name = "Bob", Age = 25, Address = new Address { City = "Rome" } };
|
||||
await _db.Users.InsertAsync(alice);
|
||||
await _db.Users.InsertAsync(bob);
|
||||
await _db.SaveChangesAsync();
|
||||
@@ -162,17 +169,19 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
else if (input.StartsWith("g"))
|
||||
{
|
||||
System.Console.Write("Enter user Id: ");
|
||||
var id = System.Console.ReadLine();
|
||||
string? id = System.Console.ReadLine();
|
||||
if (!string.IsNullOrEmpty(id))
|
||||
{
|
||||
var u = _db.Users.FindById(id);
|
||||
System.Console.WriteLine(u != null ? $"Got: {u.Name}, Age {u.Age}, City: {u.Address?.City}" : "Not found");
|
||||
System.Console.WriteLine(u != null
|
||||
? $"Got: {u.Name}, Age {u.Age}, City: {u.Address?.City}"
|
||||
: "Not found");
|
||||
}
|
||||
}
|
||||
else if (input.StartsWith("d"))
|
||||
{
|
||||
System.Console.Write("Enter user Id to delete: ");
|
||||
var id = System.Console.ReadLine();
|
||||
string? id = System.Console.ReadLine();
|
||||
if (!string.IsNullOrEmpty(id))
|
||||
{
|
||||
await _db.Users.DeleteAsync(id);
|
||||
@@ -183,8 +192,8 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
else if (input.StartsWith("l"))
|
||||
{
|
||||
var peers = _node.Discovery.GetActivePeers();
|
||||
var handshakeSvc = _serviceProvider.GetService<ZB.MOM.WW.CBDDC.Network.Security.IPeerHandshakeService>();
|
||||
var secureIcon = handshakeSvc != null ? "🔒" : "🔓";
|
||||
var handshakeSvc = _serviceProvider.GetService<IPeerHandshakeService>();
|
||||
string secureIcon = handshakeSvc != null ? "🔒" : "🔓";
|
||||
|
||||
System.Console.WriteLine($"Active Peers ({secureIcon}):");
|
||||
foreach (var p in peers)
|
||||
@@ -203,7 +212,7 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
{
|
||||
var health = await _healthCheck.CheckAsync();
|
||||
var syncStatus = _syncTracker.GetStatus();
|
||||
var handshakeSvc = _serviceProvider.GetService<ZB.MOM.WW.CBDDC.Network.Security.IPeerHandshakeService>();
|
||||
var handshakeSvc = _serviceProvider.GetService<IPeerHandshakeService>();
|
||||
|
||||
System.Console.WriteLine("=== Health Check ===");
|
||||
System.Console.WriteLine($"Database: {(health.DatabaseHealthy ? "✓" : "✗")}");
|
||||
@@ -216,17 +225,18 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
if (health.Errors.Any())
|
||||
{
|
||||
System.Console.WriteLine("Errors:");
|
||||
foreach (var err in health.Errors.Take(3)) System.Console.WriteLine($" - {err}");
|
||||
foreach (string err in health.Errors.Take(3)) System.Console.WriteLine($" - {err}");
|
||||
}
|
||||
}
|
||||
else if (input.StartsWith("ch") || input == "cache")
|
||||
{
|
||||
var stats = _cache.GetStatistics();
|
||||
System.Console.WriteLine($"=== Cache Stats ===\nSize: {stats.Size}\nHits: {stats.Hits}\nMisses: {stats.Misses}\nRate: {stats.HitRate:P1}");
|
||||
System.Console.WriteLine(
|
||||
$"=== Cache Stats ===\nSize: {stats.Size}\nHits: {stats.Hits}\nMisses: {stats.Misses}\nRate: {stats.HitRate:P1}");
|
||||
}
|
||||
else if (input.StartsWith("r") && input.Contains("resolver"))
|
||||
{
|
||||
var parts = input.Split(' ');
|
||||
string[] parts = input.Split(' ');
|
||||
if (parts.Length > 1)
|
||||
{
|
||||
var newResolver = parts[1].ToLower() switch
|
||||
@@ -240,7 +250,7 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
{
|
||||
// Note: Requires restart to fully apply. For demo, we inform user.
|
||||
System.Console.WriteLine($"⚠️ Resolver changed to {parts[1].ToUpper()}. Restart node to apply.");
|
||||
System.Console.WriteLine($" (Current session continues with previous resolver)");
|
||||
System.Console.WriteLine(" (Current session continues with previous resolver)");
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -262,7 +272,7 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
System.Console.WriteLine($"📋 {list.Name} ({list.Items.Count} items)");
|
||||
foreach (var item in list.Items)
|
||||
{
|
||||
var status = item.Completed ? "✓" : " ";
|
||||
string status = item.Completed ? "✓" : " ";
|
||||
System.Console.WriteLine($" [{status}] {item.Task}");
|
||||
}
|
||||
}
|
||||
@@ -281,8 +291,8 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
Name = "Shopping List",
|
||||
Items = new List<TodoItem>
|
||||
{
|
||||
new TodoItem { Task = "Buy milk", Completed = false },
|
||||
new TodoItem { Task = "Buy bread", Completed = false }
|
||||
new() { Task = "Buy milk", Completed = false },
|
||||
new() { Task = "Buy bread", Completed = false }
|
||||
}
|
||||
};
|
||||
|
||||
@@ -325,24 +335,20 @@ public class ConsoleInteractiveService : BackgroundService
|
||||
System.Console.WriteLine($" List: {merged.Name}");
|
||||
foreach (var item in merged.Items)
|
||||
{
|
||||
var status = item.Completed ? "✓" : " ";
|
||||
string status = item.Completed ? "✓" : " ";
|
||||
System.Console.WriteLine($" [{status}] {item.Task}");
|
||||
}
|
||||
|
||||
var resolver = _serviceProvider.GetRequiredService<IConflictResolver>();
|
||||
var resolverType = resolver.GetType().Name;
|
||||
string resolverType = resolver.GetType().Name;
|
||||
System.Console.WriteLine($"\nℹ️ Resolution Strategy: {resolverType}");
|
||||
|
||||
if (resolverType.Contains("Recursive"))
|
||||
{
|
||||
System.Console.WriteLine(" → Items merged by 'id', both edits preserved");
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Console.WriteLine(" → Last write wins, Node B changes override Node A");
|
||||
}
|
||||
}
|
||||
|
||||
System.Console.WriteLine("\n✓ Demo complete. Run 'todos' to see all lists.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user