Package

gopher

Permalink

package gopher

Provides scala API for 'go-like' CSP channels.

Overview

see readme for quick introduction.

Usage

At first you must receive gopherApi as Akka extension:

import gopher._

.....
val gopherApi = Gopher(actorSystem)

Then you can use CPS channels with blocling operations inside go clauses:

val channel = gopherApi.makeChannel[Long]
val n = 10000
val producer = go {
 @volatile var(x,y) = (0L,1L)
 for( s <- gopherApi.select.forever) {
   case z: channel.write if (z==x) =>
              x = y
              y = x+z
              if (x > n) {
                 channel.close
                 implicitly[FlowTermination[Unit]].doExit()
              }
 }
}
val consumer = for((c,i) <- channel.zip(1 to n)) {
   Console.println(s"fib(${i})=${c}")
}
Await.ready(consumer, 10 seconds)

and defer/recover in go/goScope

goScope{
  val f = openFile(myFileName)
  defer{
    if (! recover{case ex:FileNotFoundException => Console.println("invalid fname")}) {
       f.close()
    }
  }
}
See also

channels.SelectFactory

channels.SelectorBuilder

channels.Output

channels.Input

channels.Channel

GopherAPI

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. gopher
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. class ChannelClosedException extends IllegalStateException

    Permalink

    throwed when channel is closed:

    throwed when channel is closed:

    • during attempt to write to closed channel.
    • during attempt to read from closed channel 'behind' last message.
  2. class Defers[T] extends AnyRef

    Permalink

    Construction Defers: defer/recover is alternative mechanism to exception handling, simular to one in Go language.

    Construction Defers: defer/recover is alternative mechanism to exception handling, simular to one in Go language.

    We use one hidden in go and goScope construct are transformed to withDefers usage with help of macroses.

    It is also possible to use one unsugared (as in next example), but this is a bit verbose.

    def parseCsv(fname: String): Either[String, Seq[Seq[Double]]] =
     withDefer[Either[String,Seq[Seq[Double]]]]{ d =>
       val in = Source.fromFile(fname)
       d.defer{
          var r = d.recover{
                    case FileNotFoundException => Left("fileNotFound")
                  }
          if (!r) in.close()
          d.recover {
            case ex: Throwable => Left(ex.getMessage)
          }
       }
       val retval:Either[String,Seq[Seq[Double]]] = Right{
           for( (line, nLine) <- in.getLines.toSeq zip Stream.from(1) ) yield withDefer[Seq[Double]] { d =>
              line.split(",") map { s=>
                                    d.defer{
                                     d.recover{
                                        case ex: NumberFormatException =>
                                          throw new RuntimeException(s"parse error in line ${nLine} file ${fname} ")
                                     }
                                    }
                                    s.toDouble
                                  }
           }.toSeq
         }
       retval
    }
    

  3. trait FlowTermination[-A] extends AnyRef

    Permalink

    FlowTermination[-A] - termination of flow.

    FlowTermination[-A] - termination of flow.

    Inside each block in select loop or select apply (once or forever) we have implicit FlowTermination entity, which we can use for exiting the loop.

    select.forever{
        case x: info.read => Console.println(s"received from info $x")
        case x: control.read => implicitly[FlowTermination[Unit]].doExit(())
    }
  4. implicit class FutureWithRead[T] extends AnyRef

    Permalink

    sugar for reading value from future.

  5. class GopherAPI extends AnyRef

    Permalink

    Api for providing access to channel and selector interfaces.

  6. class GopherImpl extends GopherAPI with Extension

    Permalink

    Akka extension which provide gopherApi interface

    Akka extension which provide gopherApi interface

    See also

    GopherAPI

  7. class ParTransputer extends Transputer

    Permalink
  8. trait SelectTransputer extends ForeverSelectorBuilder with Transputer

    Permalink

    Transputer, where dehaviour can be described by selector function

  9. sealed trait ThreadingPolicy extends AnyRef

    Permalink
  10. trait Transputer extends AnyRef

    Permalink

    Reusable unit of application structure, which consists from set of input ports, set of output ports and behaviour

    Reusable unit of application structure, which consists from set of input ports, set of output ports and behaviour

    Transputers can be created as elementary behaviour, descibed by select statement and then can be combined into larger structures

    Transputers can be recovered from execeptions (i.e. transputer can be restarted or resume execution) or escalated to parent transputers or root superviser.

  11. trait TransputerLogging extends AnyRef

    Permalink

    mix this trait to ypu transputer for access to akka logging.

