|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: Communication |
| 4 | + * FILE: SimpleLogServer.cs |
| 5 | + * PURPOSE: Facade to simplify usage of LogCollectorServer for users who just want to provide a simple callback for log processing without needing to implement the ILogProcessor interface themselves. |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Communication.Interfaces; |
| 13 | + |
| 14 | +namespace Communication |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// A "Stupid Simple" wrapper for the LogCollector. |
| 18 | + /// No DI required. Just new it up and go. |
| 19 | + /// </summary> |
| 20 | + public class SimpleLogServer : IDisposable |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// The internal server |
| 24 | + /// </summary> |
| 25 | + private readonly LogCollectorServer _internalServer; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// The CTS |
| 29 | + /// </summary> |
| 30 | + private readonly CancellationTokenSource _cts; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The server task |
| 34 | + /// </summary> |
| 35 | + private Task _serverTask; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Initializes a new instance of the <see cref="SimpleLogServer"/> class. |
| 39 | + /// </summary> |
| 40 | + /// <param name="port">The port.</param> |
| 41 | + /// <param name="onLogReceived">The on log received.</param> |
| 42 | + public SimpleLogServer(int port, Action<string> onLogReceived) |
| 43 | + : this(port, new ActionLogProcessor(onLogReceived)) |
| 44 | + { |
| 45 | + // This constructor just wraps the Action into our helper class |
| 46 | + // and chains to Constructor 2. |
| 47 | + } |
| 48 | + /// <summary> |
| 49 | + /// Initializes a new instance of the <see cref="SimpleLogServer"/> class. |
| 50 | + /// </summary> |
| 51 | + /// <param name="port">The port.</param> |
| 52 | + /// <param name="processor">The processor.</param> |
| 53 | + /// <exception cref="System.ArgumentNullException">processor</exception> |
| 54 | + public SimpleLogServer(int port, ILogProcessor processor) |
| 55 | + { |
| 56 | + if (processor == null) throw new ArgumentNullException(nameof(processor)); |
| 57 | + |
| 58 | + _cts = new CancellationTokenSource(); |
| 59 | + _internalServer = new LogCollectorServer(port, processor); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Starts this instance. |
| 64 | + /// </summary> |
| 65 | + public void Start() |
| 66 | + { |
| 67 | + if (_serverTask != null) return; // Already running |
| 68 | + |
| 69 | + // Run the server in a background task so it doesn't block the main thread |
| 70 | + _serverTask = Task.Run(() => _internalServer.StartAsync(_cts.Token)); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Stops this instance. |
| 75 | + /// </summary> |
| 76 | + public void Stop() |
| 77 | + { |
| 78 | + _cts.Cancel(); |
| 79 | + try |
| 80 | + { |
| 81 | + _serverTask?.Wait(); // Wait for it to finish cleaning up |
| 82 | + } |
| 83 | + catch (AggregateException) { /* Ignore cancellation errors */ } |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| 88 | + /// </summary> |
| 89 | + public void Dispose() |
| 90 | + { |
| 91 | + Stop(); |
| 92 | + _cts.Dispose(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments