Skip to content
This repository has been archived by the owner on Mar 13, 2019. It is now read-only.

Commit

Permalink
remote actors (no response from future: class not found)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasseGuillaume committed Sep 27, 2013
1 parent 5f0ebb0 commit 19f9ed7
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
44 changes: 39 additions & 5 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
6 changes: 3 additions & 3 deletions app/model/Calculator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 10 additions & 4 deletions app/views/index.scala.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
@(message: String)
@(implicit request: RequestHeader)

@main("Welcome") {
<h1>Hello world</h1>
}
@main("Hey") {
<form action="@routes.Application.eval.webSocketURL()">
<input name="op1"> + <input name="op2">
<input type="submit">
<span class="result"></span>
</form>
}{
<script src="@routes.Assets.at("javascripts/chat.js")"></script>
}
4 changes: 3 additions & 1 deletion app/views/main.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@(title: String)(content: Html)
@(title: String)(content: Html)(scripts: Html)

<!DOCTYPE html>

Expand All @@ -11,5 +11,7 @@
</head>
<body>
@content
<script type='text/javascript' src='@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))'></script>
@scripts
</body>
</html>
9 changes: 6 additions & 3 deletions conf/routes
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
GET / controllers.Application.index
GET /assets/*file controllers.Assets.at(path="/public", file)
GET /webjars/*file controllers.WebJarAssets.at(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)
4 changes: 3 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0")
22 changes: 22 additions & 0 deletions public/javascripts/chat.js
Original file line number Diff line number Diff line change
@@ -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);
};
});

0 comments on commit 19f9ed7

Please sign in to comment.