Value Members

  1. object Defers

    Permalink
  2. object Gopher extends ExtensionId[GopherImpl] with ExtensionIdProvider

    Permalink

    Factory object for Akka extension

    Factory object for Akka extension

    val actorSystem = ActorSystem("myapp")
    val gopherApi = Gopher(actorSystem)
  3. object GopherAPI

    Permalink
  4. object GopherAPIExtensionHelper

    Permalink
  5. object ThreadingPolicy

    Permalink
  6. object Transputer

    Permalink
  7. macro def asyncApply1[A, B, C](hof: ((A) ⇒ B) ⇒ C)(nf: (A) ⇒ Future[B]): Future[C]

    Permalink
  8. def awaitImpl[T](c: Context)(v: scala.reflect.macros.blackbox.Context.Expr[Future[T]]): scala.reflect.macros.blackbox.Context.Expr[T]

    Permalink
  9. package channels

    Permalink

    Core entity is Continuated which provide iteratee-like structure for reading and writing.

    Overview

    • Input and Output provide callback-based interfaces for input/output facilities.
    • Channel provide implementation for asynchronics channels processing inside JVM.
    • SelectorBuilder is a way to create selectors.

    Internals

    Core entity is Continuated which provide iteratee-like structure for reading and writing. Instance of Continuated represent one step of computations and leave in queue inside ChannelProcessor or ChannelActor Selector transform Continuated to executed exclusive with each other within one selector. Also we have IdleDetector which determinate idle selectors and activer appropriative actions.

  10. implicit def compileTimeFlowTermination[A]: FlowTermination[A]

    Permalink
    Annotations
    @compileTimeOnly( ... )
  11. def defer(x: ⇒ Unit): Unit

    Permalink

    pseudostatement which can be used inside go/goScope block.

    pseudostatement which can be used inside go/goScope block.

    Annotations
    @compileTimeOnly( ... )
  12. macro def go[T](body: T)(implicit ec: ExecutionContext): Future[T]

    Permalink

    starts asyncronics execution of body in provided execution context.

    starts asyncronics execution of body in provided execution context. Inside go we can use defer/recover clauses and blocked read/write channel operations.

  13. macro def goScope[T](body: T): T

    Permalink

    provide access to using defer/recover inside body in the current thread of execution.

  14. package goasync

    Permalink
  15. def recover[T](f: PartialFunction[Throwable, T]): Boolean

    Permalink

    can be called only from defer block.

    can be called only from defer block. If we in handling exception, try to apply f to exception and if it's applied - stop panic and return true, otherwise return false.

    f

    - partial function for recovering exception.

    returns

    true if exception was recovered, false otherwise

    Annotations
    @compileTimeOnly( ... )
  16. implicit def toAsyncFullReadSelectorArgument[A, B](f: (ContRead[A, B]) ⇒ Option[(In[A]) ⇒ Future[Continuated[B]]]): ReadSelectorArgument[A, B]

    Permalink
  17. implicit def toAsyncFullSkipSelectorArgument[A](f: (Skip[A]) ⇒ Option[Future[Continuated[A]]]): SkipSelectorArgument[A]

    Permalink
  18. implicit def toAsyncFullWriteSelectorArgument[A, B](f: (ContWrite[A, B]) ⇒ Option[(A, Future[Continuated[B]])]): WriteSelectorArgument[A, B]

    Permalink
  19. implicit def toAsyncIterable[T](x: Iterable[T]): AsyncIterable[T]

    Permalink
  20. implicit def toAsyncNoGenReadSelectorArgument[A, B](f: (ContRead[A, B]) ⇒ (A) ⇒ Future[Continuated[B]]): ReadSelectorArgument[A, B]

    Permalink
  21. implicit def toAsyncNoOptSkipSelectorArgument[A](f: (Skip[A]) ⇒ Future[Continuated[A]]): SkipSelectorArgument[A]

    Permalink
  22. implicit def toAsyncNoOptWriteSelectorArgument[A, B](f: (ContWrite[A, B]) ⇒ (A, Future[Continuated[B]])): WriteSelectorArgument[A, B]

    Permalink
  23. implicit def toAsyncNoOptionReadSelectorArgument[A, B](f: (ContRead[A, B]) ⇒ (In[A]) ⇒ Future[Continuated[B]]): ReadSelectorArgument[A, B]

    Permalink
  24. implicit def toAsyncOption[T](x: Option[T]): AsyncOption[T]

    Permalink
  25. implicit def toAsyncPairReadSelectorArgument[A, B](f: (A, ContRead[A, B]) ⇒ Future[Continuated[B]]): ReadSelectorArgument[A, B]

    Permalink
  26. implicit def toSyncPairReadSelectorArgument[A, B](f: (A, ContRead[A, B]) ⇒ Continuated[B]): ReadSelectorArgument[A, B]

    Permalink
  27. implicit def toSyncReadSelectorArgument[A, B](f: (ContRead[A, B]) ⇒ (In[A]) ⇒ Continuated[B]): ReadSelectorArgument[A, B]

    Permalink
  28. implicit def toSyncSelectorArgument[A](f: (Skip[A]) ⇒ Continuated[A]): SkipSelectorArgument[A]

    Permalink
  29. implicit def toSyncWriteSelectorArgument[A, B](f: (ContWrite[A, B]) ⇒ (A, Continuated[B])): WriteSelectorArgument[A, B]

    Permalink
  30. package transputers

    Permalink

    transputers implementations

    transputers implementations

    See also

    gopher.transputers.ReplicatedTransputer

    gopher.transputers.TransputerSupervisor

  31. package util

    Permalink
  32. object withDefer

    Permalink

    syntax sugar, for calling Defers.

Inherited from AnyRef

Inherited from Any

Ungrouped