Created
June 24, 2015 12:43
-
-
Save GeertVL-zz/749f8c5de49bd2fb207b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Guest : UntypedActor | |
{ | |
private IActorRef _receptionist; | |
public Guest() | |
{ | |
var receptionist = Context.ActorSelection("/user/Klara"); | |
receptionist.Tell(new Identify("111"), Self); | |
} | |
protected override void OnReceive(object message) | |
{ | |
if (message is ActorIdentity) | |
{ | |
Console.WriteLine("Actor has an identity: " + message); | |
_receptionist = ((ActorIdentity)message).Subject; | |
Context.Watch(_receptionist); | |
} | |
else if (message.Equals("enter")) | |
{ | |
Console.WriteLine("At the reception : {0} ", Self.Path.Name); | |
var t = Task.Run(async () => | |
{ | |
var t1 = _receptionist.Ask<Badge>(new BadgeRequest { Name = Self.Path.Name, LicensePlate = "AAA111" }); | |
await Task.WhenAll(t1); | |
Console.WriteLine("Result of reception: " + t1.Result); | |
await Task.WhenAll(t1); | |
return t1.Result; | |
}); | |
t.PipeTo(_receptionist, Self); | |
//t.Wait(); | |
} | |
} | |
} | |
public class Receptionist : UntypedActor | |
{ | |
protected override void Unhandled(object message) | |
{ | |
Console.WriteLine("Message received " + message); | |
} | |
protected override void OnReceive(object message) | |
{ | |
Console.WriteLine("Message received " + message); | |
if (message is ActorIdentity) | |
{ | |
Console.WriteLine("Actor has an identity"); | |
} | |
if (message is BadgeRequest) | |
{ | |
Console.WriteLine("BadgeRequest from " + Sender.ToString()); | |
var badgeRequest = message as BadgeRequest; | |
var badge = new Badge { | |
Id = Guid.NewGuid(), | |
Name = badgeRequest.Name, | |
ValableOn = DateTime.Now, | |
DeliveredBy = Self.Path.Name | |
}; | |
Sender.Tell(badge); | |
} | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var system = ActorSystem.Create("MySystem"); | |
//var accountant = system.ActorOf<Employee>("Geert"); | |
//accountant.Tell(true); | |
//var plumber = system.ActorOf<Guest>("Verspecht"); | |
//plumber.Tell("FixPlumbing"); | |
var receptionist = system.ActorOf<Receptionist>("Klara"); | |
if (receptionist != null) { } | |
var plumber = system.ActorOf<Guest>("Verspecht"); | |
plumber.Tell("enter"); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment