Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Strong Type Message with ActorLite
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
}
}
#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