技术开发 频道

天下无处不乒乓

 Axum with Channel

  using System;

 using System.Concurrency;

 using System.Collections.Generic;

 using Microsoft.Axum;

 using System.Concurrency.Messaging;

 public channel PingPongStatus

 {

 input int HowMany;

 output int Done;

 Start: { HowMany, Done -> End; }

 }

 public agent Ping : channel PingPongStatus

 {

 public Ping()

 {

 // How many are we supposed to do?

 var iters = receive(PrimaryChannel::HowMany);

 // Create pong and send how many to do

 var chan = Pong.CreateInNewDomain();

 chan::HowMany <-- iters;

 // Send pings and receive pongs

 for (int i = 0; i < iters; i++)

 {

 chan::Ping <-- Signal.Value;

 receive(chan::Pong);

 Console.WriteLine("Ping received Pong");

 }

 // How many did Pong process?

 int pongIters = receive(chan::Done);

 PrimaryChannel::Done <-- 0;

 }

 }

 public channel PingPong

 {

 input int HowMany;

 output int Done;

 input Signal Ping;

 output Signal Pong;

 }

 public agent Pong : channel PingPong

 {

 public Pong()

 {

 // Get how many we're supposed to do

 var iters = receive(PrimaryChannel::HowMany);

 int i = 0;

 // Receive our ping and send back our pong

 for (; i < iters; i++)

 {

 receive(PrimaryChannel::Ping);

 Console.WriteLine("Pong received Ping");

 PrimaryChannel::Pong <-- Signal.Value;

 }

 PrimaryChannel::Done <-- i;

 }

 }

 public agent Program : channel Application

 {

 public Program()

 {

 // Wait for our command args

 var cmdArgs = receive(PrimaryChannel::CommandLine);

 // Set the iterations to be some number

 var iters = 3;

 // Create instance of ping and send msg

 var chan = Ping.CreateInNewDomain();

 chan::HowMany <-- iters;

 // Receive how many done

 receive(chan::Done);

 PrimaryChannel::Done <-- Signal.Value;

 }

 }

 F#

 open System

 type message = Finished | Msg of int * MailboxProcessor<message>

 let ping iters (outbox : MailboxProcessor<message>) =

 MailboxProcessor.Start(fun inbox ->

 let rec loop n = async {

 if n > 0 then

 outbox.Post( Msg(n, inbox) )

 let! msg = inbox.Receive()

 Console.WriteLine("ping received pong")

 return! loop(n-1)

 else

 outbox.Post(Finished)

 Console.WriteLine("ping finished")

 return ()}

 loop iters)

 let pong () =

 MailboxProcessor.Start(fun inbox ->

 let rec loop () = async {

 let! msg = inbox.Receive()

 match msg with

 | Finished ->

 Console.WriteLine("pong finished")

 return ()

 | Msg(n, outbox) ->

 Console.WriteLine("pong received ping")

 outbox.Post(Msg(n, inbox)

 return! loop() }

 loop())

 let ponger = pong()

 do (ping 10 ponger) |> ignore

 F# with ActorLite

 #light

 open System

 open ActorLite

 let (<=) (m:_ Actor) msg = m.Post msg

 type message = string * message Actor

 let pong =

 { new message Actor() with

 override self.Receive(message) =

 let msg, ping = message

 Console.WriteLine(msg)

 ping <= ("Pong", self) }

 let ping =

 { new message Actor() with

 override self.Receive(message) =

 let msg, pong = message

 Console.WriteLine(msg)

 pong <= ("Ping", self) }

 ping <= ("Start", pong)

 Console.ReadLine |> ignore

原文地址:http://www.cnblogs.com/jeffreyzhao/archive/2009/06/24/everything-ping-pong.html

0
相关文章