diff options
author | Martin Odersky <odersky@gmail.com> | 2003-03-12 13:39:16 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2003-03-12 13:39:16 +0000 |
commit | 9ab44e5b8c934d5bed0efab7aebb10135d21ca30 (patch) | |
tree | cc310c66ed9e4a04dc4754247a651e99d18476f3 /sources/examples | |
parent | 974cf85afb2e033fc3aa52af1ab6ff174a35a169 (diff) | |
download | scala-9ab44e5b8c934d5bed0efab7aebb10135d21ca30.tar.gz scala-9ab44e5b8c934d5bed0efab7aebb10135d21ca30.tar.bz2 scala-9ab44e5b8c934d5bed0efab7aebb10135d21ca30.zip |
*** empty log message ***
Diffstat (limited to 'sources/examples')
-rw-r--r-- | sources/examples/auction.scala | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/sources/examples/auction.scala b/sources/examples/auction.scala new file mode 100644 index 0000000000..9f96dc2984 --- /dev/null +++ b/sources/examples/auction.scala @@ -0,0 +1,58 @@ +package examples; + +import java.util.Date; +import scala.concurrent._; + +trait AuctionMessage; +case class + Offer(bid: Int, client: Actor), // make a bid + Inquire(client: Actor) extends AuctionMessage; // inquire status + +trait AuctionReply; +case class + Status(asked: Int, expiration: Date), // asked sum, expiration date + BestOffer(), // yours is the best offer + BeatenOffer(maxBid: Int), // offer beaten by maxBid + AuctionConcluded(seller: Actor, client: Actor), // auction concluded + AuctionFailed(), // failed with no bids + AuctionOver() extends AuctionReply; // bidding is closed + +class Auction(seller: Actor, minBid: Int, closing: Date) extends Actor() { + + val timeToShutdown = 36000000; // msec + val bidIncrement = 10; + override def run() { + var maxBid = minBid - bidIncrement; + var maxBidder: Actor = _; + var running = true; + while (running) { + receiveWithin ((closing.getTime() - new Date().getTime())) { + case Offer(bid, client) => + if (bid >= maxBid + bidIncrement) { + if (maxBid >= minBid) maxBidder send BeatenOffer(bid); + maxBid = bid; + maxBidder = client; + client send BestOffer(); + } else { + client send BeatenOffer(maxBid); + } + + case Inquire(client) => + client send Status(maxBid, closing); + + case TIMEOUT() => + if (maxBid >= minBid) { + val reply = AuctionConcluded(seller, maxBidder); + maxBidder send reply; + seller send reply; + } else { + seller send AuctionFailed(); + } + receiveWithin(timeToShutdown) { + case Offer(_, client) => client send AuctionOver() + case TIMEOUT() => running = false; + } + } + } + } +} |