Code from my presentation at Scala Days 2011, Stanford June 2nd.

I was asked by several folks to post the code from my presentation – so here it goes (without any changes):

import java.util.concurrent.CountDownLatch
import java.util._
import org.gridgain.scalar._
import scalar._
import org.gridgain.grid.GridClosureCallMode._
import org.gridgain.grid._

object ScalaDays {
    def main(args: Array[String]): Unit = scalar {
        pingPong()
    }

    def pingPong()  {
        val g = grid$

        if (g.remoteNodes().size < 2)
            sys.error("I need more than 2 nodes")
        else {
            val n1 = g.remoteNodes$().head
            val n2 = g.remoteNodes$().tail.head

            n1.remoteListenAsync(n2, new GridListenActor[String] {
                def receive(nid: UUID, msg: String) {
                    println(msg)

                    val latch: CountDownLatch = g.nodeLocal.get("latch")

                    latch.getCount match {
                        case 1 => stop("STOP")
                        case _ => respond("PING")
                    }

                    latch.countDown
                }
            }).get

            n2.remoteListenAsync(n1, new GridListenActor[String] {
                def receive(nid: UUID, msg: String) {
                    println(msg)

                    msg match {
                        case "PING" => respond("PONG")
                        case "STOP" => stop
                    }
                }
            }).get

            n1 *< (UNICAST, () => {
                val latch = new CountDownLatch(10)

                g.nodeLocal[String, CountDownLatch].put("latch", latch)

                n2 !< "PING"

                latch.await()
            })
        }
    }

    def sayHi() = println("I'm in this grid!")

    def mapReduce(msg: String): Int =
        grid$.remoteProjection() @< (
            SPREAD,
            for (w <- msg.split(" ")) yield () => {
                println("Calculating for: "  + w)
                w.length
            },
            (s: Seq[Int]) => {
                println("Aggregating: " + s)
                s.sum
            }
        )

    def helloWorld(msg: String) =
        grid$.remoteProjection() *<
            (SPREAD, for (w <- msg.split(" ")) yield () => println(w.toUpperCase))
}

Enjoy!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: