From 497b88f0d301d704231bcea7042abb36e278b5a0 Mon Sep 17 00:00:00 2001 From: hrj Date: Thu, 24 Feb 2022 20:06:20 +0530 Subject: [PATCH 1/2] simplify logic for background task Signed-off-by: hrj --- src/main/scala/lc/background/taskThread.scala | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/scala/lc/background/taskThread.scala b/src/main/scala/lc/background/taskThread.scala index e9bd151..4489eae 100644 --- a/src/main/scala/lc/background/taskThread.scala +++ b/src/main/scala/lc/background/taskThread.scala @@ -18,13 +18,17 @@ class BackgroundTask(config: Config, captcha: Captcha) { val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt challengeGCPstmt.executeUpdate() - val imageNum = Statements.tlStmts.get.getCountChallengeTable.executeQuery() - var throttleIn = (config.throttle * 1.1).toInt - if (imageNum.next()) - throttleIn = (throttleIn - imageNum.getInt("total")) - while (0 < throttleIn) { + val imageNumResult = Statements.tlStmts.get.getCountChallengeTable.executeQuery() + val imageNum = if (imageNumResult.next()) { + imageNumResult.getInt("total") + } else { + 0 + } + + val throttle = (config.throttle * 1.1).toInt - imageNum + + for (i <- 0 until throttle) { captcha.generateChallenge(getRandomParam()) - throttleIn -= 1 } } catch { case exception: Exception => println(exception) } } From 52b071d680461ec4836e3c9ceec368e2999c1097 Mon Sep 17 00:00:00 2001 From: hrj Date: Thu, 24 Feb 2022 20:11:00 +0530 Subject: [PATCH 2/2] renamed Captcha to CaptchaManager Signed-off-by: hrj --- src/main/scala/lc/Main.scala | 8 ++++---- src/main/scala/lc/background/taskThread.scala | 6 +++--- .../lc/core/{captcha.scala => captchaManager.scala} | 2 +- src/main/scala/lc/server/Server.scala | 10 +++++----- 4 files changed, 13 insertions(+), 13 deletions(-) rename src/main/scala/lc/core/{captcha.scala => captchaManager.scala} (98%) diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index 50c1ad7..f5a94bd 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -1,6 +1,6 @@ package lc -import lc.core.{CaptchaProviders, Captcha, Config} +import lc.core.{CaptchaProviders, CaptchaManager, Config} import lc.server.Server import lc.background.BackgroundTask import lc.database.Statements @@ -15,13 +15,13 @@ object LCFramework { val config = new Config(configFilePath) Statements.maxAttempts = config.maxAttempts val captchaProviders = new CaptchaProviders(config = config) - val captcha = new Captcha(config = config, captchaProviders = captchaProviders) - val backgroundTask = new BackgroundTask(config = config, captcha = captcha) + val captchaManager = new CaptchaManager(config = config, captchaProviders = captchaProviders) + val backgroundTask = new BackgroundTask(config = config, captchaManager = captchaManager) backgroundTask.beginThread(delay = config.threadDelay) val server = new Server( address = config.address, port = config.port, - captcha = captcha, + captchaManager = captchaManager, playgroundEnabled = config.playgroundEnabled, corsHeader = config.corsHeader ) diff --git a/src/main/scala/lc/background/taskThread.scala b/src/main/scala/lc/background/taskThread.scala index 4489eae..6df95a5 100644 --- a/src/main/scala/lc/background/taskThread.scala +++ b/src/main/scala/lc/background/taskThread.scala @@ -2,11 +2,11 @@ package lc.background import lc.database.Statements import java.util.concurrent.{ScheduledThreadPoolExecutor, TimeUnit} -import lc.core.{Captcha, Config} +import lc.core.{CaptchaManager, Config} import lc.core.{Parameters, Size} import lc.misc.HelperFunctions -class BackgroundTask(config: Config, captcha: Captcha) { +class BackgroundTask(config: Config, captchaManager: CaptchaManager) { private val task = new Runnable { def run(): Unit = { @@ -28,7 +28,7 @@ class BackgroundTask(config: Config, captcha: Captcha) { val throttle = (config.throttle * 1.1).toInt - imageNum for (i <- 0 until throttle) { - captcha.generateChallenge(getRandomParam()) + captchaManager.generateChallenge(getRandomParam()) } } catch { case exception: Exception => println(exception) } } diff --git a/src/main/scala/lc/core/captcha.scala b/src/main/scala/lc/core/captchaManager.scala similarity index 98% rename from src/main/scala/lc/core/captcha.scala rename to src/main/scala/lc/core/captchaManager.scala index 5c5f11e..290c95f 100644 --- a/src/main/scala/lc/core/captcha.scala +++ b/src/main/scala/lc/core/captchaManager.scala @@ -6,7 +6,7 @@ import java.io.ByteArrayInputStream import java.sql.{Blob, ResultSet} import java.util.UUID -class Captcha(config: Config, captchaProviders: CaptchaProviders) { +class CaptchaManager(config: Config, captchaProviders: CaptchaProviders) { def getCaptcha(id: Id): Either[Error, Image] = { val blob = getImage(id.id) diff --git a/src/main/scala/lc/server/Server.scala b/src/main/scala/lc/server/Server.scala index 54af9b7..3596b32 100644 --- a/src/main/scala/lc/server/Server.scala +++ b/src/main/scala/lc/server/Server.scala @@ -2,7 +2,7 @@ package lc.server import org.json4s.jackson.JsonMethods.parse import org.json4s.jvalue2extractable -import lc.core.Captcha +import lc.core.CaptchaManager import lc.core.ErrorMessageEnum import lc.core.{Answer, ByteConvert, Error, Id, Parameters} import lc.core.Config.formats @@ -13,7 +13,7 @@ import java.net.InetSocketAddress import java.util import scala.jdk.CollectionConverters._ -class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Boolean, corsHeader: String) { +class Server(address: String, port: Int, captchaManager: CaptchaManager, playgroundEnabled: Boolean, corsHeader: String) { var headerMap: util.Map[String, util.List[String]] = _ if (corsHeader.nonEmpty) { headerMap = Map("Access-Control-Allow-Origin" -> List(corsHeader).asJava).asJava @@ -27,7 +27,7 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo (request) => { val json = parse(request.getBodyString()) val param = json.extract[Parameters] - val id = captcha.getChallenge(param) + val id = captchaManager.getChallenge(param) getResponse(id, headerMap) } ) @@ -38,7 +38,7 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo val result = if (params.containsKey("id")) { val paramId = params.get("id").get(0) val id = Id(paramId) - captcha.getCaptcha(id) + captchaManager.getCaptcha(id) } else { Left(Error(ErrorMessageEnum.INVALID_PARAM.toString + "=> id")) } @@ -50,7 +50,7 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo (request) => { val json = parse(request.getBodyString()) val answer = json.extract[Answer] - val result = captcha.checkAnswer(answer) + val result = captchaManager.checkAnswer(answer) getResponse(result, headerMap) } )