Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Refine Exceptions for Selector and SelectionKey #101

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/main/scala/scalaz/nio/channels/SelectionKey.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 27 additions & 12 deletions src/main/scala/scalaz/nio/channels/Selector.scala
Original file line number Diff line number Diff line change
@@ -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(_))
Expand All @@ -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)

Expand Down