Skip to content

Commit

Permalink
Loop counter for RefuseActor
Browse files Browse the repository at this point in the history
  • Loading branch information
devlaam committed May 25, 2023
1 parent 135383b commit 2bdc756
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
13 changes: 10 additions & 3 deletions shared/src/main/scala/s2a/leucine/actors/user/refuse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ abstract class RefuseActor[Define <: RefuseDefine](private[actors] val actorDefi
/* The work package is initiated by this dummy letter send to myself. */
private val work = pack(new Actor.Letter[Accept]{},this)

/* Internal counter that counts the number of calls to process. */
private var loops: Long = 0

/* Deliver the letter in the envelope. */
private[actors] final def deliverEnvelope[Sender >: Common <: Accept](envelope: Env[Sender], state: State): State =
/* Let the letter be processed */
val processed = process()
val processed = process(loops)
/* Post a work message for the next process run. */
sendEnvelope(work)
/* Increase the loop counter */
loops = loops + 1
/* The state remains unchanged, if we work stateless, otherwise compute the new state.
* TODO: Can this also be solved compile time? In an elegant manner?
* Based on this: https://scastie.scala-lang.org/13dD1LD8Q3OUpLrn89oLqw? */
Expand All @@ -69,8 +74,10 @@ abstract class RefuseActor[Define <: RefuseDefine](private[actors] val actorDefi

/**
* Implement this method in your actor to process the workload in parts. As long as you do not
* explicitly stop the actor from the in- or outside, this method will continue to be called. */
protected def process(): Receive
* explicitly stop the actor from the in- or outside, this method will continue to be called.
* the loops parameter increases with every call by one, starting at zero. This may be used to
* guard against infinite looping. */
protected def process(loops: Long): Receive

/**
* Override this in your actor to process exceptions that occur while processing. The default implementation
Expand Down
9 changes: 2 additions & 7 deletions shared/src/test/scala/s2a/leucine/actors/user/refuse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,14 @@ object RefuseActorTest extends TestSuite :

class Generator(name: String, writer: Writer, val writeln: String => Unit) extends RefuseActor(RefuseStateless,name) :

var counter = 0

def process(): Unit =
def process(loops: Long): Unit =
writeln(s"$name:enter")
//writeln(s"$name:start")
writer ! Writer.Text("text1")
writer ! Writer.Number(1)
writer ! Writer.Text("text2")
writer ! Writer.Number(2)
//writeln(s"$name:stop")
writeln(s"$name:exit")
counter = counter + 1
if counter == 2 then
if loops == 1 then
writer.stop(Actor.Stop.Finish)
done()

Expand Down

0 comments on commit 2bdc756

Please sign in to comment.