-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAkkaActorHelloWorld.cs
109 lines (88 loc) · 3.21 KB
/
AkkaActorHelloWorld.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
using Akka.Actor;
using System;
using System.Linq;
// Hello world using Akka Actors
// Compiled: Visual Studio 2013
// Requires: Akka.NET Nuget package
namespace AkkaActorHelloWorld
{
public class AkkaActorHelloWorld
{
public class ProgramStarted
{
public string UserName { get; private set; }
public ProgramStarted(string username)
{
UserName = username;
}
}
public class SetOutput
{
public ActorRef Output { get; private set; }
public ConsoleColor Color { get; private set; }
public SetOutput(ActorRef output, ConsoleColor color)
{
Output = output;
Color = color;
}
}
public class HelloActor : ReceiveActor
{
private ConsoleColor _color = ConsoleColor.Red;
private ActorRef _output;
public HelloActor()
{
Receive<SetOutput>(x =>
{
_output = x.Output;
_color = x.Color;
});
Receive<ProgramStarted>(x => { _output.Tell(new WriteToConsole("Hello " + x.UserName, _color)); });
}
}
public class WriteToConsole
{
public ConsoleColor Color {get; private set; }
public string Message { get; private set; }
public WriteToConsole(string message, ConsoleColor color)
{
Color = color;
Message = message;
}
}
public class ConsoleActor : ReceiveActor
{
public ConsoleActor()
{
Receive<WriteToConsole>(x =>
{
Console.ForegroundColor = x.Color;
Console.WriteLine("Hello {0}", x.Message);
});
}
}
public static void Main()
{
var system = ActorSystem.Create("HelloWorldManager");
var consoleActor = system.ActorOf<ConsoleActor>("actor0");
var helloActor1 = system.ActorOf<HelloActor>("actor1");
helloActor1.Tell(new SetOutput(consoleActor, ConsoleColor.Blue));
var helloActor2 = system.ActorOf<HelloActor>("actor2");
helloActor2.Tell(new SetOutput(consoleActor, ConsoleColor.Green));
var helloActor3 = system.ActorOf<HelloActor>("actor3");
helloActor3.Tell(new SetOutput(consoleActor, ConsoleColor.Gray));
var helloActor4 = system.ActorOf<HelloActor>("actor4");
helloActor4.Tell(new SetOutput(consoleActor, ConsoleColor.White));
foreach (var i in Enumerable.Range(1, 100))
{
helloActor1.Tell(new ProgramStarted("Blue World"));
helloActor2.Tell(new ProgramStarted("Green User"));
helloActor3.Tell(new ProgramStarted("Gray Dog"));
helloActor4.Tell(new ProgramStarted("White Cat"));
}
Console.WriteLine("Press Enter to exit.");
Console.ReadKey(true);
Console.ReadKey(true);
}
}
}