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.
[...] follow-up to the previous post – custom Telnet shared groovy shells. This time having shared context for all shells, i.e. [...]