Welcome to the machine ...
Dienstag Mai 12, 2009
UNIX DaytimeServer - the Scala way
I'm playing around with Scala. No, frankly I'm in love with it. While reading through "Programming in Scala" (which is a perfect introduction into the subject), I converted the Daytime Server into a Scala version. Get the NetBeans Project.
Here's how it looks like:
1 package daytimeserver 2 3 import java.io.IOException 4 import java.nio.ByteBuffer 5 import java.util.Date 6 import java.nio.channels.SelectionKey 7 8 import com.sun.grizzly.{ ProtocolFilter, Context, Controller, ProtocolChain, 9 DefaultProtocolChain, ProtocolChainInstanceHandler, TCPSelectorHandler } 10 11 import com.sun.grizzly.util.OutputWriter 12 13 class MyTCPSelectorHandler extends TCPSelectorHandler { 14 15 override def onAcceptInterest(key:SelectionKey, ctx: Context ) : Boolean = { 16 var channel = acceptWithoutRegistration(key) 17 18 if (channel != null) { 19 configureChannel(channel) 20 var readKey = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE) 21 readKey.attach(System.currentTimeMillis()) 22 } 23 24 false 25 } 26 } 27 28 class MyProtocolChainInstanceHandler extends ProtocolChainInstanceHandler { 29 30 private var protocolChain = new DefaultProtocolChain() 31 32 def poll() : ProtocolChain = protocolChain 33 def offer(instance: ProtocolChain ) : Boolean = true 34 } 35 36 class DaytimeWriterFilter extends ProtocolFilter { 37 38 def execute(ctx: Context) : Boolean = { 39 if (ctx.getProtocol() == Controller.Protocol.TCP) { 40 var channel = ctx.getSelectionKey().channel() 41 var now = (new Date()).toString() + "\n" 42 var buffer = ByteBuffer.wrap(now.getBytes()) 43 44 OutputWriter.flushChannel(channel, buffer) 45 46 channel.close() 47 buffer.clear() 48 } 49 false 50 } 51 52 def postExecute(ctx : Context ) : Boolean = false 53 } 54 55 object DaytimeServer extends Application { 56 57 val PORT : Int = 1405 58 var controller = new Controller() 59 var tcpSelectorHandler = new MyTCPSelectorHandler() 60 var pciHandler = new MyProtocolChainInstanceHandler() 61 var protocolChain = pciHandler.poll() 62 63 tcpSelectorHandler.setPort(PORT) 64 controller.addSelectorHandler(tcpSelectorHandler) 65 controller.setProtocolChainInstanceHandler(pciHandler) 66 protocolChain.addFilter(new DaytimeWriterFilter()) 67 68 try { 69 println("Starting Daytime server running on port " + PORT) 70 controller.start() 71 } catch { 72 case ex: IOException => println("Exception:" + ex) 73 } 74 } 75
Posted at 01:03PM Mai 12, 2009 by schmidtm in glassfish | Kommentare[3]
Blech! Scala-code is unreadable
> var channel = ctx.getSelectionKey().channel()
What type has channel? Do I have to read the library-sourcecode?
> var readKey = channel.register(selector ...
What type has readKey?
> var protocolChain = pciHandler.poll()
What type has protocolChain?
Do i have to read the library-sourcecode once again for that?
Gesendet von UliW am Mai 13, 2009 at 11:45 AM CEST #
Hi Uli,
one could have written:
var channel:SelectableChannel = ctx.getSelectionKey().channel()
Not being forced to do so everytime (by using type-inference) is considered a feature of Scala.
You are free to disagree. Nevertheless you don't have to read the sourcecode but the API documentation. Most IDE's take you there automatically.
Gesendet von Matthias Schmidt am Mai 13, 2009 at 02:43 PM CEST #
When you hover your mouse over a declariation, you will get a hint denoting type of the expression. At least in Eclipse, NetBeans and IntellijIdea
Gesendet von 83.3.94.194 am Mai 17, 2009 at 01:36 PM CEST #