Created
October 3, 2012 07:24
-
-
Save follesoe/3825575 to your computer and use it in GitHub Desktop.
Atempt to build a simple, distributed TicTacToe - we gave up after spending all our time on SBT/versioning troubles.
This file contains hidden or 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
tictactoeserver { | |
include "common" | |
akka { | |
remote.netty.port = 2552 | |
} | |
} | |
tictactoeclient { | |
include "common" | |
akka { | |
remote.netty.port = 2553 | |
} | |
} |
This file contains hidden or 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
akka { | |
actor { | |
provider = "akka.remote.RemoteActorRefProvider" | |
} | |
remote { | |
netty { | |
hostname = "127.0.0.1" | |
} | |
} | |
} |
This file contains hidden or 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
import sbt._ | |
import sbt.Keys._ | |
object TictactortoeBuild extends Build { | |
lazy val tictactortoe = Project( | |
id = "tictactortoe", | |
base = file("."), | |
settings = Project.defaultSettings ++ Seq( | |
name := "TicTActorToe", | |
organization := "no.bekk", | |
version := "0.1-SNAPSHOT", | |
scalaVersion := "2.9.2", | |
resolvers += "Typesafe Releases" at "http://repo.typesafe.com/typesafe/releases", | |
libraryDependencies += "com.typesafe.akka" % "akka-actor" % "2.0.1", | |
libraryDependencies += "com.typesafe.akka" % "akka-remote" % "2.0.1", | |
libraryDependencies += "com.typesafe.akka" % "akka-kernel" % "2.0.1" | |
) | |
) | |
} |
This file contains hidden or 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
package no.bekk | |
import akka.kernel.Bootable | |
import scala.util.Random | |
//#imports | |
import com.typesafe.config.ConfigFactory | |
import akka.actor.{ ActorRef, Props, Actor, ActorSystem } | |
//#imports | |
class TicTacToeClientApplication extends Bootable { | |
//#setup | |
val system = | |
ActorSystem("TicTacToeClientApplication", ConfigFactory.load.getConfig("tictactoeclient")) | |
val actor = system.actorOf(Props[TicTacToeClientActor], "ticTacToeClientActor") | |
val remoteActor = system.actorFor( | |
"akka://[email protected]:2552/user/ticTacToeServerActor") | |
def connect(name: String) = { | |
actor ! (remoteActor, Connect(name)) | |
} | |
def disconnect(name: String) = { | |
actor ! (remoteActor ! Disconnect(name)) | |
} | |
//#setup | |
def startup() { | |
} | |
def shutdown() { | |
system.shutdown() | |
} | |
} | |
class TicTacToeClientActor extends Actor { | |
def receive = { | |
case (actor: ActorRef, msg: TicTacToeMessage) => actor ! msg | |
case s: String => println("From server: " + s) | |
} | |
} | |
object TicTacToeClientApp { | |
def main(args: Array[String]) { | |
val app = new TicTacToeClientApplication | |
println("Started TicTacToe Client Application") | |
while (true) { | |
app.connect("follesoe") | |
Thread.sleep(1000) | |
app.disconnect("follesoe") | |
Thread.sleep(1000) | |
} | |
} | |
} |
This file contains hidden or 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
package no.bekk | |
trait TicTacToeMessage | |
case class Connect(name: String) extends TicTacToeMessage | |
case class Disconnect(name: String) extends TicTacToeMessage |
This file contains hidden or 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
package no.bekk | |
import akka.kernel.Bootable | |
import akka.actor.{ Props, Actor, ActorSystem } | |
import com.typesafe.config.ConfigFactory | |
class TicTacToeServerActor extends Actor { | |
def receive = { | |
case Connect(name) ⇒ | |
println("%s connected".format(name)) | |
sender ! "Welcome to the Tic-Tac-Toe Lobby!" | |
//sender ! AddResult(n1, n2, n1 + n2) | |
case Disconnect(name) ⇒ | |
println("%s disconnected".format(name)) | |
//sender ! SubtractResult(n1, n2, n1 - n2) | |
} | |
} | |
class TicTacToeServerApplication extends Bootable { | |
//#setup | |
val system = ActorSystem("TicTacToeServerApplication", | |
ConfigFactory.load.getConfig("tictactoeserver")) | |
val actor = system.actorOf(Props[TicTacToeServerActor], "ticTacToeServerActor") | |
//#setup | |
def startup() { | |
println("Starting TicTacToeServerApplication") | |
} | |
def shutdown() { | |
println("Shutting down TicTacToeServerApplication") | |
system.shutdown() | |
} | |
} | |
object TicTacToeServerApp { | |
def main(args: Array[String]) { | |
new TicTacToeServerApplication | |
println("Started TicTacToe Server Application - waiting for players...") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment