The Artima Developer Community
Sponsored Link

Scala Buzz
MINA bindings for Scala

1 reply on 1 page. Most recent reply: Feb 11, 2008 11:51 PM by Andrew Gaydenko

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Rich Dougherty

Posts: 21
Nickname: richd
Registered: Aug, 2003

Rich Dougherty is a programmer from New Zealand.
MINA bindings for Scala Posted: Jan 7, 2008 1:09 AM
Reply to this message Reply

This post originated from an RSS feed registered with Scala Buzz by Rich Dougherty.
Original Post: MINA bindings for Scala
Feed Title: Rich Dougherty's Scala posts
Feed URL: http://blog.richdougherty.com/feeds/posts/default/-/scala?alt=rss
Feed Description: Programming with Scala.
Latest Scala Buzz Posts
Latest Scala Buzz Posts by Rich Dougherty
Latest Posts From Rich Dougherty's Scala posts

Advertisement

I've written some proof-of-concept Scala bindings for Apache MINA, a non-blocking I/O library. It was fairly straightforward to translate MINA's event-based model into one that uses message-passing between Scala actors. Although the bindings aren't complete, there's enough there to write a simple server.

Here's how we use the bindings to write a Hello World server.

First, we create an IoService to listen to port 12345. This IoService uses MINA's built-in protocol for sending and receiving lines of text. Note that we're not using the bindings yet; this is all normal MINA code.

val service = new NioAcceptor() service.setReuseAddress(true) service.getFilterChain().addLast("codec", new ProtocolCodecFilter( new TextLineCodecFactory(Charset.forName( "UTF-8" )))) service.setLocalAddress(new InetSocketAddress(12345))

Now we create an actor which listens for new sessions. (A session is MINA's name for a connection.) When we get a new session, we ask it to write Hello world, then we ask it close. The underlying MINA IoSession has been wrapped with an actor, and these requests are made by sending (!) messages to that actor.

val serviceHandler = actor { loop { react { case SessionCreated(session:Actor) => { session ! Write("Hello world") session ! Close(false) } } } }

We attach the actor to the IoService using the normal setHandler() method. An implicit conversion function automatically converts our serviceHandler actor into a MINA IoHandler.

service.setHandler(serviceHandler)

Finally, we bind the service so we can accept connections.

service.bind()

You can download the patched MINA source from here. The source based on a recent snapshot of MINA's trunk. I've added two modules: integration-scala and example-scala. You'll need Maven to compile the source.

Here's an example sequence of commands to compile the project, install the JARs and run the Reverser example server. (I set maven.test.skip because some tests are failing on MINA's trunk.)

$ mvn -Dmaven.test.skip install $ cd example-scala $ mvn scala:run ... Listening on port 12345.

Now you can telnet from another shell and use the server to reverse strings!

$ telnet localhost 12345 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. hello olleh goodbye eybdoog quit Connection closed by foreign host.

A few extra notes:

  • AsyncWeb should be added to MINA soon, and this will let MINA speak HTTP. This is the main reason I'm working with MINA's trunk rather than with one of the stable releases.
  • I've had problems on Mac OS X 10.4 with sockets staying in the CLOSE_WAIT state. I suspect that this is caused by a bug in Apple's version of the JRE. I have no problems when I use a more recent JRE on Linux.
  • I haven't done any performance testing at this stage.
  • I took inspiration for my Maven configuration from Lift. Thanks.

Read: MINA bindings for Scala


Andrew Gaydenko

Posts: 8
Nickname: anli
Registered: Dec, 2007

Re: MINA bindings for Scala Posted: Feb 11, 2008 11:51 PM
Reply to this message Reply
Rich,

What is the current state of your investigation? If I understand well, AW isn't in MINA trunk yet, but it is on the way. I'm looking for light (without servlet's crap) http server (being learning Scala), and your efforts are very interesting for me. BTW, will MINA (with http codec) support file uploading? I have tried to register in mina list, but did not get confirmation.


Andrew

Flat View: This topic has 1 reply on 1 page
Topic: MINA bindings for Scala Previous Topic   Next Topic Topic: Linguistic Success

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use