Skip to content

Commit

Permalink
adding all actors
Browse files Browse the repository at this point in the history
  • Loading branch information
eric committed Jan 31, 2014
1 parent 7340c6e commit 2ec6df5
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 21 deletions.
37 changes: 17 additions & 20 deletions app/actors/MessageStream.scala
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
package actors

import scala.collection.parallel.ParSet

import play.api.libs.ws.WS
import play.api.libs.iteratee.{ Iteratee, Enumerator, Concurrent }
import play.api.libs.concurrent.Execution.Implicits._

import akka.actor.{Props, ActorRef, Actor}
import akka.actor.{ Props, ActorRef, Actor }
import akka.event.EventStream

object MessageStream {

case class Subscribe(actor: ActorRef)
type Message = String

case class Unsubscribe(actor: ActorRef)
}
case class Subscribe(actor: ActorRef)

case class Unsubscribe(actor: ActorRef)

class MessageStream extends Actor {
case class Broadcast(message: Message)

}

import MessageStream._
class MessageStream(messages: List[MessageStream.Message]) extends Actor {

type Message = String
import MessageStream._

var subscribers = ParSet[ActorRef]()
val eventStream = new EventStream

val messages = Enumerator[Message]("The discarded bicycle pressures the rod.", "How does the definitive plaster honor an appointed scholar?", "A hollow parade tends a worthwhile deaf. Why can't the circle migrate?")

val (streamEnumerator, streamChannel) = Concurrent.broadcast[Message]
def receive = {

streamEnumerator |>>> Iteratee.foreach[Message](m => subscribers.foreach(_ ! m))
case Subscribe(actor: ActorRef) => eventStream.subscribe(sender, classOf[SocketEndpoint.NewMessage])

def receive = {

case Subscribe(actor: ActorRef) => subscribers = subscribers + actor
case Unsubscribe(actor: ActorRef) => eventStream.unsubscribe(sender)

case Unsubscribe(actor: ActorRef) => subscribers = subscribers - actor
}
case Broadcast(message: Message) => eventStream.publish(SocketEndpoint.NewMessage(message))
}
}
27 changes: 27 additions & 0 deletions app/actors/SocketEndpoint.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package actors

import play.api.libs.ws.WS
import play.api.libs.iteratee.{ Iteratee, Enumerator, Concurrent }
import play.api.libs.concurrent.Execution.Implicits._

import akka.actor.{ Props, ActorRef, Actor }
import akka.event.EventStream

object SocketEndpoint {

case class NewMessage(message: MessageStream.Message)

def props(out: Enumerator[MessageStream.Message]) = Props(classOf[SocketEndpoint], out)

}

class SocketEndpoint(out: Enumerator[MessageStream.Message]) extends Actor {

import SocketEndpoint._

def receive = {

case NewMessage(message: MessageStream.Message) => out >>> Enumerator(message)

}
}
32 changes: 32 additions & 0 deletions app/actors/Supervisor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package actors

import play.api.libs.ws.WS
import play.api.libs.iteratee.{ Iteratee, Enumerator, Concurrent }
import play.api.libs.concurrent.Execution.Implicits._
import play.api.Play.current

import akka.actor.{ Props, ActorRef, Actor }
import akka.event.EventStream

object Supervisor {

case class NewSocket(out: Enumerator[MessageStream.Message])

}

class Supervisor extends Actor {

import Supervisor._

var sockets = Set[ActorRef]()

def receive = {

case NewSocket(out: Enumerator[MessageStream.Message]) => {
val newSocket = context.actorOf(SocketEndpoint.props(out))

sockets = sockets + newSocket
}

}
}
10 changes: 10 additions & 0 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ package controllers

import play.api._
import play.api.mvc._
import play.api.libs.concurrent.Akka
import play.api.Play.current

import akka.actor.Actor
import akka.actor.Props
import akka.actor.ActorSystem

import actors.Supervisor

object Application extends Controller {

val sockets = Akka.system.actorOf(Props(classOf[Supervisor]))

def index = Action {
Ok(views.html.index("Your new application is ready."))
}
Expand Down
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ libraryDependencies ++= Seq(
)

play.Project.playScalaSettings

scalariformSettings
28 changes: 28 additions & 0 deletions data/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Actually keffiyeh retro master cleanse American Apparel blog, cray Banksy hoodie Thundercats High Life cred typewriter VHS.
Lomo twee fanny pack Etsy, messenger bag meggings Blue Bottle readymade Pinterest.
Hella fashion axe Banksy fap, wayfarers direct trade farm-to-table art party hoodie 3 wolf moon.
Retro Etsy kitsch, Pinterest Vice Marfa American Apparel keffiyeh High Life Terry Richardson 8-bit slow-carb quinoa Austin roof party.
Williamsburg Shoreditch pug Blue Bottle art party kogi, authentic food truck Carles shabby chic Portland banjo before they sold out seitan vinyl.
Banh mi sustainable Vice pickled, organic disrupt ethnic gastropub butcher Thundercats.
Ethical YOLO 8-bit, Brooklyn sartorial mustache artisan slow-carb.
Tofu tattooed flannel locavore.
Shabby chic sriracha gentrify Bushwick, mustache put a bird on it readymade synth fingerstache deep v meggings blog photo booth.
Keffiyeh Portland pop-up, 3 wolf moon dreamcatcher fingerstache vegan 90's scenester Carles.
Etsy ethical viral, aesthetic mumblecore gentrify pork belly pour-over.
Occupy pickled post-ironic, hella Bushwick butcher you probably haven't heard of them gluten-free food truck craft beer paleo.
PBR butcher Blue Bottle, twee small batch Tonx irony Cosby sweater mixtape fap cardigan sartorial.
Bushwick retro sustainable squid, artisan next level ugh authentic lomo keffiyeh raw denim ethnic Pinterest Etsy.
Hella mlkshk cardigan Wes Anderson, 3 wolf moon Marfa ugh before they sold out kale chips flexitarian sriracha retro gluten-free pour-over pork belly.
Mixtape chillwave 90's butcher.
Narwhal actually viral cornhole tote bag, typewriter Marfa.
Swag VHS selvage chambray, single-origin coffee Tonx twee High Life gastropub cred Helvetica.
Put a bird on it street art cornhole 3 wolf moon, Tumblr meggings mumblecore sriracha irony locavore umami hashtag pour-over fashion axe.
DIY 90's Tonx, raw denim master cleanse Wes Anderson American Apparel Terry Richardson trust fund bespoke.
Shoreditch typewriter semiotics whatever slow-carb.
Sustainable food truck banjo meggings, semiotics single-origin coffee gluten-free Marfa asymmetrical Tumblr.
You probably haven't heard of them Wes Anderson pork belly Bushwick butcher drinking vinegar.
Chia drinking vinegar trust fund Brooklyn salvia.
You probably haven't heard of them Brooklyn Carles Tonx Bushwick.
Banjo umami High Life, lo-fi actually master cleanse PBR hella Pitchfork scenester letterpress.
VHS trust fund Tonx, four loko organic fingerstache Bushwick Thundercats wayfarers actually drinking vinegar fixie tote bag 90's wolf.
Readymade chia DIY keytar organic.
4 changes: 3 additions & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ 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.1")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.1")

addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.2.1")

0 comments on commit 2ec6df5

Please sign in to comment.