Tuesday, May 20, 2008

[A Grizzly Groovy Echo]

Well, I'm kicking off my first blogging experience, having been prompted to do so after attending the absoultely wonderful talk by Jared Richardson on Career 2.0 given at the TriJUG this month. He will be giving a talks on the 2008 No Fluff Just Stuff tour, so be sure to sign up and check it out.


My first JavaOne was very cool. I was freshly spurred to look at Grizzly again after attending the inspiring presentation by Jean-Francois Arcand and thought, just for kicks (and to get content for a blog :)) I would try to implement his "Grizzly Echo Server in 20 lines" in Groovy, my new favorite programming language with an eye to "just how short it can be with only what is provided by Groovy and Grizzly".
Here it is in 19 lines: (10 if you ignore the imports, blank line and formatting to fit the blog)


import com.sun.grizzly.*
import com.sun.grizzly.filter.*
import com.sun.grizzly.util.*
import java.nio.*

def protocolChain = new DefaultProtocolChain()
protocolChain.addFilter(new ReadFilter())
protocolChain.addFilter(new EchoFilter())
def pcih = [
    'poll':{protocolChain},
    'offer':{instance -> true}
]
def controller = new Controller()
def tcpHndlr = new TCPSelectorHandler()
tcpHndlr.port = 8080
controller.addSelectorHandler(tcpHndlr)
controller.protocolChainInstanceHandler =
    pcih as ProtocolChainInstanceHandler
controller.start();

Just paste into a groovyconsole and hit Ctrl-R. Use a typical telnet client to access. Never mind the "Hotel California" effect!
----
Lesson Learned: It is really easy to add links into a blog!!!