diff --git a/src/main/scala/scalaz/nio/channels/SelectionKey.scala b/src/main/scala/scalaz/nio/channels/SelectionKey.scala index c3fdbd6..638f416 100644 --- a/src/main/scala/scalaz/nio/channels/SelectionKey.scala +++ b/src/main/scala/scalaz/nio/channels/SelectionKey.scala @@ -32,14 +32,16 @@ class SelectionKey(private[nio] val selectionKey: JSelectionKey) { final val cancel: UIO[Unit] = IO.effectTotal(selectionKey.cancel()) - final val interestOps: UIO[Int] = - IO.effectTotal(selectionKey.interestOps()) + final val interestOps: IO[CancelledKeyException, Int] = + IO.effect(selectionKey.interestOps()).refineOrDie(JustCancelledKeyException) - final def interestOps(ops: Int): UIO[SelectionKey] = - IO.effectTotal(selectionKey.interestOps(ops)).map(new SelectionKey(_)) + final def interestOps(ops: Int): IO[CancelledKeyException, SelectionKey] = + IO.effect(selectionKey.interestOps(ops)) + .map(new SelectionKey(_)) + .refineOrDie(JustCancelledKeyException) - final val readyOps: UIO[Int] = - IO.effectTotal(selectionKey.readyOps()) + final val readyOps: IO[CancelledKeyException, Int] = + IO.effect(selectionKey.readyOps()).refineOrDie(JustCancelledKeyException) final def isReadable: IO[CancelledKeyException, Boolean] = IO.effect(selectionKey.isReadable()).refineOrDie(JustCancelledKeyException) diff --git a/src/main/scala/scalaz/nio/channels/Selector.scala b/src/main/scala/scalaz/nio/channels/Selector.scala index ef44780..7df5599 100644 --- a/src/main/scala/scalaz/nio/channels/Selector.scala +++ b/src/main/scala/scalaz/nio/channels/Selector.scala @@ -1,43 +1,54 @@ package scalaz.nio.channels import java.io.IOException -import java.nio.channels.{ Selector => JSelector } +import java.nio.channels.{ ClosedSelectorException, Selector => JSelector } import scalaz.nio.channels.spi.SelectorProvider import scalaz.nio.io._ -import scalaz.zio.{ IO, UIO } +import scalaz.zio.{ IO, JustExceptions, UIO } import scalaz.zio.duration.Duration import scala.collection.JavaConverters class Selector(private[nio] val selector: JSelector) { + import Selector._ + final val isOpen: UIO[Boolean] = IO.effectTotal(selector.isOpen) final val provider: UIO[SelectorProvider] = IO.effectTotal(selector.provider()).map(new SelectorProvider(_)) - final val keys: UIO[Set[SelectionKey]] = - IO.effectTotal(selector.keys()).map { keys => + final val keys: IO[ClosedSelectorException, Set[SelectionKey]] = + IO.effect(selector.keys()).refineOrDie(JustClosedSelectorException).map { keys => JavaConverters.asScalaSet(keys).toSet.map(new SelectionKey(_)) } - final val selectedKeys: UIO[Set[SelectionKey]] = - IO.effectTotal(selector.selectedKeys()).map { keys => + final val selectedKeys: IO[ClosedSelectorException, Set[SelectionKey]] = + IO.effect(selector.selectedKeys()).refineOrDie(JustClosedSelectorException).map { keys => JavaConverters.asScalaSet(keys).toSet.map(new SelectionKey(_)) } final def removeKey(key: SelectionKey): UIO[Unit] = IO.effectTotal(selector.selectedKeys().remove(key.selectionKey)).void - final val selectNow: IO[IOException, Int] = - IO.effect(selector.selectNow()).refineOrDie(JustIOException) + /** + * Can throw IOException and ClosedSelectorException. + */ + final val selectNow: IO[Exception, Int] = + IO.effect(selector.selectNow()).refineOrDie(JustExceptions) - final def select(timeout: Duration): IO[IOException, Int] = - IO.effect(selector.select(timeout.toMillis)).refineOrDie(JustIOException) + /** + * Can throw IOException and ClosedSelectorException. + */ + final def select(timeout: Duration): IO[Exception, Int] = + IO.effect(selector.select(timeout.toMillis)).refineOrDie(JustExceptions) - final val select: IO[IOException, Int] = - IO.effect(selector.select()).refineOrDie(JustIOException) + /** + * Can throw IOException and ClosedSelectorException. + */ + final val select: IO[Exception, Int] = + IO.effect(selector.select()).refineOrDie(JustExceptions) final val wakeup: IO[Nothing, Selector] = IO.effectTotal(selector.wakeup()).map(new Selector(_)) @@ -48,6 +59,10 @@ class Selector(private[nio] val selector: JSelector) { object Selector { + val JustClosedSelectorException: PartialFunction[Throwable, ClosedSelectorException] = { + case e: ClosedSelectorException => e + } + final val make: IO[IOException, Selector] = IO.effect(new Selector(JSelector.open())).refineOrDie(JustIOException)