-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpawnerActor.cs
86 lines (76 loc) · 2.33 KB
/
SpawnerActor.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
using Akka.Actor;
using Akka.Event;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Actors
{
/// <summary>
/// Attore che gestisce lo spawn dei nodi e che applica
/// una politica di supervisione consona.
/// </summary>
public class SpawnerActor : ReceiveActor
{
private readonly ILoggingAdapter _logger = Context.GetLogger();
public SpawnerActor()
{
Receive<SpawnActorRequest>((msg) => OnReceive(msg));
Receive<SpawnActorTestMessage>((msg) => OnReceive(msg));
}
private void OnReceive(SpawnActorRequest msg)
{
try
{
IActorRef child = Context.ActorOf(msg.ActorProps, msg.ActorName);
Sender.Tell(child);
}
catch (Exception ex)
{
Sender.Tell(ex);
}
}
private void OnReceive(SpawnActorTestMessage msg)
{
Sender.Tell(true);
}
protected override SupervisorStrategy SupervisorStrategy()
{
return new OneForOneStrategy(
maxNrOfRetries: 0,
withinTimeRange: Timeout.InfiniteTimeSpan,
localOnlyDecider: ex =>
{
_logger.Error($"A drone mission failed due to exception: {ex}");
return Directive.Stop;
/*
switch (ex)
{
case ArithmeticException ae:
return Directive.Resume;
case NullReferenceException nre:
return Directive.Restart;
case ArgumentException are:
return Directive.Stop;
default:
return Directive.Escalate;
}
*/
});
}
}
public class SpawnActorRequest
{
public Props ActorProps { get; }
public string ActorName { get; }
public SpawnActorRequest(Props actorProps, string actorName)
{
ActorProps = actorProps;
ActorName = actorName;
}
}
public class SpawnActorTestMessage
{
}
}