Skip to content

Commit

Permalink
Adding MasterSearchActor and TimeMesuare
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Nov 15, 2014
1 parent 7e04754 commit d794a03
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 9 deletions.
Binary file modified .cache
Binary file not shown.
2 changes: 2 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="C:/Users/kalap/Documents/reactive-scala1 (1)/reactive-scala1/jar/com.typesafe.akka/akka-actor_2.10/jars/akka-actor_2.10-2.3.6.jar"/>
<classpathentry kind="lib" path="C:/Users/kalap/Documents/reactive-scala1 (1)/reactive-scala1/jar/com.typesafe/config/bundles/config-1.2.1.jar"/>
<classpathentry kind="lib" path="C:/Users/kalap/Downloads/scalatest_2.10-2.1.3.jar"/>
<classpathentry kind="lib" path="C:/Users/kalap/Downloads/akka-testkit_2.10-2.3.6.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
6 changes: 6 additions & 0 deletions bin/auction/helpers/GlobalState
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package auction.helpers

object RegisteredAuctions {
private var registerdAuctions: Map[Integer, SystemSettings.AuctionPrice] = Map()

}
84 changes: 84 additions & 0 deletions results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
1416073085911
1416073085911
1416073085911
1416073085911
1416073085911
1416073085911
1416073085911
1416073085911
1416073085911
1416073085911
1416073085921
1416073085921
1416073085921
1416073085921
1416073085921
1416073085921
1416073085921
1416073085921
1416073085931
1416073085931
1416073085931
1416073085931
1416073085931
1416073085931
1416073085931
1416073085931
1416073085931
1416073085941
1416073085941
1416073085941
1416073085941
1416073085941
1416073085941
1416073085941
1416073085941
1416073085941
1416073085941
1416073085941
1416073085951
1416073085951
1416073085952
1416073085952
1416073085953
1416073085955
1416073085955
1416073085956
1416073085957
1416073085958
1416073085959
1416073085959
1416073085959
1416073085960
1416073085961
1416073085961
1416073085962
1416073085963
1416073085963
1416073085964
1416073085964
1416073085964
1416073085964
1416073085966
1416073085967
1416073085967
1416073085968
1416073085973
1416073085973
1416073085973
1416073085973
1416073085973
1416073085973
1416073085973
1416073085973
1416073085973
1416073085973
1416073085973
1416073085973
1416073085983
1416073085983
1416073085983
1416073085983
1416073085983
1416073085983
1416073085993
17 changes: 13 additions & 4 deletions src/auction/actors/AuctionSearchActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,42 @@ import auction.helpers.registerAuction
import auction.helpers.AuctionSystemLogger
import auction.helpers.auctionRegistered
import auction.helpers.SystemSettings
import auction.helpers.GlobalState
import auction.helpers.TimeMeasure

