Browse files

Update for play 2.0.2 - make it thread-safe

  • Loading branch information...
1 parent b3b78fc commit 5120de5b77627836fe26602546f82c99a28cdf1b Gaëtan Renaudeau committed Jul 5, 2012
View
9 java/app/models/PaintRoom.java
@@ -14,10 +14,9 @@
public String name;
// The list of all connected painters (identified by ids)
- public Map<Integer, Painter> painters = new HashMap<Integer, Painter>();
-
- public int counter = 0;
- public int connections = 0;
+ public Map<Integer, Painter> painters = new HashMap<Integer, Painter>(); // FIXME not thread safe, use ConcurrentMap
+ public int counter = 0; // FIXME not thread safe, use AtomicInteger
+ public int connections = 0; // FIXME not thread safe, use AtomicInteger
public PaintRoom(String name) {
this.name = name;
@@ -91,4 +90,4 @@ public void notifyAll(JsonNode json) {
}
}
-}
+}
View
19 scala/app/controllers/Application.scala
@@ -5,7 +5,10 @@ import play.api.mvc._
import play.api.libs.json._
import play.api.libs.iteratee._
import play.api.libs.concurrent._
-import collection.mutable.{Map => MMap}
+import java.util.concurrent.atomic.AtomicInteger
+import java.util.concurrent.ConcurrentHashMap
+import scala.collection.mutable.ConcurrentMap
+import collection.JavaConversions._
object Application extends Controller {
@@ -38,22 +41,22 @@ case class Painter (name: String, color: String, size: Long) {
case class PaintRoom(name: String) {
// The list of all connected painters (identified by ids)
- val painters = MMap[Int, Painter]()
+ val painters: ConcurrentMap[Int, Painter] = new ConcurrentHashMap[Int, Painter]()
// The enum for pushing data to spread to all connected painters
val hubEnum = Enumerator.imperative[JsValue]()
// The hub used to get multiple output of a common input (the hubEnum)
val hub = Concurrent.hub[JsValue](hubEnum)
- private var counter = 0
- private var connections = 0
+ private var counter = new AtomicInteger(0)
+ private var connections = new AtomicInteger(0)
// Create a new painter and get a (input, output) couple for him
def createPainter(): (Iteratee[JsValue, _], Enumerator[JsValue]) = {
- counter += 1
- connections += 1
- val pid = counter // the painter id
+ counter.incrementAndGet()
+ connections.incrementAndGet()
+ val pid = counter.get // the painter id
// out: handle messages to send to the painter
val out =
@@ -86,7 +89,7 @@ case class PaintRoom(name: String) {
}) mapDone { _ =>
// User has disconnected.
painters -= pid
- connections -= 1
+ connections.decrementAndGet()
hubEnum push (JsObject(Seq("type" -> JsString("disconnect"), "pid" -> JsNumber(pid))))
Logger.debug("(pid:"+pid+") disconnected.")
Logger.info(connections+" painter(s) currently connected.");
View
2 scala/project/Build.scala
@@ -5,7 +5,7 @@ import PlayProject._
object ApplicationBuild extends Build {
val appName = "Play Painter"
- val appVersion = "1.0-SNAPSHOT"
+ val appVersion = "1.0.1"
val appDependencies = Seq(
// Add your project dependencies here,
View
2 scala/project/build.properties
@@ -1 +1 @@
-sbt.version=0.11.2
+sbt.version=0.11.3
View
2 scala/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("play" % "sbt-plugin" % "2.0-SNAPSHOT")
+addSbtPlugin("play" % "sbt-plugin" % "2.0.2")

0 comments on commit 5120de5

Please sign in to comment.