Skip to content

Commit

Permalink
Getting ready to wire JSON service to Hanuman
Browse files Browse the repository at this point in the history
  • Loading branch information
mslinn committed Oct 20, 2011
1 parent 08ff44c commit 8499d27
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
17 changes: 12 additions & 5 deletions src/main/scala/net/interdoodle/hbe/HelloJsonServices.scala
Expand Up @@ -13,24 +13,31 @@ import blueeyes.json.JsonAST._
* @author Mike Slinn */

trait HelloJsonServices extends BlueEyesServiceBuilder
with HttpRequestCombinators
with BijectionsChunkString
with BijectionsChunkJson {
with HttpRequestCombinators
with BijectionsChunkString
with BijectionsChunkJson {
val helloJson: HttpService[ByteChunk] = service("helloJson", "0.1") {
requestLogging {
logging {
log =>
context: HttpServiceContext =>
request {
path("/json") {
path("/json/") {
jvalue {
get {
requestParam: HttpRequest[JValue] =>
requestParam:HttpRequest[JValue] =>
val json = JString("Hello World!")
val response = HttpResponse[JValue](content = Some(json))
log.info(response.toString())
Future.sync(response)
}
path('operation/'id) {
get { request =>
val operation = request.parameters('operation)
val id = request.parameters('id)
Future.sync(HttpResponse(content = Some("session ID=" + id + "; operation=" + operation)))
}
}
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/main/scala/net/interdoodle/hbe/domain/Hanuman.scala
Expand Up @@ -4,24 +4,29 @@ import collection.mutable.LinkedList
import akka.event.EventHandler
import akka.actor.{Actor, ActorRef}

/** Monkey god (supervisor) creates many Akka Actor references (to type Monkey) with the same probability distribution.

/** Monkey god (supervisor) creates many Akka Actor references (to type Monkey) with identical probability distributions.
* Dispatches requests to generate semi-random text.
* @author Mike Slinn */
class Hanuman (corpus:String, number:Int=100) extends Actor {

class Hanuman (corpus:String, number:Int=100) extends Actor {
var busyMonkeyActorRefs = List[ActorRef]()
var monkeyActorRefs = List[ActorRef]()

val letterProbability = new LetterProbabilities()
letterProbability.add(corpus)
letterProbability.computeValues()

for (i <- 1 to number) {
val monkeyActorRef = Actor.actorOf(new Monkey(letterProbability)).start()
monkeyActorRef.id = "monkey_" + i.toString
monkeyActorRefs = monkeyActorRef :: monkeyActorRefs
}


def generatePages() {
for (monkeyActorRef <- monkeyActorRefs) {
val future = monkeyActorRef ! "type"
monkeyActorRef ! "type"
}
}

Expand All @@ -41,11 +46,12 @@ class Hanuman (corpus:String, number:Int=100) extends Actor {
println("Monkeys are all finished typing!")
}
case _ => {
EventHandler.info(this, "Hanuman received unknown message")
EventHandler.info(this, "Hanuman received an unknown message")
}
}

def remove[A](c:A, l:List[A]) = l indexOf c match {
/** Removes an element from a list */
private def remove[A](c:A, l:List[A]) = l indexOf c match {
case -1 => l
case n => (l take n) ++ (l drop (n + 1))
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/net/interdoodle/hbe/domain/Monkey.scala
Expand Up @@ -27,6 +27,6 @@ class Monkey (letterProbability:LetterProbabilities) extends Actor {
val page = generatePage
self.sender.foreach(_ ! TypingResult(monkeyRef, page))
}
case _ => EventHandler.info(this, "Monkey received unknown message")
case _ => EventHandler.info(this, "Monkey received an unknown message")
}
}
3 changes: 2 additions & 1 deletion src/test/scala/net/interdoodle/hbe/domain/HanumanSuite.scala
Expand Up @@ -15,7 +15,8 @@ class HanumanSuite extends FunSuite {
"0123456789"*2 +
"`~!@#$%^&*()_-+={[}]|\\\"':;<,>.?/"
val hanuman = Actor.actorOf(new Hanuman(document, 10)).start()
hanuman ! "generatePages"
val future = hanuman !!! "generatePages"
val result:Any = future.get
// todo write more tests and Hanuman business logic
}
}

0 comments on commit 8499d27

Please sign in to comment.