-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Achim Botz
committed
Mar 4, 2015
1 parent
937e2e9
commit 082ba8b
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.sandbox.chat | ||
|
||
import akka.actor.ActorSystem | ||
import akka.actor.Props | ||
|
||
object ChatApp extends App { | ||
|
||
val system = ActorSystem("chat-app") | ||
|
||
val server = | ||
system.actorOf(Props[ChatServer], "ChuckNorris") | ||
|
||
val client1 = | ||
system.actorOf(Props(new ChatClient("client1", server)), "client1") | ||
val client2 = | ||
system.actorOf(Props(new ChatClient("client2", server)), "client2") | ||
val client3 = | ||
system.actorOf(Props(new ChatClient("client3", server)), "client3") | ||
|
||
system.shutdown | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.sandbox.chat | ||
|
||
import akka.actor.Actor | ||
import ChatServer._ | ||
import akka.actor.ActorRef | ||
|
||
class ChatClient(name: String, chatServer: ActorRef) extends Actor { | ||
def receive: Actor.Receive = { | ||
case Broadcast(msg) => | ||
println(s"$name: received $msg") | ||
} | ||
|
||
chatServer ! Join(name) | ||
chatServer ! Broadcast(s"Hi! I'm $name") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.sandbox.chat | ||
|
||
import akka.actor.Actor | ||
import akka.actor.ActorRef | ||
import ChatServer._ | ||
|
||
class ChatServer extends Actor { | ||
var chatters: Map[ActorRef,String] = Map.empty | ||
|
||
override def receive: Actor.Receive = { | ||
case Join(name) => chatters += sender -> name | ||
case Leave => chatters -= sender | ||
case Broadcast(msg) => | ||
for { | ||
senderName <- chatters.get(sender) | ||
} | ||
chatters.keys foreach (_ ! Broadcast(s"$senderName: $msg")) | ||
} | ||
} | ||
|
||
object ChatServer { | ||
sealed trait ChatServerMsg | ||
case class Join(name: String) extends ChatServerMsg | ||
case object Leave | ||
case class Broadcast(msg: String) | ||
} |