2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2018-01-04 17:26:29 +00:00
|
|
|
* Copyright (C) 2015-2018 Lightbend Inc. <https://www.lightbend.com>
|
2015-01-28 14:19:50 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
package akka.stream.impl
|
|
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
import akka.NotUsed
|
2015-08-19 23:04:20 -04:00
|
|
|
import akka.actor._
|
2017-03-16 21:04:07 +02:00
|
|
|
import akka.annotation.{ DoNotInherit, InternalApi }
|
2015-04-16 02:24:01 +02:00
|
|
|
import akka.stream._
|
2016-03-11 17:08:30 +01:00
|
|
|
import akka.stream.impl.StreamLayout.AtomicModule
|
2015-01-28 14:19:50 +01:00
|
|
|
import org.reactivestreams._
|
2016-07-27 13:29:23 +02:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
import scala.annotation.unchecked.uncheckedVariance
|
2016-03-11 17:08:30 +01:00
|
|
|
import akka.event.Logging
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2015-03-05 12:21:17 +01:00
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@DoNotInherit private[akka] abstract class SourceModule[+Out, +Mat](val shape: SourceShape[Out]) extends AtomicModule[SourceShape[Out], Mat] {
|
2016-03-11 17:08:30 +01:00
|
|
|
|
|
|
|
|
protected def label: String = Logging.simpleName(this)
|
|
|
|
|
final override def toString: String = f"$label [${System.identityHashCode(this)}%08x]"
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2015-04-10 14:39:48 +02:00
|
|
|
def create(context: MaterializationContext): (Publisher[Out] @uncheckedVariance, Mat)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2016-07-27 13:29:23 +02:00
|
|
|
// TODO: Remove this, no longer needed?
|
2015-01-28 14:19:50 +01:00
|
|
|
protected def newInstance(shape: SourceShape[Out] @uncheckedVariance): SourceModule[Out, Mat]
|
|
|
|
|
|
2016-07-27 13:29:23 +02:00
|
|
|
// TODO: Amendshape changed the name of ports. Is it needed anymore?
|
|
|
|
|
|
|
|
|
|
def attributes: Attributes
|
2015-03-05 12:21:17 +01:00
|
|
|
|
2015-07-06 22:00:21 +02:00
|
|
|
protected def amendShape(attr: Attributes): SourceShape[Out] = {
|
2016-07-27 13:29:23 +02:00
|
|
|
val thisN = traversalBuilder.attributes.nameOrDefault(null)
|
2015-07-06 22:00:21 +02:00
|
|
|
val thatN = attr.nameOrDefault(null)
|
2015-03-05 12:21:17 +01:00
|
|
|
|
2015-07-06 22:00:21 +02:00
|
|
|
if ((thatN eq null) || thisN == thatN) shape
|
2015-12-14 14:52:06 +01:00
|
|
|
else shape.copy(out = Outlet(thatN + ".out"))
|
2015-07-06 22:00:21 +02:00
|
|
|
}
|
2016-07-27 13:29:23 +02:00
|
|
|
|
|
|
|
|
override private[stream] def traversalBuilder = LinearTraversalBuilder.fromModule(this, attributes).makeIsland(SourceModuleIslandTag)
|
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-05 12:21:17 +01:00
|
|
|
* INTERNAL API
|
2015-01-28 14:19:50 +01:00
|
|
|
* Holds a `Subscriber` representing the input side of the flow.
|
|
|
|
|
* The `Subscriber` can later be connected to an upstream `Publisher`.
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@InternalApi private[akka] final class SubscriberSource[Out](val attributes: Attributes, shape: SourceShape[Out]) extends SourceModule[Out, Subscriber[Out]](shape) {
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2015-04-10 14:39:48 +02:00
|
|
|
override def create(context: MaterializationContext): (Publisher[Out], Subscriber[Out]) = {
|
2015-06-16 15:34:54 +02:00
|
|
|
val processor = new VirtualProcessor[Out]
|
2015-01-28 14:19:50 +01:00
|
|
|
(processor, processor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override protected def newInstance(shape: SourceShape[Out]): SourceModule[Out, Subscriber[Out]] = new SubscriberSource[Out](attributes, shape)
|
2016-07-27 13:29:23 +02:00
|
|
|
override def withAttributes(attr: Attributes): SourceModule[Out, Subscriber[Out]] = new SubscriberSource[Out](attr, amendShape(attr))
|
2015-01-28 14:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-05 12:21:17 +01:00
|
|
|
* INTERNAL API
|
2015-01-28 14:19:50 +01:00
|
|
|
* Construct a transformation starting with given publisher. The transformation steps
|
|
|
|
|
* are executed by a series of [[org.reactivestreams.Processor]] instances
|
|
|
|
|
* that mediate the flow of elements downstream and the propagation of
|
|
|
|
|
* back-pressure upstream.
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@InternalApi private[akka] final class PublisherSource[Out](p: Publisher[Out], val attributes: Attributes, shape: SourceShape[Out]) extends SourceModule[Out, NotUsed](shape) {
|
2016-03-11 17:08:30 +01:00
|
|
|
|
|
|
|
|
override protected def label: String = s"PublisherSource($p)"
|
|
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
override def create(context: MaterializationContext) = (p, NotUsed)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
override protected def newInstance(shape: SourceShape[Out]): SourceModule[Out, NotUsed] = new PublisherSource[Out](p, attributes, shape)
|
2016-07-27 13:29:23 +02:00
|
|
|
override def withAttributes(attr: Attributes): SourceModule[Out, NotUsed] = new PublisherSource[Out](p, attr, amendShape(attr))
|
2015-01-28 14:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-05 12:21:17 +01:00
|
|
|
* INTERNAL API
|
2015-01-28 14:19:50 +01:00
|
|
|
* Creates and wraps an actor into [[org.reactivestreams.Publisher]] from the given `props`,
|
|
|
|
|
* which should be [[akka.actor.Props]] for an [[akka.stream.actor.ActorPublisher]].
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@InternalApi private[akka] final class ActorPublisherSource[Out](props: Props, val attributes: Attributes, shape: SourceShape[Out]) extends SourceModule[Out, ActorRef](shape) {
|
2015-01-28 14:19:50 +01:00
|
|
|
|
2015-04-10 14:39:48 +02:00
|
|
|
override def create(context: MaterializationContext) = {
|
2016-05-03 18:58:26 -07:00
|
|
|
val publisherRef = ActorMaterializerHelper.downcast(context.materializer).actorOf(context, props)
|
2015-01-28 14:19:50 +01:00
|
|
|
(akka.stream.actor.ActorPublisher[Out](publisherRef), publisherRef)
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-19 17:19:45 +01:00
|
|
|
override protected def newInstance(shape: SourceShape[Out]): SourceModule[Out, ActorRef] =
|
|
|
|
|
new ActorPublisherSource[Out](props, attributes, shape)
|
2016-07-27 13:29:23 +02:00
|
|
|
override def withAttributes(attr: Attributes): SourceModule[Out, ActorRef] = new ActorPublisherSource(props, attr, amendShape(attr))
|
2015-03-31 15:13:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*/
|
2017-03-16 21:04:07 +02:00
|
|
|
@InternalApi private[akka] final class ActorRefSource[Out](
|
2017-09-07 21:07:41 +02:00
|
|
|
completionMatcher: PartialFunction[Any, Unit],
|
|
|
|
|
failureMatcher: PartialFunction[Any, Throwable],
|
|
|
|
|
bufferSize: Int, overflowStrategy: OverflowStrategy, val attributes: Attributes, shape: SourceShape[Out])
|
2015-03-31 15:13:57 +02:00
|
|
|
extends SourceModule[Out, ActorRef](shape) {
|
|
|
|
|
|
2016-03-11 17:08:30 +01:00
|
|
|
override protected def label: String = s"ActorRefSource($bufferSize, $overflowStrategy)"
|
|
|
|
|
|
2015-04-10 14:39:48 +02:00
|
|
|
override def create(context: MaterializationContext) = {
|
2016-05-03 18:58:26 -07:00
|
|
|
val mat = ActorMaterializerHelper.downcast(context.materializer)
|
2017-09-07 21:07:41 +02:00
|
|
|
val ref = mat.actorOf(context, ActorRefSourceActor.props(
|
|
|
|
|
completionMatcher,
|
|
|
|
|
failureMatcher,
|
|
|
|
|
bufferSize, overflowStrategy, mat.settings))
|
2015-03-31 15:13:57 +02:00
|
|
|
(akka.stream.actor.ActorPublisher[Out](ref), ref)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override protected def newInstance(shape: SourceShape[Out]): SourceModule[Out, ActorRef] =
|
2017-09-07 21:07:41 +02:00
|
|
|
new ActorRefSource[Out](completionMatcher, failureMatcher, bufferSize, overflowStrategy, attributes, shape)
|
2016-07-27 13:29:23 +02:00
|
|
|
override def withAttributes(attr: Attributes): SourceModule[Out, ActorRef] =
|
2017-09-07 21:07:41 +02:00
|
|
|
new ActorRefSource(completionMatcher, failureMatcher, bufferSize, overflowStrategy, attr, amendShape(attr))
|
2015-03-03 10:57:25 +01:00
|
|
|
}
|