class AuctionSearchActor extends Actor {

private var registerdAuctions: Map[Integer, SystemSettings.AuctionPrice] = Map()
private val AUCTION_REGISTER: String = " Auction Registered "
private val SEARCH_ACTOR: String = "Search Actor"
private val SEARCHING: String = "Searching auctions with keyword "
private val AUCTION_REGISTER: String = " Auction Registered "

def find(keyWord: String): List[SystemSettings.AuctionPrice] = {
AuctionSystemLogger.log(SEARCH_ACTOR, SEARCHING + keyWord)
val pattern = keyWord.r
var result: List[SystemSettings.AuctionPrice] = List()
SystemSettings.TITLES.
filter(x => x._2 contains keyWord).
foreach(x => result = result.+:(registerdAuctions(x._1)))
foreach(x => {
if (GlobalState.registerdAuctions.contains(x._1)) {
result = result.+:(GlobalState.registerdAuctions(x._1))
}
})
return result
}

def receive = {
case findAuction(keyWord) => {
val foundAuctions = find(keyWord)
val start: Long = System.currentTimeMillis();
sender ! sendFoundAuctions(foundAuctions)
val stop: Long = System.currentTimeMillis();
val result = start.toString()
TimeMeasure.addResult(result)
}
case registerAuction(auctionId, auction, price) => {
AuctionSystemLogger.log(SEARCH_ACTOR, AUCTION_REGISTER)
val auctionPrice = SystemSettings.AuctionPrice(price, auction)
registerdAuctions += auctionId -> auctionPrice
GlobalState.registerdAuctions += auctionId -> auctionPrice
}
}

Expand Down
45 changes: 45 additions & 0 deletions src/auction/actors/MasterSearchActor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package auction.actors

import akka.actor.Actor
import akka.actor.Props
import akka.actor.Terminated
import akka.routing.ActorRefRoutee
import akka.routing.RoundRobinRoutingLogic
import akka.routing.Router
import auction.helpers.AuctionSystemLogger
import auction.helpers.GlobalState
import auction.helpers.SystemSettings
import auction.helpers.findAuction
import auction.helpers.registerAuction
import akka.routing.BroadcastRoutingLogic

class MasterSearchActor extends Actor {

private val MASTER_SEARCH: String = "MasterSearch"

var router = {
val routees = Vector.fill(SystemSettings.NUMER_OF_SEARCH_ACTORS) {
val auctionSearch = context.actorOf(Props[AuctionSearchActor])
context watch auctionSearch
ActorRefRoutee(auctionSearch)
}
Router(RoundRobinRoutingLogic(), routees)
}

def receive = {
case w: findAuction => {
Router(BroadcastRoutingLogic(), router.routees)
router.route(w, sender())
}
case Terminated(actorRef) =>
router = router.removeRoutee(actorRef)
val r = context.actorOf(Props[AuctionSearchActor])
context watch r
router = router.addRoutee(r)
case w: registerAuction => {
Router(RoundRobinRoutingLogic(), router.routees)
router.route(w, sender())
}
}

}
3 changes: 3 additions & 0 deletions src/auction/helpers/AuctionManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import auction.actors.AuctionActor
import auction.actors.AuctionSearchActor
import auction.actors.BuyerActor
import auction.actors.SellerActor
import auction.actors.MasterSearchActor

class AuctionManager extends Actor with FSM[State, Data] {

Expand All @@ -31,6 +32,7 @@ class AuctionManager extends Actor with FSM[State, Data] {
private var auctionsFinished = 0

val auctionSearch = context.actorOf(Props[AuctionSearchActor], "auctionSearch")
val masterSearch = context.actorOf(Props[MasterSearchActor], "masterSearch")

private def initAuctions(numberOfAuctions: Int): List[ActorRef] = {
return (1 to numberOfAuctions).map(num => context.actorOf(Props[AuctionActor], "auction" + num)).toList
Expand Down Expand Up @@ -132,6 +134,7 @@ class AuctionManager extends Actor with FSM[State, Data] {
}
case Event(closeAuctionSystem(), AuctionSystemData(_, _, _)) => {
AuctionSystemLogger.log(AUCTION_SYSTEM, SYSTEM_CLOSED)
TimeMeasure.writeResultsToFile();
goto(AuctionSystemOff) using AuctionSystemData(Nil, Nil, Nil)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/auction/helpers/AuctionSystemLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object AuctionSystemLogger {
def addAuctionsResults(buyer: String, auctionId: Int) = {
resultsMap += auctionId -> buyer
}

def logResults() = {
println()
println("--------------------------------------------------------")
Expand Down
5 changes: 5 additions & 0 deletions src/auction/helpers/GlobalState.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package auction.helpers

object GlobalState {
var registerdAuctions: Map[Integer, SystemSettings.AuctionPrice] = Map()
}
1 change: 1 addition & 0 deletions src/auction/helpers/Messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ sealed trait TimerMessage
sealed trait SellerMessage
sealed trait SearchMessage
sealed trait NotifyMessage
sealed trait MasterSearchMessage

case class startAuctionSystem() extends AuctionManagamentMessage
case class closeAuctionSystem() extends AuctionManagamentMessage
Expand Down
10 changes: 6 additions & 4 deletions src/auction/helpers/SystemSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ object SystemSettings {
val TIMERS_BOTTOM_TIME: Int = 10
val TIMERS_TOP_TIME: Int = 15
val AUCTION_BOTTOM_PRICE: Int = 10
val AUCTION_TOP_PRICE: Int = 100
val NUMBER_OF_AUCTIONS = 8;
val NUMBER_OF_BUYERS = 15;
val AUCTION_TOP_PRICE: Int = 200
val NUMBER_OF_AUCTIONS = 100;
val NUMBER_OF_BUYERS = 100;
val NUMBER_OF_SELLERS = 3
val TITLES: Map[Int, String] = AuctionDataCreator.createAuctionsTitle;
val ACTOR_SELECTION_SEARCH = "/auctionSearch"
val ACTOR_SELECTION_SEARCH = "/masterSearch"
val NUMER_OF_SEARCH_ACTORS: Int = 10
val RESULTS_OUTPUT_FILE = "results.txt"

case class AuctionPrice(price: Int, auction: ActorRef)
}
27 changes: 27 additions & 0 deletions src/auction/helpers/TimeMeasure.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package auction.helpers

import java.io._
import scala.collection.mutable.MutableList

object TimeMeasure {
var data : List[String] = List();

private def printToFile(f: java.io.File)(op: java.io.PrintWriter => Unit) {
val p = new java.io.PrintWriter(f)
try { op(p) } finally { p.close() }
}

def addResult(result: String) {
data = data :+ result
}

def writeResultsToFile() {
printToFile(new File(SystemSettings.RESULTS_OUTPUT_FILE)) {
p =>
{
if (p != null) data.foreach(p.println)
}
}
}

}

0 comments on commit d794a03

Please sign in to comment.