Created
May 14, 2011 02:14
-
-
Save jsuereth/971618 to your computer and use it in GitHub Desktop.
IterateeNonBlockingEchoServer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package scalaz.example.nio | |
import scalaz._ | |
import concurrent.Promise | |
import effects.IO | |
import nio.sockets._ | |
import nio.Channels._ | |
import Scalaz._ | |
import iteratees._ | |
import java.nio.channels.SocketChannel | |
import java.nio.channels.ByteChannel | |
import java.nio.ByteBuffer | |
object ExampleSocketServer { | |
def run(implicit s: concurrent.Strategy) { | |
val server = serverSocketChannel(8080) | |
val firstConnection : IO[Promise[SocketChannel]] = | |
server.map(s => FlattenI(s(head[Promise, SocketChannel]))).map(_.run.map(_.get)) | |
val echo : IO[Iteratee[ByteBuffer, Promise, Unit]] = firstConnection flatMap { futureSocket => | |
val consumer = writeToChannel[Promise](futureSocket.map(_.asInstanceOf[ByteChannel])) | |
val reader = futureSocket map (i => readSocketChannel(i)) | |
reader.get map (e => FlattenI(e(consumer))) | |
} | |
val runner : IO[Unit] = echo.map(_.run) | |
runner.unsafePerformIO | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple echo server in my scalaz nio branch. Comments welcome!