gopher.JVMGopher.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scala-gopher_3.0.0-RC2 Show documentation
Show all versions of scala-gopher_3.0.0-RC2 Show documentation
scala-gopher: asynchronous implementation of CSP ( go-like channels/selectors ) in scala
The newest version!
package gopher
import cps._
import gopher.impl._
import java.util.concurrent.Executors
import java.util.concurrent.ExecutorService
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.atomic.AtomicReference
import java.util.Timer
import java.util.logging._
import scala.concurrent.duration._
class JVMGopher[F[_]:CpsSchedulingMonad](cfg: JVMGopherConfig) extends Gopher[F]:
def makeChannel[A](bufSize:Int = 0, autoClose: Boolean = false) =
if autoClose then
PromiseChannel[F,A](this, taskExecutor)
else
if (bufSize == 0)
GuardedSPSCUnbufferedChannel[F,A](this, cfg.controlExecutor,cfg.taskExecutor)
else
GuardedSPSCBufferedChannel[F,A](this, bufSize, cfg.controlExecutor,cfg.taskExecutor)
val time = new JVMTime(this)
def setLogFun(logFun:(Level, String, Throwable|Null) => Unit): ((Level, String, Throwable|Null) => Unit) =
currentLogFun.getAndSet(logFun)
def log(level: Level, message: String, ex: Throwable| Null): Unit =
currentLogFun.get().apply(level,message,ex)
def taskExecutor = cfg.taskExecutor
def scheduledExecutor = JVMGopher.scheduledExecutor
private val currentLogFun: AtomicReference[(Level,String,Throwable|Null)=>Unit]=new AtomicReference(JVMGopher.defaultLogFun)
object JVMGopher extends GopherAPI:
def apply[F[_]:CpsSchedulingMonad](cfg: GopherConfig):Gopher[F] =
val jvmConfig = cfg match
case DefaultGopherConfig => defaultConfig
case jcfg:JVMGopherConfig => jcfg
new JVMGopher[F](jvmConfig)
lazy val timer = new Timer("gopher")
lazy val scheduledExecutor = Executors.newScheduledThreadPool(1)
lazy val defaultConfig=JVMGopherConfig(
controlExecutor=Executors.newFixedThreadPool(2),
taskExecutor=ForkJoinPool.commonPool(),
)
val logger = Logger.getLogger("JVMGopher")
def defaultLogFun(level: Level, message:String, ex: Throwable|Null): Unit =
if (ex eq null) {
logger.log(level, message)
} else {
logger.log(level, message, ex)
}
val Gopher = JVMGopher
© 2015 - 2025 Weber Informatics LLC | Privacy Policy