diff --git a/app/Global.scala b/app/Global.scala index 55b1d0f..27091b7 100644 --- a/app/Global.scala +++ b/app/Global.scala @@ -3,9 +3,9 @@ import model._ object Global extends GlobalSettings { override def onStart(app: Application) { - LookupApplication.startup() + // LookupApplication.startup() } override def onStop(app: Application) { - LookupApplication.shutdown() + // LookupApplication.shutdown() } } \ No newline at end of file diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala index 4a21241..e08678d 100644 --- a/app/controllers/Application.scala +++ b/app/controllers/Application.scala @@ -2,13 +2,47 @@ package controllers import play.api._ import play.api.mvc._ +import play.api.libs.json._ + +import play.api.libs.iteratee._ +import play.api.libs.iteratee.Concurrent + +import scala.concurrent._ +import ExecutionContext.Implicits.global + +import scala.concurrent.duration._ +import akka.util.Timeout +import akka.pattern.ask import model._ -import ca.polymtl.log4900.eval._ + +import ca.polymtl.log4900.eval.Add +import ca.polymtl.log4900.eval.MathResult +import ca.polymtl.log4900.eval.AddResult object Application extends Controller { - def index = Action { - LookupApplication.doSomething(Add(100, 100)) - Ok(views.html.index("Your new application is ready.")) - } + implicit val timeout = Timeout(5 seconds) + + def index = Action { implicit request => + Ok(views.html.index(request)) + } + + def eval = WebSocket.using[JsValue] { implicit request => + val (enumerator, channel) = Concurrent.broadcast[JsValue] + + val in = Iteratee.foreach[JsValue](content => { + val op1 = (content \ "op1").as[Int] + val op2 = (content \ "op2").as[Int] + + (LookupApplication.actor ? Add(1, 1)).mapTo[MathResult] onComplete { + case(e) => println("fail") + } +// } onSuccess { +// case AddResult(o1,o2,res) => println(s"$o1 + $o2 = $res") +// } + }) + + (in, enumerator) + } + } \ No newline at end of file diff --git a/app/model/Calculator.scala b/app/model/Calculator.scala index f7b692f..e118860 100644 --- a/app/model/Calculator.scala +++ b/app/model/Calculator.scala @@ -12,9 +12,9 @@ import akka.actor.ReceiveTimeout import ca.polymtl.log4900.eval._ object LookupApplication extends Bootable { - val system = ActorSystem("LookupApplication", ConfigFactory.load.getConfig("remotelookup")) - val remotePath = "akka.tcp://CalculatorApplication@127.0.0.1:2552/user/simpleCalculator" - val actor = system.actorOf(Props(classOf[LookupActor], remotePath), "lookupActor") + lazy val system = ActorSystem("LookupApplication", ConfigFactory.load.getConfig("remotelookup")) + lazy val remotePath = "akka.tcp://CalculatorApplication@127.0.0.1:2552/user/simpleCalculator" + lazy val actor = system.actorOf(Props(classOf[LookupActor], remotePath), "lookupActor") def doSomething(op: MathOp): Unit = actor ! op diff --git a/app/views/index.scala.html b/app/views/index.scala.html index bd36568..ea2136f 100644 --- a/app/views/index.scala.html +++ b/app/views/index.scala.html @@ -1,5 +1,11 @@ -@(message: String) +@(implicit request: RequestHeader) -@main("Welcome") { -

Hello world

-} +@main("Hey") { +
+ + + + +
+}{ + +} \ No newline at end of file diff --git a/app/views/main.scala.html b/app/views/main.scala.html index 746262a..2572ea3 100644 --- a/app/views/main.scala.html +++ b/app/views/main.scala.html @@ -1,4 +1,4 @@ -@(title: String)(content: Html) +@(title: String)(content: Html)(scripts: Html) @@ -11,5 +11,7 @@ @content + + @scripts diff --git a/conf/routes b/conf/routes index fba8d4d..794207d 100644 --- a/conf/routes +++ b/conf/routes @@ -1,3 +1,6 @@ -GET / controllers.Application.index -GET /assets/*file controllers.Assets.at(path="/public", file) -GET /webjars/*file controllers.WebJarAssets.at(file) \ No newline at end of file +GET / controllers.Application.index +GET /eval controllers.Application.eval + + +GET /assets/*file controllers.Assets.at(path="/public", file) +GET /webjars/*file controllers.WebJarAssets.at(file) \ No newline at end of file diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 4870c24..4a5f9c5 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -14,9 +14,11 @@ object Dependencies { val webjars = "org.webjars" %% "webjars-play" % "2.2.0-RC1" val bootstrap = "org.webjars" % "bootstrap" % "3.0.0" + val jquery = "org.webjars" % "jquery" % "2.0.3" val frontEnd = Seq( webjars, - bootstrap + bootstrap, + jquery ) } \ No newline at end of file diff --git a/project/plugins.sbt b/project/plugins.sbt index 5325a5c..e1aef47 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -5,4 +5,4 @@ logLevel := Level.Warn resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/" // Use the Play sbt plugin for Play projects -addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0-RC2") \ No newline at end of file +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0") \ No newline at end of file diff --git a/public/javascripts/chat.js b/public/javascripts/chat.js new file mode 100644 index 0000000..6cd2b3f --- /dev/null +++ b/public/javascripts/chat.js @@ -0,0 +1,22 @@ +$(function(){ + 'use strict'; + + var chatSocket = new WebSocket($('form').attr("action")); + + $("[type='submit']").click(function(e){ + e.preventDefault(); + + chatSocket.send( + JSON.stringify({ + op1: parseInt($("[name='op1']").val()), + op2: parseInt($("[name='op2']").val()) + }) + ); + return false; + }); + + chatSocket.onmessage = function(e){ + var data = JSON.parse(event.data) + $(".result").text(data.result || data.error); + }; +}); \ No newline at end of file