Skip to content

Instantly share code, notes, and snippets.

@ohsitab
Created August 27, 2010 22:25
Show Gist options
  • Save ohsitab/554301 to your computer and use it in GitHub Desktop.
Save ohsitab/554301 to your computer and use it in GitHub Desktop.
/*
Scala Exsample Chapter 3
http://www29.atwiki.jp/tmiya/pages/26.html
Scala2.8で、オークション終了後、シャットダウンするまでの間にOfferメッセージを
受け取ったときの問題を修正したもの。
*/
class Auction(seller: Actor, minBid: Int, closing: Date) extends Actor {
val timeToShutdown = 36000000 // msec
val bidIncrement = 10
def act() {
var maxBid = minBid - bidIncrement
var maxBidder: Actor = null
loop {
receiveWithin ((closing.getTime() - new Date().getTime())) {
case Offer(bid, client) =>
if (bid >= maxBid + bidIncrement) {
if (maxBid >= minBid) maxBidder ! BeatenOffer(bid)
maxBid = bid; maxBidder = client; client ! BestOffer
} else {
client ! BeatenOffer(maxBid)
}
case Inquire(client) =>
client ! Status(maxBid, closing)
case TIMEOUT =>
if (maxBid >= minBid) {
val reply = AuctionConcluded(seller, maxBidder)
maxBidder ! reply; seller ! reply
} else {
seller ! AuctionFailed
}
loop {
receiveWithin(timeToShutdown) {
case Offer(_, client) => client ! AuctionOver
case TIMEOUT => actors.Actor.exit
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment