1 using Microsoft.Extensions.Logging;
4 using System.Collections.Generic;
8 using System.Threading.Tasks;
24 readonly ILogger<CommContext>
logger;
60 this.ioManager = ioManager ??
throw new ArgumentNullException(nameof(ioManager));
61 this.logger = logger ??
throw new ArgumentNullException(nameof(logger));
63 directory = ioManager.
ResolvePath(directory) ??
throw new ArgumentNullException(nameof(directory));
65 throw new ArgumentNullException(nameof(filter));
67 fileSystemWatcher =
new FileSystemWatcher(directory, filter)
69 EnableRaisingEvents =
true,
70 IncludeSubdirectories =
false,
71 NotifyFilter = NotifyFilters.LastWrite
74 fileSystemWatcher.Created += HandleWrite;
75 fileSystemWatcher.Changed += HandleWrite;
77 cancellationTokenSource =
new CancellationTokenSource();
78 cancellationToken = cancellationTokenSource.Token;
88 fileSystemWatcher.Dispose();
89 cancellationTokenSource.Cancel();
90 cancellationTokenSource.Dispose();
102 var fileBytes = await ioManager.ReadAllBytes(e.FullPath, cancellationToken).ConfigureAwait(
false);
103 var file = Encoding.UTF8.GetString(fileBytes);
105 logger.LogTrace(
"Read interop command json: {0}", file);
112 Parameters = JsonConvert.DeserializeObject<IReadOnlyDictionary<string, object>>(file),
116 catch (JsonException ex)
119 logger.LogDebug(
"Suppressing json convert exception for command file write: {0}", ex);
123 await (handler?.HandleInterop(command, cancellationToken) ?? Task.CompletedTask).ConfigureAwait(
false);
125 catch (OperationCanceledException) { }
128 logger.LogDebug(
"Exception while trying to handle command json write: {0}", ex);
135 if (this.handler != null)
136 throw new InvalidOperationException(
"RegisterHandler already called!");
137 this.handler = handler ??
throw new ArgumentNullException(nameof(handler));
Use server authentication
readonly CancellationToken cancellationToken
The CancellationToken for the CommContext
Represents a registration of an interop session
readonly FileSystemWatcher fileSystemWatcher
The FileSystemWatcher for the CommContext
string ResolvePath(string path)
Retrieve the full path of some path given a relative path. Must be used before passing relative path...
readonly ILogger< CommContext > logger
The ILogger for the CommContext
void RegisterHandler(ICommHandler handler)
Register a handler with the ICommContext
ICommHandler handler
The ICommHandler for the CommContext
Represents a command from DD
CommContext(IIOManager ioManager, ILogger< CommContext > logger, string directory, string filter)
Construct an CommContext
readonly IIOManager ioManager
The IIOManager for the CommContext
bool disposed
If the CommContext has been disposed
readonly CancellationTokenSource cancellationTokenSource
The CancellationTokenSource for the CommContext
async void HandleWrite(object sender, FileSystemEventArgs e)
Runs when the fileSystemWatcher triggers
Interface for using filesystems