Friday, April 23, 2010

Groovy Actors example

Groovy also implements Actors through the GPars library.

Groovy and Scala are both languages on top of the JVM and as i understand on some articles, they  both suffer on a JVM limitation and implement the Actor model nearly the same way and by using concurrency features that will be available in Java 7 (JSR166y).

I've reworked the little Groovy sample by using Grape: simple copy and paste the following code in a file actors.groovy for example and run:

chmod u+x actors.groovy
./actors.groovy

#!/usr/bin/env groovy

@Grab(group='org.codehaus.gpars', module='gpars', version='0.10-beta-1')

import groovyx.gpars.actor.AbstractPooledActor

class SecuredServer extends AbstractPooledActor {
    def password
    void afterStart() { 
        password = new Random().nextInt(10) 
        println "password set to ${password}"
    }
    void act() {
        loop {
            react {
                if (it > password) reply 'too large'
                else if (it < password) reply 'too small'
                else { reply 'autorized'; stop(); System.exit 0; }
            }
        }
    }
}

class Client extends AbstractPooledActor {
    def server
    void act() {
        loop {
            def guess = new Random().nextInt(10)
            server.send guess
            react {
                switch(it) {
                    case 'too large': println "$guess was too large"; break
                    case 'too small': println "$guess was too small"; break
                    case 'autorized': println "Access granted with: $guess"; stop(); break
                }
            }
        }
    }
}

def myServer = new SecuredServer().start()
def client = new Client(server: myServer).start()
[myServer, client]*.join()

1 comment: