Permalink
Browse files

Getting ready to wire JSON service to Hanuman

  • Loading branch information...
1 parent 08ff44c commit 8499d2714a19eea9b7d3f8b21507bd43cc282411 @mslinn mslinn committed Oct 20, 2011
@@ -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)))
+ }
+ }
}
}
}
@@ -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"
}
}
@@ -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))
}
@@ -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")
}
}
@@ -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.