using System.Collections.Concurrent; using Microsoft.Extensions.Logging; public class PrintServer { private readonly IPrintService _printService; private readonly ILogger _logger; private readonly ConcurrentDictionary _printerQueues; public PrintServer(IPrintService printService, ILogger logger) { _printService = printService; _logger = logger; _printerQueues = new ConcurrentDictionary(); } public void RegisterPrinter(string printerIp) { var queue = new PrinterQueue(printerIp, _printService, _logger); if (_printerQueues.TryAdd(printerIp, queue)) { queue.Start(); Console.WriteLine($"Printer {printerIp} registered"); } } public void UnregisterPrinter(string printerIp) { if (_printerQueues.TryRemove(printerIp, out var queue)) { queue.StopAsync().Wait(); Console.WriteLine($"Printer {printerIp} unregistered"); } } public void SubmitJob(string printerIp, PrintJob job) { if (_printerQueues.TryGetValue(printerIp, out var queue)) { queue.Enqueue(job); } else { _logger.LogWarning( $"Printer {printerIp} not found. Job cannot be submitted."); } } public Dictionary GetQueueStatus() { var status = new Dictionary(); foreach (var kvp in _printerQueues) { status[kvp.Key] = kvp.Value.QueueLength; } return status; } public async Task ShutdownAsync() { var stopTasks = _printerQueues.Values.Select(q => q.StopAsync()); await Task.WhenAll(stopTasks); Console.WriteLine("Print server shut down"); } }