-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceHost.cs
689 lines (617 loc) · 33.4 KB
/
DeviceHost.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
using CK.Core;
using CK.PerfectEvent;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace CK.DeviceModel;
/// <summary>
/// Base class for <see cref="IDeviceHost"/> implementation.
/// <see cref="IDeviceHost"/> is a <see cref="ISingletonAutoService"/> that has the <see cref="IsMultipleAttribute"/>.
/// This is not a <see cref="Microsoft.Extensions.Hosting.IHostedService"/>: a device host has no Start/Stop, it is
/// passive and only manages its set of devices.
/// </summary>
/// <typeparam name="T">The Device type.</typeparam>
/// <typeparam name="THostConfiguration">The configuration type for this host.</typeparam>
/// <typeparam name="TConfiguration">The Device's configuration type.</typeparam>
[CKTypeDefiner]
public abstract partial class DeviceHost<T, THostConfiguration, TConfiguration> : IDeviceHost, IInternalDeviceHost
where T : Device<TConfiguration>
where THostConfiguration : DeviceHostConfiguration<TConfiguration>
where TConfiguration : DeviceConfiguration, new()
{
/// <summary>
/// This is the whole state of this Host. It is updated atomically (by setting a
/// new dictionary instance). All Find methods (and RouteCommand) can use it lock-free.
/// It is exposed by GetDevices() methods.
/// </summary>
Dictionary<string, T> _devices;
readonly PerfectEventSender<IDeviceHost, IReadOnlyDictionary<string, IDevice>> _baseDevicesChanged;
readonly PerfectEventSender<IDeviceHost, IReadOnlyDictionary<string, T>> _devicesChanged;
readonly PerfectEventSender<IDeviceHost, DeviceLifetimeEvent> _allDevicesLifetimeEvent;
readonly PerfectEventSender<IDeviceHost, BaseDeviceEvent> _allDevicesEvent;
/// <summary>
/// This lock uses the NoRecursion policy.
/// It protects the whole ApplyConfigurationAsync: only one global reconfiguration is
/// allowed at a time.
/// Reconfigurations or destruction can concurrently happen when the IDevice methods are used.
/// </summary>
readonly AsyncLock _applyConfigAsyncLock;
// Compile cached lambda.
readonly Func<THostConfiguration> _hostConfigFactory;
/// <summary>
/// ApplyConfigurationAsync starts by creating this by copying the lock free _devices inside
/// the _reconfigureSyncLock.
/// <para>
/// Then it first handles the creation of the new devices (still in the _reconfigureSyncLock)
/// and creates 3 lists: one with the device to reconfigure (the ones that already exist), one with the new devices
/// to start (AlwaysRunning or RunnableStarted) and one with the devices to destroy (if the configuration is not partial).
/// The _reconfigureSyncLock is released and the device InternalReconfigureAsync, StartAsync or DestroyAsync (from the 3
/// lists) are called, just as if they were called from any other threads.
/// </para>
/// <para>
/// The device's DestroyAsync method calls the synchronous OnDeviceDestroyed.
/// OnDeviceDestroyed enters the _reconfigureSyncLock and updates the _reconfiguringDevices field
/// if it's not null or creates a new dictionary (copying the _devices), update it and set it as the new _devices.
/// </para>
/// <para>
/// Once ApplyConfigurationAsync has called all required DestroyAsync methods,
/// it enters the _reconfigureSyncLock for the last time to set the _devices to the _reconfiguringDevices
/// and to reset the _reconfiguringDevices to null. It finally returns the array of DeviceConfigurationResult.
/// </para>
/// </summary>
Dictionary<string, T>? _reconfiguringDevices;
bool _reconfiguringDevicesChanged;
readonly object _reconfigureSyncLock;
/// <summary>
/// Initializes a new host.
/// </summary>
/// <param name="deviceHostName">A name that SHOULD identify this host instance unambiguously in a running context.</param>
protected DeviceHost( string deviceHostName )
: this( true )
{
Throw.CheckNotNullOrWhiteSpaceArgument( deviceHostName );
// The name of the lock is the DeviceHostName.
_applyConfigAsyncLock = new AsyncLock( LockRecursionPolicy.NoRecursion, deviceHostName );
}
/// <summary>
/// Initializes a new host with a <see cref="DeviceHostName"/> sets to its simple type name
/// (that should end, by convention, with "Host").
/// </summary>
protected DeviceHost()
: this( true )
{
_applyConfigAsyncLock = new AsyncLock( LockRecursionPolicy.NoRecursion, GetType().Name );
}
DeviceHost( bool privateCall )
{
_devices = new Dictionary<string, T>();
_baseDevicesChanged = new PerfectEventSender<IDeviceHost, IReadOnlyDictionary<string, IDevice>>();
_devicesChanged = new PerfectEventSender<IDeviceHost, IReadOnlyDictionary<string, T>>();
_allDevicesLifetimeEvent = new PerfectEventSender<IDeviceHost, DeviceLifetimeEvent>();
_allDevicesEvent = new PerfectEventSender<IDeviceHost, BaseDeviceEvent>();
_devicesChanged.CreateBridge( _baseDevicesChanged, e =>
{
// CK.Core v16.0.0 has not the AsIReadOnlyDictionary on IReadOnlyDictionary.
// This has been fixed in subsequent versions.
return ((Dictionary<string, T>)e).AsIReadOnlyDictionary<string, T, IDevice>();
} );
// Generates a typed delegate to instantiate the THostConfiguration dynamically used
// to apply a partial configuration.
var t = typeof( THostConfiguration );
var ctor = t.GetConstructor( Type.EmptyTypes );
if( ctor == null ) throw new InvalidOperationException( $"Type '{t.Name}' must have a default public constructor." );
var m = new DynamicMethod( "CreateInstance", t, Type.EmptyTypes, true );
ILGenerator ilGenerator = m.GetILGenerator();
ilGenerator.Emit( OpCodes.Newobj, ctor );
ilGenerator.Emit( OpCodes.Ret );
_hostConfigFactory = (Func<THostConfiguration>)m.CreateDelegate( typeof( Func<THostConfiguration> ) );
_reconfigureSyncLock = new DeviceHostLock();
_alwayRunningStopped = new List<(IInternalDevice Device, int Count, DateTime NextCall)>();
// Shut up the CS8618 warning is raised here: Non-nullable field '_applyConfigAsynclock' is uninitialized.
// (But keep the warning for any other fields.)
_applyConfigAsyncLock = null!;
}
/// <inheritdoc />
public string DeviceHostName => _applyConfigAsyncLock.Name;
/// <inheritdoc />
public int Count => _devices.Count;
Type IDeviceHost.GetDeviceHostConfigurationType() => typeof( THostConfiguration );
Type IDeviceHost.GetDeviceConfigurationType() => typeof( TConfiguration );
BaseConfigureDeviceCommand IInternalDeviceHost.CreateLockedConfigureCommand( string name,
string? controllerKey,
DeviceConfiguration? externalConfiguration,
DeviceConfiguration? clonedConfig )
{
return new InternalConfigureDeviceCommand<TConfiguration>( GetType(), externalConfiguration, clonedConfig, (name, controllerKey) );
}
BaseStartDeviceCommand IInternalDeviceHost.CreateStartCommand( string name ) => new InternalStartDeviceCommand( GetType(), name );
BaseStopDeviceCommand IInternalDeviceHost.CreateStopCommand( string name, bool ignoreAlwaysRunning ) => new InternalStopDeviceCommand( GetType(), name, ignoreAlwaysRunning );
BaseDestroyDeviceCommand IInternalDeviceHost.CreateDestroyCommand( string name ) => new InternalDestroyDeviceCommand( GetType(), name );
BaseSetControllerKeyDeviceCommand IInternalDeviceHost.CreateSetControllerKeyDeviceCommand( string name, string? current, string? newControllerKey ) => new InternalSetControllerKeyDeviceCommand( GetType(), name, current, newControllerKey );
bool IInternalDeviceHost.OnDeviceDoDestroy( IActivityMonitor monitor, IDevice device )
{
lock( _reconfigureSyncLock )
{
if( _reconfiguringDevices != null )
{
_reconfiguringDevices.Remove( device.Name );
_reconfiguringDevicesChanged = true;
return false;
}
var devices = new Dictionary<string, T>( _devices );
devices.Remove( device.Name );
_devices = devices;
// Returning true will call RaiseDevicesChangedEventAsync.
return true;
}
}
/// <summary>
/// Gets a device by its name.
/// <para>
/// This is efficient since it lookups an independent read only dictionary instance. No lock needed.
/// </para>
/// </summary>
/// <param name="deviceName">The device name to find.</param>
/// <returns>The device or null if not found.</returns>
public T? Find( string deviceName ) => _devices.GetValueOrDefault( deviceName );
/// <inheritdoc cref="Find(string)"/>
public T? this[string deviceName] => _devices.GetValueOrDefault( deviceName );
IDevice? IDeviceHost.Find( string deviceName ) => Find( deviceName );
IReadOnlyDictionary<string, IDevice> IDeviceHost.GetDevices() => _devices.AsIReadOnlyDictionary<string, T, IDevice>();
/// <summary>
/// Gets a snapshot of the current devices indexed by name.
/// This read only dictionary can be freely used (there is no concurrency issues), <see cref="DevicesChanged"/>
/// event can be used to monitor changes.
/// </summary>
/// <returns>A snapshot of the devices.</returns>
public IReadOnlyDictionary<string, T> GetDevices() => _devices;
PerfectEvent<IDeviceHost, IReadOnlyDictionary<string, IDevice>> IDeviceHost.DevicesChanged => _baseDevicesChanged.PerfectEvent;
/// <inheritdoc />
public PerfectEvent<IDeviceHost, IReadOnlyDictionary<string, T>> DevicesChanged => _devicesChanged.PerfectEvent;
Task IInternalDeviceHost.RaiseDevicesChangedEventAsync( IActivityMonitor monitor ) => RaiseDevicesChangedEventAsync( monitor );
Task RaiseDevicesChangedEventAsync( IActivityMonitor monitor ) => DaemonStoppedToken.IsCancellationRequested
? Task.CompletedTask
: _devicesChanged.SafeRaiseAsync( monitor,
this,
_devices );
/// <inheritdoc />
public PerfectEvent<IDeviceHost, DeviceLifetimeEvent> AllDevicesLifetimeEvent => _allDevicesLifetimeEvent.PerfectEvent;
/// <inheritdoc />
public PerfectEvent<IDeviceHost, BaseDeviceEvent> AllDevicesEvent => _allDevicesEvent.PerfectEvent;
Task IInternalDeviceHost.RaiseAllDevicesLifetimeEventAsync( IActivityMonitor monitor, DeviceLifetimeEvent e )
{
return DaemonStoppedToken.IsCancellationRequested
? Task.CompletedTask
: _allDevicesLifetimeEvent.SafeRaiseAsync( monitor, this, e );
}
Task IInternalDeviceHost.RaiseAllDevicesEventAsync( IActivityMonitor monitor, BaseDeviceEvent e )
{
return DaemonStoppedToken.IsCancellationRequested
? Task.CompletedTask
: _allDevicesEvent.SafeRaiseAsync( monitor, this, e );
}
/// <summary>
/// Captures the result of <see cref="ApplyConfigurationAsync"/>.
/// </summary>
public readonly struct ConfigurationResult
{
readonly IReadOnlyCollection<string>? _destroyedNames;
/// <summary>
/// Error constructor: only the initial configuration is provided.
/// </summary>
/// <param name="badConfiguration">The configuration.</param>
internal ConfigurationResult( THostConfiguration badConfiguration )
{
Success = false;
HostConfiguration = badConfiguration;
var r = new DeviceApplyConfigurationResult[badConfiguration.Items.Count];
Array.Fill( r, DeviceApplyConfigurationResult.InvalidConfiguration );
Results = r;
_destroyedNames = null;
}
internal ConfigurationResult( bool success, THostConfiguration initialConfiguration, DeviceApplyConfigurationResult[] r, HashSet<string>? destroyedNames )
{
Success = success;
HostConfiguration = initialConfiguration;
Results = r;
_destroyedNames = (IReadOnlyCollection<string>?)destroyedNames ?? Array.Empty<string>();
}
/// <summary>
/// Gets whether the configuration of the host succeeded.
/// </summary>
public bool Success { get; }
/// <summary>
/// Gets whether the error is due to an invalid or rejected <see cref="HostConfiguration"/>
/// (detailed <see cref="Results"/> for each device is null in such case).
/// </summary>
public bool InvalidHostConfiguration => !Success && _destroyedNames == null;
/// <summary>
/// Gets the original configuration.
/// </summary>
public THostConfiguration HostConfiguration { get; }
/// <summary>
/// Gets the detailed results for each <see cref="IDeviceHostConfiguration.Items"/>.
/// If <see cref="InvalidHostConfiguration"/> is true, they are all <see cref="DeviceApplyConfigurationResult.InvalidConfiguration"/>.
/// </summary>
public IReadOnlyList<DeviceApplyConfigurationResult> Results { get; }
/// <summary>
/// Gets the device names that have been destroyed if <see cref="IDeviceHostConfiguration.IsPartialConfiguration"/> is false.
/// Empty otherwise.
/// </summary>
public IReadOnlyCollection<string> DestroyedDeviceNames => _destroyedNames ?? Array.Empty<string>();
}
Task<DeviceApplyConfigurationResult> IDeviceHost.EnsureDeviceAsync( IActivityMonitor monitor, DeviceConfiguration configuration )
{
return EnsureDeviceAsync( monitor, (TConfiguration)configuration );
}
/// <summary>
/// Applies a device configuration: this ensures that the device exists (it is created if needed) and is
/// configured by the provided <paramref name="configuration"/>.
/// </summary>
/// <param name="monitor">The monitor to use.</param>
/// <param name="configuration">The configuration to apply.</param>
/// <returns>the result of the device configuration.</returns>
public async Task<DeviceApplyConfigurationResult> EnsureDeviceAsync( IActivityMonitor monitor, TConfiguration configuration )
{
Throw.CheckNotNullArgument( configuration );
THostConfiguration hostConfig = _hostConfigFactory();
hostConfig.Items.Add( configuration );
var result = await ApplyConfigurationAsync( monitor, hostConfig ).ConfigureAwait( false );
return result.Results[0];
}
async Task<bool> IDeviceHost.ApplyConfigurationAsync( IActivityMonitor monitor, IDeviceHostConfiguration configuration, bool allowEmptyConfiguration )
{
var r = await ApplyConfigurationAsync( monitor, (THostConfiguration)configuration, allowEmptyConfiguration ).ConfigureAwait( false );
return r.Success;
}
BaseConfigureDeviceCommand IDeviceHost.CreateConfigureCommand( DeviceConfiguration? configuration )
{
return new InternalConfigureDeviceCommand<TConfiguration>( GetType(), configuration, null );
}
/// <summary>
/// Applies a host configuration: multiple devices can be configured at once and if <see cref="DeviceHostConfiguration{TConfiguration}.IsPartialConfiguration"/> is false,
/// devices for which no configuration appear are stopped and destroyed.
/// </summary>
/// <param name="monitor">The monitor to use.</param>
/// <param name="configuration">The configuration to apply.</param>
/// <param name="allowEmptyConfiguration">By default, an empty configuration is considered as an error.</param>
/// <returns>The composite result of the potentially multiple configurations.</returns>
public virtual async Task<ConfigurationResult> ApplyConfigurationAsync( IActivityMonitor monitor, THostConfiguration configuration, bool allowEmptyConfiguration = false )
{
Throw.CheckNotNullArgument( monitor );
Throw.CheckNotNullArgument( configuration );
using var autoTag = monitor.TemporarilySetAutoTags( IDeviceHost.DeviceModel );
var safeConfig = configuration.DeepClone();
if( !safeConfig.CheckValidity( monitor, allowEmptyConfiguration ) ) return new ConfigurationResult( configuration );
var results = new DeviceApplyConfigurationResult[safeConfig.Items.Count];
// When the call to the constructor is enough, there will be no DeviceLifetimeEvent emitted.
// To be able to signal this device creation as a DeviceLifetimeEvent,
// we capture these beasts and emit a "fake" device event
// with Status/Configuration/ControllerKeyChanged all set to true for them.
List<IInternalDevice>? createdDevices = null;
bool success = true;
using( monitor.OpenInfo( $"Reconfiguring '{DeviceHostName}'. Applying {safeConfig.Items.Count} device configurations ({(safeConfig.IsPartialConfiguration ? "partial" : "full")} configuration)." ) )
{
await _applyConfigAsyncLock.EnterAsync( monitor ).ConfigureAwait( false );
try
{
HashSet<string>? toDestroy = null;
List<(int, T)>? toStart = null;
List<(int, T)>? toReconfigure = null;
// Capturing what we need to be able to work outside the sync lock.
lock( _reconfigureSyncLock )
{
_reconfiguringDevices = new Dictionary<string, T>( _devices );
_reconfiguringDevicesChanged = false;
if( !safeConfig.IsPartialConfiguration ) toDestroy = new HashSet<string>( _reconfiguringDevices.Keys );
int configIdx = 0;
foreach( var c in safeConfig.Items )
{
if( !_reconfiguringDevices.TryGetValue( c.Name, out var exists ) )
{
using( monitor.OpenTrace( $"Creating new device '{c.Name}'." ) )
{
var d = SafeCreateDevice( monitor, c, externalConfig: configuration.Items[configIdx] );
Debug.Assert( d == null || d.Name == c.Name );
if( d == null )
{
results[configIdx] = DeviceApplyConfigurationResult.CreateFailed;
success = false;
}
else
{
if( createdDevices == null ) createdDevices = new List<IInternalDevice>();
createdDevices.Add( d );
if( c.Status == DeviceConfigurationStatus.RunnableStarted || c.Status == DeviceConfigurationStatus.AlwaysRunning )
{
if( toStart == null ) toStart = new List<(int, T)>();
toStart.Add( (configIdx, d) );
}
else
{
_reconfiguringDevices.Add( c.Name, d );
results[configIdx] = DeviceApplyConfigurationResult.CreateSucceeded;
_reconfiguringDevicesChanged = true;
}
}
}
}
else
{
if( toDestroy != null ) toDestroy.Remove( c.Name );
if( toReconfigure == null ) toReconfigure = new List<(int, T)>();
toReconfigure.Add( (configIdx, exists) );
}
++configIdx;
}
}
// No more in the lock: applies the toDestroy, toReconfigure and eventually toStart lists.
if( toDestroy != null && toDestroy.Count > 0 )
{
using( monitor.OpenInfo( $"Destroying {toDestroy.Count} devices." ) )
{
foreach( var n in toDestroy )
{
var d = Find( n );
if( d != null && !d.IsDestroyed )
{
// We wait for the device to be destroyed here:
// resources from a destroyed devices may require an exclusive access
// that a reconfigured or started device (handled below) need.
await d.DestroyAsync( monitor ).ConfigureAwait( false );
}
else
{
monitor.Info( $"Device '{n}' is already destroyed." );
}
}
}
}
if( toReconfigure != null )
{
using( monitor.OpenInfo( $"Reconfiguring {toReconfigure.Count} devices." ) )
{
foreach( var (idx, d) in toReconfigure )
{
DeviceApplyConfigurationResult r = await d.InternalReconfigureAsync( monitor, configuration.Items[idx], safeConfig.Items[idx], default ).ConfigureAwait( false );
results[idx] = r;
success &= (r == DeviceApplyConfigurationResult.UpdateSucceeded || r == DeviceApplyConfigurationResult.None);
}
}
}
if( toStart != null )
{
using( monitor.OpenInfo( $"Starting {toStart.Count} new devices." ) )
{
DeviceApplyConfigurationResult r;
foreach( var (idx, d) in toStart )
{
if( await d.StartAsync( monitor ) )
{
r = DeviceApplyConfigurationResult.CreateAndStartSucceeded;
}
else
{
r = DeviceApplyConfigurationResult.CreateSucceededButStartFailed;
success = false;
}
results[idx] = r;
_reconfiguringDevices.Add( d.Name, d );
_reconfiguringDevicesChanged = true;
}
}
}
// Settling.
lock( _reconfigureSyncLock )
{
_devices = _reconfiguringDevices;
_reconfiguringDevices = null;
}
return new ConfigurationResult( success, configuration, results, toDestroy );
}
finally
{
_applyConfigAsyncLock.Leave( monitor );
if( _reconfiguringDevicesChanged )
{
await RaiseDevicesChangedEventAsync( monitor ).ConfigureAwait( false );
}
if( createdDevices != null )
{
foreach( var d in createdDevices )
{
await d.EnsureInitialLifetimeEventAsync( monitor );
}
}
}
}
}
/// <inheritdoc />
public async Task ClearAsync( IActivityMonitor monitor, bool waitForDeviceDestroyed )
{
using var autoTag = monitor.TemporarilySetAutoTags( IDeviceHost.DeviceModel );
if( waitForDeviceDestroyed )
{
await Task.WhenAll( ParrallelStartDestroy( monitor ) ).ConfigureAwait( false );
}
else
{
var d = _devices;
monitor.Info( $"Clearing '{DeviceHostName}': {d.Count} devices will be eventually destroyed." );
foreach( var device in d.Values )
{
await device.DestroyAsync( monitor, waitForDeviceDestroyed: false ).ConfigureAwait( false );
}
}
}
Task[] ParrallelStartDestroy( IActivityMonitor monitor )
{
Type thisType = GetType();
var d = _devices;
monitor.Info( $"Clearing '{DeviceHostName}': destroying {d.Count} devices." );
int i = 0;
Task[] all = new Task[d.Count];
foreach( var device in d.Values )
{
var cmd = new InternalDestroyDeviceCommand( thisType, device.Name );
device.UnsafeSendCommand( monitor, cmd );
all[i++] = cmd.Completion.Task;
}
return all;
}
T? SafeCreateDevice( IActivityMonitor monitor, TConfiguration config, TConfiguration externalConfig )
{
if( DaemonStoppedToken.IsCancellationRequested )
{
monitor.Trace( "System is shutting down. Skipping new device creation." );
return null;
}
try
{
if( !externalConfig.CheckValid( monitor ) )
{
monitor.Error( $"External configuration CheckValid failed but its clone has been validated. Something really weird happens." );
return null;
}
var device = CreateDevice( monitor, config, externalConfig );
if( device != null && device.Name != config.Name )
{
monitor.Error( $"Created device name mismatch expected '{config.Name}' got '{device.Name}'." );
return null;
}
return device;
}
catch( Exception ex )
{
monitor.Error( $"While trying to instantiate a device from {config.GetType().Name}.", ex );
return null;
}
}
/// <summary>
/// Helper that looks for a type from the same namespace and assembly than the <paramref name="typeConfiguration"/>:
/// the type configuration name must end with "Configuration" and the device must be the same name without this "Configuration" suffix
/// (or with a "Device" suffix).
/// </summary>
/// <param name="monitor">The monitor to use.</param>
/// <param name="typeConfiguration">The configuration's type.</param>
/// <returns>The device type or null if not found.</returns>
protected virtual Type? FindDeviceTypeByConvention( IActivityMonitor monitor, Type typeConfiguration )
{
var name = typeConfiguration.Name;
if( !name.EndsWith( "Configuration" ) )
{
monitor.Error( $"Configuration's type name should end with \"Configuration\" suffix." );
return null;
}
Debug.Assert( "Configuration".Length == 13 );
var fullName = typeConfiguration.FullName;
Debug.Assert( fullName != null );
var deviceFullName = fullName.Substring( 0, fullName.Length - 13 );
try
{
// Parameter throwOnError doesn't guaranty that NO exception is thrown.
return typeConfiguration.Assembly.GetType( deviceFullName, throwOnError: false )
?? typeConfiguration.Assembly.GetType( deviceFullName + "Device", throwOnError: false );
}
catch( Exception ex )
{
monitor.Error( $"While looking for type: '{deviceFullName}' or '{deviceFullName}Device'.", ex );
}
return null;
}
/// <summary>
/// Helper that uses <see cref="Activator.CreateInstance(Type, object[])"/> with the <paramref name="monitor"/> and the
/// result of the call to <see cref="CreateCreateInfo(TConfiguration, TConfiguration)"/> with <paramref name="config"/>
/// and <paramref name="externalConfig"/> as the constructor parameters.
/// </summary>
/// <param name="tDevice">The device type to instantiate.</param>
/// <param name="monitor">The monitor to use.</param>
/// <param name="config">The actual configuration (safe clone).</param>
/// <param name="externalConfig">The external configuration (original).</param>
/// <returns>The new device instance.</returns>
protected virtual T InstantiateDevice( Type tDevice, IActivityMonitor monitor, TConfiguration config, TConfiguration externalConfig )
{
return (T)Activator.CreateInstance( tDevice, new object[] { monitor, CreateCreateInfo( config, externalConfig ) } )!;
}
/// <summary>
/// Helper that creates a <see cref="Device{TConfiguration}.CreateInfo"/> opaque object.
/// </summary>
/// <param name="config">The actual configuration (safe clone).</param>
/// <param name="externalConfig">The external configuration (original).</param>
/// <returns>The create information.</returns>
protected Device<TConfiguration>.CreateInfo CreateCreateInfo( TConfiguration config, TConfiguration externalConfig )
{
return new Device<TConfiguration>.CreateInfo( config, externalConfig, this );
}
/// <summary>
/// Creates a <typeparamref name="T"/> device based on a <typeparamref name="TConfiguration"/> instance.
/// This default implementation uses the protected virtual <see cref="FindDeviceTypeByConvention(IActivityMonitor, Type)"/> to
/// locate the device type (based on <typeparamref name="TConfiguration"/> first and then the actual <paramref name="config"/>'s type)
/// and then (if found) instantiates it by calling the other helper <see cref="InstantiateDevice"/>.
/// </summary>
/// <param name="monitor">The monitor to use.</param>
/// <param name="config">The actual configuration (safe clone).</param>
/// <param name="externalConfig">The external configuration (original).</param>
/// <returns>A new device instance initialized with the <paramref name="config"/> or null on error.</returns>
protected virtual T? CreateDevice( IActivityMonitor monitor, TConfiguration config, TConfiguration externalConfig )
{
Debug.Assert( !String.IsNullOrWhiteSpace( config.Name ), "Already tested by DeviceHostConfiguration<TConfiguration>.CheckValidity." );
Debug.Assert( !_devices.ContainsKey( config.Name ), "Already tested by DeviceHostConfiguration<TConfiguration>.CheckValidity." );
var tDevice = FindDeviceTypeByConvention( monitor, typeof( TConfiguration ) );
if( tDevice == null )
{
var actualType = config.GetType();
monitor.Warn( $"Device type lookup based on TConfiguration interface type failed (formal type). Trying based on actual type '{actualType}'." );
tDevice = FindDeviceTypeByConvention( monitor, actualType );
if( tDevice == null )
{
monitor.Error( $"Unable to locate a Device type based on the configuration type." );
return null;
}
}
return InstantiateDevice( tDevice, monitor, config, externalConfig );
}
/// <inheritdoc />
public DeviceHostCommandResult SendCommand( IActivityMonitor monitor, BaseDeviceCommand command, bool checkControllerKey = true, CancellationToken token = default )
{
var (status, device) = ValidateAndRouteCommand( monitor, command );
if( status != DeviceHostCommandResult.Success ) return status;
Debug.Assert( device != null );
monitor.Trace( IDeviceHost.DeviceModel, $"{DeviceHostName}: sending {(command.ImmediateSending ? "immediate" : "")} '{command}' to '{device.Name}'." );
if( !(command.ImmediateSending
? device.SendRoutedCommandImmediate( command, checkControllerKey, token )
: device.SendRoutedCommand( command, checkControllerKey, token )) )
{
return DeviceHostCommandResult.DeviceDestroyed;
}
return DeviceHostCommandResult.Success;
}
/// <summary>
/// Helper that checks and routes a command to its device.
/// </summary>
/// <param name="monitor">The monitor to use.</param>
/// <param name="command">The command.</param>
/// <returns>The status and the device if found.</returns>
protected (DeviceHostCommandResult, T?) ValidateAndRouteCommand( IActivityMonitor monitor, BaseDeviceCommand command )
{
Throw.CheckNotNullArgument( monitor );
Throw.CheckNotNullArgument( command );
if( !command.HostType.IsAssignableFrom( GetType() ) ) return (DeviceHostCommandResult.InvalidHostType, null);
if( !command.CheckValidity( monitor ) ) return (DeviceHostCommandResult.CommandCheckValidityFailed, null);
// Should we do something like this here?
// command.DependentToken ??= monitor.CreateDependentToken();
Debug.Assert( command.DeviceName != null, "CheckValidity ensured that." );
var d = Find( command.DeviceName );
if( d == null )
{
monitor.Warn( $"Device named '{command.DeviceName}' not found in '{DeviceHostName}' host." );
return (DeviceHostCommandResult.DeviceNameNotFound, d);
}
return (DeviceHostCommandResult.Success, d);
}
}