Created
July 16, 2009 15:10
-
-
Save JeffreyZhao/148464 to your computer and use it in GitHub Desktop.
Strong Type Message with ActorLite
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
namespace Strong | |
{ | |
using System; | |
using ActorLite; | |
static class Program | |
{ | |
static void Main(string[] args) | |
{ | |
new Ping(5).Start(new Pong()); | |
Console.ReadLine(); | |
} | |
} | |
public interface IPongMessageHandler | |
{ | |
void Pong(Pong pong); | |
} | |
public interface IPingMessageHandler | |
{ | |
void Ping(Ping ping); | |
void Finish(); | |
} | |
public class Ping : Actor<Action<IPongMessageHandler>>, IPongMessageHandler | |
{ | |
private int m_count; | |
public Ping(int count) | |
{ | |
this.m_count = count; | |
} | |
protected override void Receive(Action<IPongMessageHandler> message) | |
{ | |
message(this); | |
} | |
public void Start(Pong pong) | |
{ | |
pong.Post(h => h.Ping(this)); | |
} | |
#region IPongMessageHandler Members | |
void IPongMessageHandler.Pong(Pong pong) | |
{ | |
Console.WriteLine("Ping received pong"); | |
if (--this.m_count > 0) | |
{ | |
pong.Post(h => h.Ping(this)); | |
} | |
else | |
{ | |
pong.Post(h => h.Finish()); | |
this.Exit(); | |
} | |
} | |
#endregion | |
} | |
public class Pong : Actor<Action<IPingMessageHandler>>, IPingMessageHandler | |
{ | |
protected override void Receive(Action<IPingMessageHandler> message) | |
{ | |
message(this); | |
} | |
#region IPingMessageHandler Members | |
void IPingMessageHandler.Ping(Ping ping) | |
{ | |
Console.WriteLine("Pong received ping"); | |
ping.Post(h => h.Pong(this)); | |
} | |
void IPingMessageHandler.Finish() | |
{ | |
Console.WriteLine("Finished"); | |
this.Exit(); | |
} | |
#endregion | |
} | |
} |
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
#light | |
open ActorLite | |
open System | |
let (<<) (a:_ Actor) msg = a.Post msg | |
type IPingMessageHandler = | |
abstract Ping : Ping -> unit | |
abstract Finish : unit -> unit | |
and IPongMessageHandler = | |
abstract Pong : Pong -> unit | |
and Ping() = | |
inherit (IPongMessageHandler -> unit) Actor() | |
let mutable count = 5 | |
override self.Receive(message) = message self | |
member self.Start(pong : Pong) = | |
pong << fun h -> self |> h.Ping | |
interface IPongMessageHandler with | |
member self.Pong(pong) = | |
printfn "Ping received pong" | |
count <- count - 1 | |
if (count > 0) then | |
pong << fun h -> self |> h.Ping | |
else | |
pong << fun h -> h.Finish() | |
self.Exit() | |
and Pong() = | |
inherit (IPingMessageHandler -> unit) Actor() | |
override self.Receive(message) = message self | |
interface IPingMessageHandler with | |
member self.Ping(ping) = | |
printfn "Pong received ping" | |
ping << fun h -> self |> h.Pong | |
member self.Finish() = | |
printfn "Finished" | |
self.Exit() | |
(new Pong()) |> (new Ping()).Start; | |
Console.ReadLine() |> ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment