-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Props.cs
820 lines (726 loc) · 30.9 KB
/
Props.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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
//-----------------------------------------------------------------------
// <copyright file="Props.cs" company="Akka.NET Project">
// Copyright (C) 2009-2018 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2018 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Akka.Dispatch;
using Akka.Util.Internal;
using Akka.Util.Reflection;
using Akka.Routing;
using Akka.Util;
using Newtonsoft.Json;
namespace Akka.Actor
{
/// <summary>
/// This class represents a configuration object used in creating an <see cref="Akka.Actor.ActorBase">actor</see>.
/// It is immutable and thus thread-safe.
/// <example>
/// <code>
/// private Props props = Props.Empty();
/// private Props props = Props.Create(() => new MyActor(arg1, arg2));
///
/// private Props otherProps = props.WithDispatcher("dispatcher-id");
/// private Props otherProps = props.WithDeploy(deployment info);
/// </code>
/// </example>
/// </summary>
public class Props : IEquatable<Props> , ISurrogated
{
private const string NullActorTypeExceptionText = "Props must be instantiated with an actor type.";
/// <summary>
/// This class represents a surrogate of a <see cref="Props"/> configuration object.
/// Its main use is to help during the serialization process.
/// </summary>
public class PropsSurrogate : ISurrogate
{
/// <summary>
/// The type of actor to create
/// </summary>
public Type Type { get; set; }
/// <summary>
/// The configuration used to deploy the actor.
/// </summary>
public Deploy Deploy { get; set; }
/// <summary>
/// The arguments used to create the actor.
/// </summary>
public object[] Arguments { get; set; }
/// <summary>
/// Creates a <see cref="Props"/> encapsulated by this surrogate.
/// </summary>
/// <param name="system">The actor system that owns this router.</param>
/// <returns>The <see cref="Props"/> encapsulated by this surrogate.</returns>
public ISurrogated FromSurrogate(ActorSystem system)
{
return new Props(Deploy, Type, Arguments);
}
}
/// <summary>
/// Creates a surrogate representation of the current <see cref="Props"/>.
/// </summary>
/// <param name="system">The actor system that owns this router.</param>
/// <returns>The surrogate representation of the current <see cref="Props"/>.</returns>
public ISurrogate ToSurrogate(ActorSystem system)
{
return new PropsSurrogate()
{
Arguments = Arguments,
Type = Type,
Deploy = Deploy,
};
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// <c>true</c> if the current object is equal to the <paramref name="other" /> parameter; otherwise, <c>false</c>.
/// </returns>
public bool Equals(Props other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return CompareDeploy(other) && CompareSupervisorStrategy(other) && CompareArguments(other) && CompareInputType(other);
}
private bool CompareInputType(Props other)
{
return inputType == other.inputType;
}
private bool CompareDeploy(Props other)
{
return Deploy.Equals(other.Deploy);
}
private bool CompareSupervisorStrategy(Props other)
{
return true; //TODO: fix https://github.com/akkadotnet/akka.net/issues/599
return Equals(SupervisorStrategy, other.SupervisorStrategy);
}
private bool CompareArguments(Props other)
{
if (other == null)
return false;
if (Arguments == null && other.Arguments == null)
return true;
if (Arguments == null)
return false;
if (Arguments.Length != other.Arguments.Length)
return false;
//TODO: since arguments can be serialized, we can not compare by ref
//arguments may also not implement equality operators, so we can not structurally compare either
//we can not just call a serializer and compare outputs either, since different args may require diff serializer mechanics
return true;
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Props) obj);
}
/// <inheritdoc/>
public override int GetHashCode()
{
unchecked
{
int hashCode = (Deploy != null ? Deploy.GetHashCode() : 0);
// hashCode = (hashCode*397) ^ (SupervisorStrategy != null ? SupervisorStrategy.GetHashCode() : 0);
// hashCode = (hashCode*397) ^ (Arguments != null ? Arguments.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (inputType != null ? inputType.GetHashCode() : 0);
return hashCode;
}
}
private static readonly Deploy defaultDeploy = new Deploy();
private static readonly Object[] noArgs = { };
private static readonly Props empty = Props.Create<EmptyActor>();
/// <summary>
/// A pre-configured <see cref="Akka.Actor.Props"/> that doesn't create actors.
///
/// <note>
/// The value of this field is null.
/// </note>
/// </summary>
public static readonly Props None = null;
private static readonly IIndirectActorProducer defaultProducer = new DefaultProducer();
private Type inputType;
private Type outputType;
private IIndirectActorProducer producer;
/// <summary>
/// Initializes a new instance of the <see cref="Props"/> class.
/// </summary>
protected Props()
: this(defaultDeploy, null, noArgs)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Props" /> class.
/// </summary>
/// <param name="copy">The object that is being cloned.</param>
protected Props(Props copy)
: this(copy.Deploy, copy.inputType, copy.SupervisorStrategy, copy.Arguments)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Props" /> class.
///
/// <note>
/// <see cref="Props"/> configured in this way uses the <see cref="Akka.Actor.Deploy"/> deployer.
/// </note>
/// </summary>
/// <param name="type">The type of the actor to create.</param>
/// <param name="args">The arguments needed to create the actor.</param>
/// <exception cref="ArgumentNullException">
/// This exception is thrown if <see cref="Props"/> is not instantiated with an actor type.
/// </exception>
public Props(Type type, object[] args)
: this(defaultDeploy, type, args)
{
if (type == null)
throw new ArgumentNullException(nameof(type), NullActorTypeExceptionText);
}
/// <summary>
/// Initializes a new instance of the <see cref="Props" /> class.
///
/// <note>
/// <see cref="Props"/> configured in this way uses the <see cref="Akka.Actor.Deploy"/> deployer.
/// </note>
/// </summary>
/// <param name="type">The type of the actor to create.</param>
/// <exception cref="ArgumentNullException">
/// This exception is thrown if <see cref="Props"/> is not instantiated with an actor type.
/// </exception>
public Props(Type type)
: this(defaultDeploy, type, noArgs)
{
if (type == null)
throw new ArgumentNullException(nameof(type), NullActorTypeExceptionText);
}
/// <summary>
/// Initializes a new instance of the <see cref="Props" /> class.
/// </summary>
/// <param name="type">The type of the actor to create.</param>
/// <param name="supervisorStrategy">The supervisor strategy used to manage the actor.</param>
/// <param name="args">The arguments needed to create the actor.</param>
/// <exception cref="ArgumentNullException">
/// This exception is thrown if <see cref="Props"/> is not instantiated with an actor type.
/// </exception>
public Props(Type type, SupervisorStrategy supervisorStrategy, IEnumerable<object> args)
: this(defaultDeploy, type, args.ToArray())
{
if (type == null)
throw new ArgumentNullException(nameof(type), NullActorTypeExceptionText);
SupervisorStrategy = supervisorStrategy;
}
/// <summary>
/// Initializes a new instance of the <see cref="Props" /> class.
/// </summary>
/// <param name="type">The type of the actor to create.</param>
/// <param name="supervisorStrategy">The supervisor strategy used to manage the actor.</param>
/// <param name="args">The arguments needed to create the actor.</param>
/// <exception cref="ArgumentNullException">
/// This exception is thrown if <see cref="Props"/> is not instantiated with an actor type.
/// </exception>
public Props(Type type, SupervisorStrategy supervisorStrategy, params object[] args)
: this(defaultDeploy, type, args)
{
if (type == null)
throw new ArgumentNullException(nameof(type), NullActorTypeExceptionText);
SupervisorStrategy = supervisorStrategy;
}
/// <summary>
/// Initializes a new instance of the <see cref="Props" /> class.
/// </summary>
/// <param name="deploy">The configuration used to deploy the actor.</param>
/// <param name="type">The type of the actor to create.</param>
/// <param name="args">The arguments needed to create the actor.</param>
/// <exception cref="ArgumentNullException">
/// This exception is thrown if <see cref="Props"/> is not instantiated with an actor type.
/// </exception>
public Props(Deploy deploy, Type type, IEnumerable<object> args)
: this(deploy, type, args.ToArray())
{
if (type == null)
throw new ArgumentNullException(nameof(type), NullActorTypeExceptionText);
}
/// <summary>
/// Initializes a new instance of the <see cref="Props" /> class.
/// </summary>
/// <param name="deploy">The configuration used to deploy the actor.</param>
/// <param name="type">The type of the actor to create.</param>
/// <param name="args">The arguments needed to create the actor.</param>
/// <exception cref="ArgumentException">This exception is thrown if <paramref name="type"/> is an unknown actor producer.</exception>
public Props(Deploy deploy, Type type, params object[] args)
{
Deploy = deploy;
inputType = type;
Arguments = args;
producer = CreateProducer(inputType, Arguments);
}
/// <summary>
/// The type of the actor that is created.
/// </summary>
[JsonIgnore]
public Type Type
{
get
{
if (outputType == null) {
outputType = producer.ActorType;
}
return outputType;
}
}
/// <summary>
/// The dispatcher used in the deployment of the actor.
/// </summary>
[JsonIgnore]
public string Dispatcher
{
get
{
var dispatcher = Deploy.Dispatcher;
return dispatcher == Deploy.NoDispatcherGiven ? Dispatchers.DefaultDispatcherId : dispatcher;
}
}
/// <summary>
/// The mailbox used in the deployment of the actor.
/// </summary>
[JsonIgnore]
public string Mailbox
{
get
{
return Deploy.Mailbox;
}
}
/// <summary>
/// The assembly qualified name of the type of the actor that is created.
/// </summary>
public string TypeName
{
get { return inputType.AssemblyQualifiedName; }
//for serialization
private set { inputType = Type.GetType(value); }
}
/// <summary>
/// The router used in the deployment of the actor.
/// </summary>
[JsonIgnore]
public RouterConfig RouterConfig
{
get { return Deploy.RouterConfig; }
}
/// <summary>
/// The configuration used to deploy the actor.
/// </summary>
public Deploy Deploy { get; protected set; }
/// <summary>
/// The supervisor strategy used to manage the actor.
/// </summary>
public SupervisorStrategy SupervisorStrategy { get; protected set; }
/// <summary>
/// A pre-configured <see cref="Akka.Actor.Props"/> that creates an actor that doesn't respond to messages.
/// </summary>
public static Props Empty
{
get { return empty; }
}
/// <summary>
/// The arguments needed to create the actor.
/// </summary>
public object[] Arguments { get; private set; }
/// <summary>
/// Creates an actor using a specified lambda expression.
/// </summary>
/// <typeparam name="TActor">The type of the actor to create.</typeparam>
/// <param name="factory">The lambda expression used to create the actor.</param>
/// <param name="supervisorStrategy">Optional: The supervisor strategy used to manage the actor.</param>
/// <returns>The newly created <see cref="Akka.Actor.Props" />.</returns>
/// <exception cref="ArgumentException">The create function must be a 'new T (args)' expression</exception>
public static Props Create<TActor>(Expression<Func<TActor>> factory, SupervisorStrategy supervisorStrategy=null) where TActor : ActorBase
{
if (factory.Body is UnaryExpression)
return new DynamicProps<TActor>(factory.Compile());
var newExpression = factory.Body.AsInstanceOf<NewExpression>();
if (newExpression == null)
throw new ArgumentException("The create function must be a 'new T (args)' expression");
object[] args = newExpression.GetArguments().ToArray();
return new Props(typeof (TActor), supervisorStrategy, args);
}
/// <summary>
/// Creates an actor using the given arguments.
/// </summary>
/// <typeparam name="TActor">The type of the actor to create.</typeparam>
/// <param name="args">The arguments needed to create the actor.</param>
/// <returns>The newly created <see cref="Akka.Actor.Props" />.</returns>
public static Props Create<TActor>(params object[] args) where TActor : ActorBase
{
return new Props(typeof(TActor), args);
}
/// <summary>
/// Creates an actor using a specified actor producer.
/// </summary>
/// <typeparam name="TProducer">The type of producer used to create the actor.</typeparam>
/// <param name="args">The arguments needed to create the actor.</param>
/// <returns>The newly created <see cref="Akka.Actor.Props" />.</returns>
public static Props CreateBy<TProducer>(params object[] args) where TProducer : class, IIndirectActorProducer
{
return new Props(typeof(TProducer), args);
}
/// <summary>
/// Creates an actor using a specified supervisor strategy.
/// </summary>
/// <typeparam name="TActor">The type of the actor to create.</typeparam>
/// <param name="supervisorStrategy">The supervisor strategy used to manage the actor.</param>
/// <returns>The newly created <see cref="Akka.Actor.Props" />.</returns>
public static Props Create<TActor>(SupervisorStrategy supervisorStrategy) where TActor : ActorBase, new()
{
return new Props(typeof(TActor), supervisorStrategy);
}
/// <summary>
/// Creates an actor of a specified type.
/// </summary>
/// <param name="type">The type of the actor to create.</param>
/// <param name="args">The arguments needed to create the actor.</param>
/// <returns>The newly created <see cref="Akka.Actor.Props" />.</returns>
/// <exception cref="ArgumentNullException">Props must be instantiated with an actor type.</exception>
public static Props Create(Type type, params object[] args)
{
if (type == null)
throw new ArgumentNullException(nameof(type), NullActorTypeExceptionText);
return new Props(type, args);
}
/// <summary>
/// Creates a new <see cref="Akka.Actor.Props" /> with a given <paramref name="mailbox" />.
///
/// <note>
/// This method is immutable and returns a new instance of <see cref="Akka.Actor.Props" />.
/// </note>
/// </summary>
/// <param name="mailbox">The mailbox used when deploying the actor.</param>
/// <returns>A new <see cref="Akka.Actor.Props" /> with the provided <paramref name="mailbox" />.</returns>
public Props WithMailbox(string mailbox)
{
Props copy = Copy();
copy.Deploy = Deploy.WithMailbox(mailbox);
return copy;
}
/// <summary>
/// Creates a new <see cref="Akka.Actor.Props" /> with a given <paramref name="dispatcher" />.
///
/// <note>
/// This method is immutable and returns a new instance of <see cref="Akka.Actor.Props" />.
/// </note>
/// </summary>
/// <param name="dispatcher">The dispatcher used when deploying the actor.</param>
/// <returns>A new <see cref="Akka.Actor.Props" /> with the provided <paramref name="dispatcher" />.</returns>
public Props WithDispatcher(string dispatcher)
{
Props copy = Copy();
copy.Deploy = Deploy.WithDispatcher(dispatcher);
return copy;
}
/// <summary>
/// Creates a new <see cref="Akka.Actor.Props" /> with a given router.
///
/// <note>
/// This method is immutable and returns a new instance of <see cref="Akka.Actor.Props" />.
/// </note>
/// </summary>
/// <param name="routerConfig">The router used when deploying the actor.</param>
/// <returns>A new <see cref="Akka.Actor.Props" /> with the provided <paramref name="routerConfig" />.</returns>
public Props WithRouter(RouterConfig routerConfig)
{
Props copy = Copy();
copy.Deploy = Deploy.WithRouterConfig(routerConfig);
return copy;
}
/// <summary>
/// Creates a new <see cref="Akka.Actor.Props" /> with a given deployment configuration.
///
/// <note>
/// This method is immutable and returns a new instance of <see cref="Akka.Actor.Props" />.
/// </note>
/// </summary>
/// <param name="deploy">The configuration used to deploy the actor.</param>
/// <returns>A new <see cref="Akka.Actor.Props" /> with the provided <paramref name="deploy" />.</returns>
public Props WithDeploy(Deploy deploy)
{
Props copy = Copy();
var original = copy.Deploy;
// TODO: this is a hack designed to preserve explicit router deployments https://github.com/akkadotnet/akka.net/issues/546
// in reality, we should be able to do copy.Deploy = deploy.WithFallback(copy.Deploy); but that blows up at the moment
// - Aaron Stannard
copy.Deploy = deploy.WithFallback(copy.Deploy);
//if (!(original.RouterConfig is NoRouter || original.RouterConfig is FromConfig) && deploy.RouterConfig is NoRouter)
//{
// copy.Deploy = deploy.WithFallback(copy.Deploy);
// copy.Deploy = deploy.WithRouterConfig(original.RouterConfig);
//}
////both configs describe valid, programmatically defined routers (usually clustered routers)
//else if (!(original.RouterConfig is NoRouter || original.RouterConfig is FromConfig) &&
// !(deploy.RouterConfig is FromConfig))
//{
// var deployedRouter = deploy.RouterConfig.WithFallback(original.RouterConfig);
// copy.Deploy = copy.Deploy.WithRouterConfig(deployedRouter);
//}
//else
//{
// copy.Deploy = deploy;
//}
return copy;
}
/// <summary>
/// Creates a new <see cref="Akka.Actor.Props" /> with a given supervisor strategy.
///
/// <note>
/// This method is immutable and returns a new instance of <see cref="Akka.Actor.Props" />.
/// </note>
/// </summary>
/// <param name="supervisorStrategy">The supervisor strategy used to manage the actor.</param>
/// <returns>A new <see cref="Akka.Actor.Props" /> with the provided <paramref name="supervisorStrategy" />.</returns>
public Props WithSupervisorStrategy(SupervisorStrategy supervisorStrategy)
{
Props copy = Copy();
copy.SupervisorStrategy = supervisorStrategy;
return copy;
}
//TODO: use Linq Expressions so compile a creator
//cache the creator
/// <summary>
/// Creates a new actor using the configured actor producer.
///
/// <remarks>
/// This method is only useful when called during actor creation by the ActorSystem.
/// </remarks>
/// </summary>
/// <exception cref="TypeLoadException">
/// This exception is thrown if there was an error creating an actor of type <see cref="Props.Type"/>
/// with the arguments from <see cref="Props.Arguments"/>.
/// </exception>
/// <returns>The newly created actor</returns>
public virtual ActorBase NewActor()
{
var type = Type;
var arguments = Arguments;
try {
return producer.Produce();
} catch (Exception e) {
throw new TypeLoadException($"Error while creating actor instance of type {type} with {arguments.Length} args: ({StringFormat.SafeJoin(",", arguments)})", e);
}
}
/// <summary>
/// Creates a copy of the current instance.
/// </summary>
/// <returns>The newly created <see cref="Akka.Actor.Props"/></returns>
protected virtual Props Copy()
{
return new Props(Deploy, inputType, Arguments) { SupervisorStrategy = SupervisorStrategy };
}
#region INTERNAL API
/// <summary>
/// This class represents a specialized <see cref="UntypedActor" /> that doesn't respond to messages.
/// </summary>
internal class EmptyActor : UntypedActor
{
/// <summary>
/// Handles messages received by the actor.
/// </summary>
/// <param name="message">The message past to the actor.</param>
protected override void OnReceive(object message)
{
}
}
private class DefaultProducer : IIndirectActorProducer
{
public ActorBase Produce()
{
throw new InvalidOperationException("No actor producer specified!");
}
public Type ActorType
{
get { return typeof(ActorBase); }
}
public void Release(ActorBase actor)
{
actor = null;
}
}
private class ActivatorProducer : IIndirectActorProducer
{
private readonly Type _actorType;
private readonly object[] _args;
public ActivatorProducer(Type actorType, object[] args)
{
_actorType = actorType;
_args = args;
}
public ActorBase Produce()
{
return Activator.CreateInstance(_actorType, _args).AsInstanceOf<ActorBase>();
}
public Type ActorType
{
get { return _actorType; }
}
public void Release(ActorBase actor)
{
actor = null;
}
}
private class FactoryConsumer<TActor> : IIndirectActorProducer where TActor : ActorBase
{
private readonly Func<TActor> _factory;
public FactoryConsumer(Func<TActor> factory)
{
_factory = factory;
}
public ActorBase Produce()
{
return _factory.Invoke();
}
public Type ActorType
{
get { return typeof(TActor); }
}
public void Release(ActorBase actor)
{
actor = null;
}
}
#endregion
private static IIndirectActorProducer CreateProducer(Type type, object[] args)
{
if (type == null) {
return defaultProducer;
}
if (typeof(IIndirectActorProducer).IsAssignableFrom(type)) {
return Activator.CreateInstance(type, args).AsInstanceOf<IIndirectActorProducer>();
}
if (typeof(ActorBase).IsAssignableFrom(type)) {
return new ActivatorProducer(type, args);
}
throw new ArgumentException($"Unknown actor producer [{type.FullName}]", nameof(type));
}
/// <summary>
/// Signals the producer that it can release its reference to the actor.
/// </summary>
/// <param name="actor">The actor to release</param>
internal void Release(ActorBase actor)
{
try
{
if (this.producer != null) this.producer.Release(actor);
}
finally
{
actor = null;
}
}
}
/// <summary>
/// This class represents a specialized <see cref="Akka.Actor.Props"/> used when the actor has been terminated.
/// </summary>
public class TerminatedProps : Props
{
/// <summary>
/// N/A
/// </summary>
/// <exception cref="InvalidOperationException">This exception is thrown automatically since the actor has been terminated.</exception>
/// <returns>N/A</returns>
public override ActorBase NewActor()
{
throw new InvalidOperationException("This actor has been terminated");
}
}
/// <summary>
/// This class represents a specialized <see cref="Akka.Actor.Props"/> that uses dynamic invocation
/// to create new actor instances, rather than a traditional <see cref="System.Activator"/>.
///
/// <note>
/// This is intended to be used in conjunction with Dependency Injection.
/// </note>
/// </summary>
/// <typeparam name="TActor">The type of the actor to create.</typeparam>
internal class DynamicProps<TActor> : Props where TActor : ActorBase
{
private readonly Func<TActor> invoker;
/// <summary>
/// Initializes a new instance of the <see cref="DynamicProps{TActor}" /> class.
/// </summary>
/// <param name="invoker">The factory method used to create an actor.</param>
public DynamicProps(Func<TActor> invoker)
: base(typeof(TActor))
{
this.invoker = invoker;
}
/// <summary>
/// Creates a new actor using the configured factory method.
/// </summary>
/// <returns>The actor created using the factory method.</returns>
public override ActorBase NewActor()
{
return invoker.Invoke();
}
#region Copy methods
private DynamicProps(Props copy, Func<TActor> invoker)
: base(copy)
{
this.invoker = invoker;
}
/// <summary>
/// Creates a copy of the current instance.
/// </summary>
/// <returns>The newly created <see cref="Akka.Actor.Props"/></returns>
protected override Props Copy()
{
Props initialCopy = base.Copy();
#if CLONEABLE
var invokerCopy = (Func<TActor>)invoker.Clone();
#else
// TODO: CORECLR FIX IT
var invokerCopy = (Func<TActor>)invoker;
#endif
return new DynamicProps<TActor>(initialCopy, invokerCopy);
}
#endregion
}
/// <summary>
/// This interface defines a class of actor creation strategies deviating from
/// the usual default of just reflectively instantiating the <see cref="Akka.Actor.ActorBase">Actor</see>
/// subclass. It can be used to allow a dependency injection framework to
/// determine the actual actor class and how it shall be instantiated.
/// </summary>
public interface IIndirectActorProducer
{
/// <summary>
/// This factory method must produce a fresh actor instance upon each
/// invocation. It is not permitted to return the same instance more than
/// once.
/// </summary>
/// <returns>A fresh actor instance.</returns>
ActorBase Produce();
/// <summary>
/// This method is used by <see cref="Akka.Actor.Props"/> to determine the type of actor to create.
/// The returned type is not used to produce the actor.
/// </summary>
/// <returns>The type of the actor created.</returns>
Type ActorType { get; }
/// <summary>
/// This method is used by <see cref="Akka.Actor.Props"/> to signal the producer that it can
/// release it's reference.
///
/// <remarks>
/// To learn more about using Dependency Injection in .NET, see <see href="http://www.amazon.com/Dependency-Injection-NET-Mark-Seemann/dp/1935182501">HERE</see>.
/// </remarks>
/// </summary>
/// <param name="actor">The actor to release</param>
void Release(ActorBase actor);
}
}