-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
317 additions
and
101 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
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
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
55 changes: 55 additions & 0 deletions
55
core/src/main/scala/com/karasiq/shadowcloud/ui/ChallengeHub.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,55 @@ | ||
package com.karasiq.shadowcloud.ui | ||
|
||
import java.time.Instant | ||
import java.util.UUID | ||
|
||
import akka.actor.ActorSystem | ||
import akka.event.Logging | ||
import akka.util.ByteString | ||
import com.karasiq.shadowcloud.ui.Challenge.AnswerFormat | ||
|
||
import scala.collection.concurrent.TrieMap | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.{Future, Promise, TimeoutException} | ||
|
||
class ChallengeHub(implicit as: ActorSystem) { | ||
import as.dispatcher | ||
private[this] val log = Logging(as, classOf[ChallengeHub]) | ||
private[this] val challenges = TrieMap.empty[UUID, (Challenge, Promise[ByteString], Deadline)] | ||
private[this] val schedule = as.scheduler.scheduleAtFixedRate(1 second, 1 second) { () ⇒ | ||
challenges.values.collect { | ||
case (challenge, promise, deadline) if deadline.isOverdue() ⇒ | ||
log.warning("Challenge timed out: {}", challenge.title) | ||
promise.tryFailure(new TimeoutException(s"Challenge timed out: ${challenge.title}")) | ||
} | ||
} | ||
|
||
def list(): Seq[Challenge] = | ||
challenges.values.map(_._1).toVector.sortBy(_.time.toEpochMilli) | ||
|
||
def create( | ||
title: String, | ||
html: String = "", | ||
answerFormat: AnswerFormat = AnswerFormat.String, | ||
deadline: FiniteDuration = 5 minutes | ||
): Future[ByteString] = { | ||
val challenge = Challenge(UUID.randomUUID(), Instant.now(), title, html, answerFormat) | ||
val promise = Promise[ByteString] | ||
log.info("Challenge created: {}", title) | ||
challenges(challenge.id) = (challenge, promise, Deadline.now + deadline) | ||
promise.future.onComplete(_ ⇒ challenges -= challenge.id) | ||
promise.future | ||
} | ||
|
||
def solve(id: UUID, response: ByteString = ByteString.empty): Unit = challenges.remove(id).foreach { | ||
case (challenge, promise, _) ⇒ | ||
log.info("Challenge solved: {}", challenge.title) | ||
promise.trySuccess(response) | ||
} | ||
|
||
override def finalize(): Unit = { | ||
schedule.cancel() | ||
challenges.values.foreach { case (_, p, _) ⇒ p.tryFailure(new RuntimeException("Challenge hub closed")) } | ||
super.finalize() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
model/src/main/scala/com/karasiq/shadowcloud/ui/Challenge.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,19 @@ | ||
package com.karasiq.shadowcloud.ui | ||
|
||
import java.time.Instant | ||
import java.util.UUID | ||
|
||
import com.karasiq.shadowcloud.model.SCEntity | ||
import com.karasiq.shadowcloud.ui.Challenge.AnswerFormat | ||
|
||
@SerialVersionUID(0L) | ||
final case class Challenge(id: UUID, time: Instant, title: String, html: String, answerFormat: AnswerFormat) extends SCEntity | ||
|
||
object Challenge { | ||
sealed trait AnswerFormat | ||
object AnswerFormat { | ||
case object Ack extends AnswerFormat | ||
case object String extends AnswerFormat | ||
case object Binary extends AnswerFormat | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.