-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (82 loc) · 2.1 KB
/
Program.cs
File metadata and controls
101 lines (82 loc) · 2.1 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Akka.Actor;
namespace CalculatorApp
{
class Program
{
static void Main(string[] args)
{
var system = ActorSystem.Create("calculator-system");
var calculator = system.ActorOf<CalculatorActor>("calculator");
var answer = calculator.Ask<Answer>(new Add(1, 2)).Result;
Console.WriteLine("1 + 2 = " + answer.Value);
var answerSubtract = calculator.Ask<Answer>(new Subtract(5, 3)).Result;
Console.WriteLine("5 - 3 = " + answerSubtract.Value);
var lastAnswer = calculator.Ask<Answer>(GetLastAnswer.Instance).Result;
Console.WriteLine("Last answer = " + lastAnswer.Value);
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
public class CalculatorActor : ReceiveActor
{
public CalculatorActor()
{
var answer = 0d;
Receive<Add>(add =>
{
answer = add.Term1 + add.Term2;
Sender.Tell(new Answer(answer));
});
Receive<Subtract>(sub =>
{
answer = sub.Term1 - sub.Term2;
Sender.Tell(new Answer(answer));
});
Receive<GetLastAnswer>(m => Sender.Tell(new Answer(answer)));
}
}
public class Add
{
private readonly double _term1;
private readonly double _term2;
public Add(double term1, double term2)
{
_term1 = term1;
_term2 = term2;
}
public double Term1 { get { return _term1; } }
public double Term2 { get { return _term2; } }
}
public class Subtract
{
private readonly double _term1;
private readonly double _term2;
public Subtract(double term1, double term2)
{
_term1 = term1;
_term2 = term2;
}
public double Term1 { get { return _term1; } }
public double Term2 { get { return _term2; } }
}
public class Answer
{
private readonly double _value;
public Answer(double value)
{
_value = value;
}
public double Value { get { return _value; } }
}
public class GetLastAnswer
{
private static readonly GetLastAnswer _instance = new GetLastAnswer();
private GetLastAnswer() { }
public static GetLastAnswer Instance { get { return _instance; } }
}
}