Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added first tutorial based on Scala and SBT
- Loading branch information
Jonas Bonér
committed
Apr 1, 2011
1 parent
e7cf485
commit 329e8bb
Showing
31 changed files
with
507 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+839 KB
...utorials/akka-tutorial-pi-sbt/project/boot/other/net.java.dev.jna/jna/3.2.3/jna-3.2.3.jar
Binary file not shown.
Empty file.
Binary file added
BIN
+6.06 MB
akka-tutorials/akka-tutorial-pi-sbt/project/boot/scala-2.7.7/lib/scala-compiler.jar
Binary file not shown.
Binary file added
BIN
+3.61 MB
akka-tutorials/akka-tutorial-pi-sbt/project/boot/scala-2.7.7/lib/scala-library.jar
Binary file not shown.
Binary file added
BIN
+8.32 KB
.../project/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/classpath_2.7.7-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+43.4 KB
...bt/project/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/compile_2.7.7-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+104 KB
...sbt/sbt/0.7.5.RC1/compiler-interface-bin_2.7.7.final/compiler-interface-bin-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+101 KB
...sbt/sbt/0.7.5.RC1/compiler-interface-bin_2.8.0.final/compiler-interface-bin-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+101 KB
...sbt/sbt/0.7.5.RC1/compiler-interface-bin_2.8.1.final/compiler-interface-bin-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+103 KB
...-bin_2.9.0.RC1__50.0/compiler-interface-bin_2.9.0.RC1__50.0-0.7.5.RC1_20110323T000139.jar
Binary file not shown.
Binary file added
BIN
+15.1 KB
...scala-tools.sbt/sbt/0.7.5.RC1/compiler-interface-src/compiler-interface-src-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+85.3 KB
...oot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/compiler-interface-src/jline-0.9.94.jar
Binary file not shown.
Binary file added
BIN
+3.8 KB
...bt/project/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/control_2.7.7-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+154 KB
...-pi-sbt/project/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/io_2.7.7-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+925 KB
...-tutorial-pi-sbt/project/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/ivy-2.2.0.jar
Binary file not shown.
Binary file added
BIN
+245 KB
...pi-sbt/project/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/ivy_2.7.7-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+168 KB
...utorial-pi-sbt/project/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/jsch-0.1.31.jar
Binary file not shown.
Binary file added
BIN
+4.48 KB
...oject/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/launcher-interface-0.7.5.RC1.jar
Binary file not shown.
Empty file.
Binary file added
BIN
+2.23 MB
...pi-sbt/project/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/sbt_2.7.7-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+4.3 KB
...-pi-sbt/project/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/test-interface-0.5.jar
Binary file not shown.
Binary file added
BIN
+29.1 KB
.../project/boot/scala-2.7.7/org.scala-tools.sbt/sbt/0.7.5.RC1/xsbti/interface-0.7.5.RC1.jar
Binary file not shown.
Binary file added
BIN
+104 KB
akka-tutorials/akka-tutorial-pi-sbt/project/boot/scala-2.9.0.RC1/lib/jansi.jar
Binary file not shown.
Binary file added
BIN
+155 KB
akka-tutorials/akka-tutorial-pi-sbt/project/boot/scala-2.9.0.RC1/lib/jline.jar
Binary file not shown.
Binary file added
BIN
+10.9 MB
akka-tutorials/akka-tutorial-pi-sbt/project/boot/scala-2.9.0.RC1/lib/scala-compiler.jar
Binary file not shown.
Binary file added
BIN
+8.27 MB
akka-tutorials/akka-tutorial-pi-sbt/project/boot/scala-2.9.0.RC1/lib/scala-library.jar
Binary file not shown.
392 changes: 392 additions & 0 deletions
392
akka-tutorials/akka-tutorial-pi-sbt/project/boot/update.log
Large diffs are not rendered by default.
Oops, something went wrong.
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,5 @@ | ||
project.organization=se.scalablesolutions.akka | ||
project.name=Akka Tutorial 1 SBT | ||
project.version=1.0 | ||
build.scala.versions=2.9.0.RC1 | ||
sbt.version=0.7.5.RC1 |
6 changes: 6 additions & 0 deletions
6
akka-tutorials/akka-tutorial-pi-sbt/project/plugins/Plugins.scala
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,6 @@ | ||
import sbt._ | ||
|
||
class Plugins(info: ProjectInfo) extends PluginDefinition(info) { | ||
val akkaRepo = "Akka Repo" at "http://akka.io/repository" | ||
val akkaPlugin = "se.scalablesolutions.akka" % "akka-sbt-plugin" % "1.1-SNAPSHOT" | ||
} |
3 changes: 3 additions & 0 deletions
3
akka-tutorials/akka-tutorial-pi-sbt/project/plugins/project/build.properties
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,3 @@ | ||
#Project properties | ||
#Fri Apr 01 12:52:25 CEST 2011 | ||
plugin.uptodate=true |
101 changes: 101 additions & 0 deletions
101
akka-tutorials/akka-tutorial-pi-sbt/src/main/scala/Pi.scala
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,101 @@ | ||
/** | ||
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se> | ||
*/ | ||
|
||
package akka.tutorial.sbt.pi | ||
|
||
import akka.actor.{Actor, ActorRef} | ||
import Actor._ | ||
import akka.routing.{Routing, CyclicIterator} | ||
import akka.event.EventHandler | ||
import akka.dispatch.Dispatchers | ||
|
||
import System.{currentTimeMillis => now} | ||
import java.util.concurrent.CountDownLatch | ||
|
||
object Main extends App { | ||
Pi.calculate | ||
} | ||
|
||
/* | ||
Pi estimate: 3.1415926435897883 | ||
=== 8 workers (with custom dispatcher 4/4) | ||
Calculation time: 5163 millis | ||
=== 8 workers (with default dispatcher) | ||
Calculation time: 6789 millis | ||
=== 4 workers | ||
Calculation time: 5438 millis | ||
=== 2 workers | ||
Calculation time: 6002 millis | ||
=== 1 workers | ||
Calculation time: 8173 millis | ||
*/ | ||
object Pi { | ||
val nrOfWorkers = 4 | ||
val nrOfMessages = 10000 | ||
val lengthOfCalculationRange = 10000 | ||
|
||
// ===== Messages ===== | ||
sealed trait PiMessage | ||
case class Work(arg: Int, fun: (Int) => Double) extends PiMessage | ||
case class Result(value: Double) extends PiMessage | ||
|
||
// ===== Worker ===== | ||
class Worker extends Actor { | ||
def receive = { | ||
case Work(arg, fun) => self.reply(Result(fun(arg))) | ||
} | ||
} | ||
|
||
// ===== Master ===== | ||
class Master(nrOfMessages: Int, latch: CountDownLatch) extends Actor { | ||
var pi: Double = _ | ||
var count: Int = _ | ||
var start: Long = _ | ||
|
||
def receive = { | ||
case Result(value) => | ||
pi += value | ||
count += 1 | ||
if (count == nrOfMessages) self.stop | ||
} | ||
|
||
override def preStart = start = now | ||
|
||
override def postStop = { | ||
EventHandler.info(this, "\n\tPi estimate: \t\t%s\n\tCalculation time: \t%s millis".format(pi, (now - start))) | ||
Actor.registry.shutdownAll // shut down all workers | ||
latch.countDown | ||
} | ||
} | ||
|
||
def calculate = { | ||
val latch = new CountDownLatch(1) | ||
|
||
// create the master | ||
val master = actorOf(new Master(nrOfMessages, latch)).start | ||
|
||
// the master ref is also the 'implicit sender' that the workers should reply to | ||
implicit val replyTo = Option(master) | ||
|
||
// create the workers | ||
val workers = new Array[ActorRef](nrOfWorkers) | ||
for (i <- 0 until nrOfWorkers) workers(i) = actorOf[Worker].start | ||
|
||
// wrap them with a load-balancing router | ||
val router = Routing.loadBalancerActor(CyclicIterator(workers)).start | ||
|
||
val fun = (x: Int) => (for (k <- (x * lengthOfCalculationRange) to ((x + 1) * lengthOfCalculationRange - 1)) yield (4 * math.pow(-1, k) / (2 * k + 1))).sum | ||
|
||
// schedule work | ||
for (arg <- 0 until nrOfMessages) router ! Work(arg, fun) | ||
|
||
latch.await | ||
} | ||
} | ||
|