-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacing Akka IO with Akka Streams #5
* New flow to encode Frames into ByteStrings
- Loading branch information
Showing
3 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
core/src/main/scala/net/sigusr/mqtt/impl/stages/FramesToBytes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package net.sigusr.mqtt.impl.stages | ||
|
||
import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} | ||
import akka.stream.{Attributes, FlowShape, Inlet, Outlet} | ||
import akka.util.ByteString | ||
import net.sigusr.mqtt.impl.frames.Frame | ||
import scodec.Codec | ||
|
||
class FramesToBytes extends GraphStage[FlowShape[Frame, ByteString]] { | ||
|
||
val in = Inlet[Frame]("FramesToBytes.in") | ||
val out = Outlet[ByteString]("FramesToBytes.out") | ||
|
||
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = | ||
new GraphStageLogic(shape) with OutHandler with InHandler { | ||
|
||
@throws[Exception](classOf[Exception]) | ||
override def onPull(): Unit = if (!hasBeenPulled(in)) tryPull(in) | ||
|
||
@throws[Exception](classOf[Exception]) | ||
override def onPush(): Unit = { | ||
val result = Codec[Frame].encode(grab(in)).require | ||
if (result.nonEmpty) | ||
push(out, ByteString(result.toByteArray)) | ||
tryPull(in) | ||
} | ||
|
||
setHandler(out, this) | ||
setHandler(in, this) | ||
} | ||
|
||
override def shape: FlowShape[Frame, ByteString] = FlowShape(in, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters