Little Groovy fun

Got a bit of free time to play with Groovy today.
So I checked out some groovy sockets example and made a little groovy shell server that is running in a groovy shell itself (-:

UPD: A little video demo

Sources (updated 2011-12-16 04:16):

server = new ServerSocket(54321);
sockets = [];

serverThread = new Thread() {
    private ServerSocket server;
    private Collection sockets;
    
    public void run() {
      
        while(true) {
            server.accept {
                socket ->
                println "Socket connection established"
                sockets.add(socket)
                socket.withStreams {
                    input, output ->
                        println "Instantiating shell"
                        new org.codehaus.groovy.tools.shell.Groovysh(new org.codehaus.groovy.tools.shell.IO(input,output,output)).run("");
                        println "Shell exit"
                }
            }
        }
    }

    public Thread init(ServerSocket server, Collection sockets) {
        this.server = server;
        this.sockets = sockets;
        return this;
    }
}.init(server, sockets);

serverThread.start();

There must be nicer Groovy syntax for instantiating Threads etc (and yeah, I still instinctively put brackets and semicolons in Java manner), but in general that is it.

Launch it, open some new terminal windows, run “telnet localhost 54321” in them, and le voila – groovy shells on Telnet.
Note that System.out output is displayed in “master” shell, so whenever you do System.out.println in telnet shells the output appears in “master” shell window – which is kind-of fun (-:

I should find some nice way to share some kind of a context between those shells, so they could share objects with each other (it’s all running in one JVM after all) – then some interesting things can be done with this.

That’s all for now.

Advertisements

One thought on “Little Groovy fun

  1. Pingback: Custom telnet Groovy shells – version with shared context « M.V.M-n.

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