mirror of
https://github.com/dev-claw/vripper-project.git
synced 2026-08-19 08:35:41 +02:00
+15
-10
@@ -42,16 +42,18 @@ internal class ImageDownloadRunnable(
|
||||
class Context(val imageEntity: ImageEntity, val settings: Settings) {
|
||||
|
||||
private val log by LoggerDelegate()
|
||||
private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||
private lateinit var coroutineScope: CoroutineScope
|
||||
private val jobs = mutableListOf<Job>()
|
||||
|
||||
val httpContext: HttpClientContext = HttpClientContext.create().apply {
|
||||
cookieStore = HTTPService.cookieStore
|
||||
}
|
||||
val requests = mutableListOf<HttpUriRequestBase>()
|
||||
val headers = mutableMapOf<String, String>()
|
||||
var completed = false
|
||||
var stopped = false
|
||||
|
||||
init {
|
||||
fun init() {
|
||||
coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||
ApplicationProperties.VRIPPER_DIR.listDirectoryEntries()
|
||||
.filter { it.fileName.pathString.startsWith("cookies") }.forEach { cookiesPath ->
|
||||
loadCookies(cookiesPath).also { cookies ->
|
||||
@@ -83,6 +85,7 @@ internal class ImageDownloadRunnable(
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun clear() {
|
||||
runBlocking {
|
||||
requests.forEach { it.abort() }
|
||||
@@ -102,8 +105,6 @@ internal class ImageDownloadRunnable(
|
||||
private val vgAuthService: VGAuthService by inject()
|
||||
private val hosts: List<Host> = getKoin().getAll()
|
||||
val context = Context(imageEntity, settings)
|
||||
var completed = false
|
||||
var stopped = false
|
||||
|
||||
fun download() {
|
||||
try {
|
||||
@@ -140,7 +141,7 @@ internal class ImageDownloadRunnable(
|
||||
dataAccessService.updateImage(context.imageEntity)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
if (stopped) {
|
||||
if (context.stopped) {
|
||||
return
|
||||
}
|
||||
context.imageEntity.status = Status.ERROR
|
||||
@@ -189,20 +190,24 @@ internal class ImageDownloadRunnable(
|
||||
|
||||
override fun run() {
|
||||
try {
|
||||
if (stopped) {
|
||||
context.init()
|
||||
if (context.stopped) {
|
||||
return
|
||||
}
|
||||
download()
|
||||
} finally {
|
||||
completed = true
|
||||
context.completed = true
|
||||
context.clear()
|
||||
if (context.stopped && context.imageEntity.downloaded != context.imageEntity.size) {
|
||||
context.imageEntity.status = Status.STOPPED
|
||||
}
|
||||
dataAccessService.updateImage(context.imageEntity)
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
stopped = true
|
||||
context.stopped = true
|
||||
context.clear()
|
||||
dataAccessService.updateImage(context.imageEntity)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
|
||||
@@ -55,7 +55,7 @@ internal class QueueManager(
|
||||
val toProcess =
|
||||
if (postEntityId != null) running.filter { it.context.imageEntity.postEntityId == postEntityId } else running
|
||||
toProcess.forEach { it.stop() }
|
||||
while (toProcess.count { !it.completed } > 0) {
|
||||
while (toProcess.count { !it.context.completed } > 0) {
|
||||
Thread.sleep(100)
|
||||
}
|
||||
running.removeAll(toProcess)
|
||||
@@ -158,7 +158,7 @@ internal class QueueManager(
|
||||
downloadManagerLock.withLock {
|
||||
val image = imageDownloadRunnable.context.imageEntity
|
||||
clearRunningRunnable(imageDownloadRunnable.context.imageEntity.id)
|
||||
if (!isPending(image.postEntityId) && !isRunning(image.postEntityId) && !imageDownloadRunnable.stopped) {
|
||||
if (!isPending(image.postEntityId) && !isRunning(image.postEntityId) && !imageDownloadRunnable.context.stopped) {
|
||||
dataAccessService.finishPost(image.postEntityId, true)
|
||||
}
|
||||
reportQueueState()
|
||||
|
||||
@@ -33,9 +33,11 @@ class LoadingView : View("VRipper") {
|
||||
|
||||
init {
|
||||
DatabaseManager.connect()
|
||||
ClipboardManager.init()
|
||||
coroutineScope.launch {
|
||||
GuiEventBus.events.filterIsInstance(GuiEventBus.ApplicationInitialized::class).collect {
|
||||
runLater {
|
||||
ClipboardManager.init()
|
||||
}
|
||||
message.set("")
|
||||
AppManager.stop()
|
||||
grpcEndpointService.disconnect()
|
||||
|
||||
@@ -200,6 +200,11 @@ class LogTableView : View() {
|
||||
tableView.placeholder = Label("Loading")
|
||||
}
|
||||
|
||||
is GuiEventBus.RemoteError -> runLater {
|
||||
items.clear()
|
||||
tableView.placeholder = Label("Connection Failure")
|
||||
}
|
||||
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -516,6 +516,11 @@ class PostsTableView : View() {
|
||||
items.clear()
|
||||
}
|
||||
|
||||
is GuiEventBus.RemoteError -> runLater {
|
||||
items.clear()
|
||||
tableView.placeholder = Label("Connection Failure")
|
||||
}
|
||||
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,15 +29,15 @@ class StatusBarView : View("Status bar") {
|
||||
GuiEventBus.events.collect {
|
||||
when (it) {
|
||||
GuiEventBus.LocalSession, GuiEventBus.RemoteSession -> {
|
||||
while (isActive) {
|
||||
val result = runCatching { statusBarController.loggedInUser() }
|
||||
if (result.isSuccess) {
|
||||
runLater {
|
||||
loggedUser.set(result.getOrNull())
|
||||
}
|
||||
break
|
||||
val result = runCatching { statusBarController.loggedInUser() }
|
||||
if (result.isSuccess) {
|
||||
runLater {
|
||||
loggedUser.set(result.getOrNull())
|
||||
}
|
||||
} else {
|
||||
runLater {
|
||||
loggedUser.set(null)
|
||||
}
|
||||
delay(1000)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -214,6 +214,11 @@ class ThreadTableView : View() {
|
||||
items.clear()
|
||||
}
|
||||
|
||||
is GuiEventBus.RemoteError -> runLater {
|
||||
items.clear()
|
||||
tableView.placeholder = Label("Connection Failure")
|
||||
}
|
||||
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package me.vripper.gui.controller
|
||||
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.retryWhen
|
||||
import me.vripper.gui.model.ImageModel
|
||||
import me.vripper.gui.utils.AppEndpointManager.currentAppEndpointService
|
||||
import me.vripper.model.Image
|
||||
@@ -41,7 +39,7 @@ class ImageController : Controller() {
|
||||
}
|
||||
|
||||
fun onUpdateImages(postId: Long) =
|
||||
currentAppEndpointService().onUpdateImagesByPostEntityId(postId).retryWhen { _, _ -> delay(1000); true }
|
||||
currentAppEndpointService().onUpdateImagesByPostEntityId(postId)
|
||||
|
||||
fun onStopped() = currentAppEndpointService().onStopped().retryWhen { _, _ -> delay(1000);true }
|
||||
fun onStopped() = currentAppEndpointService().onStopped()
|
||||
}
|
||||
@@ -12,7 +12,7 @@ class StatusBarController : Controller() {
|
||||
}
|
||||
|
||||
fun connectionState(): String {
|
||||
return currentAppEndpointService().connectionState()
|
||||
return runCatching { currentAppEndpointService().connectionState() }.getOrElse { "" }
|
||||
}
|
||||
|
||||
suspend fun getVersion(): String {
|
||||
|
||||
@@ -17,6 +17,8 @@ object GuiEventBus {
|
||||
object ChangingSession : GUIEvent
|
||||
object LocalSession : GUIEvent
|
||||
object RemoteSession : GUIEvent
|
||||
object Undefined : GUIEvent
|
||||
data class RemoteError(val source: String, val message: String) : GUIEvent
|
||||
}
|
||||
|
||||
sealed interface GUIEvent
|
||||
@@ -412,5 +412,5 @@ internal class GrpcEndpointService : IAppEndpointService {
|
||||
}
|
||||
}
|
||||
|
||||
override fun connectionState(): String = channel?.getState(true)?.name ?: ConnectivityState.SHUTDOWN.name
|
||||
override fun connectionState(): String = channel?.getState(false)?.name ?: ConnectivityState.SHUTDOWN.name
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ object AppEndpointManager : KoinComponent {
|
||||
val localAppEndpointService: IAppEndpointService by inject(named("localAppEndpointService"))
|
||||
val remoteAppEndpointService: IAppEndpointService by inject(named("remoteAppEndpointService"))
|
||||
|
||||
private lateinit var current: GUIEvent
|
||||
private var current: GUIEvent = GuiEventBus.Undefined
|
||||
|
||||
fun set(event: GUIEvent) {
|
||||
this.current = event
|
||||
|
||||
@@ -4,12 +4,11 @@ import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.cancellable
|
||||
import kotlinx.coroutines.flow.channelFlow
|
||||
import kotlinx.coroutines.flow.retryWhen
|
||||
import me.vripper.gui.event.GuiEventBus
|
||||
|
||||
object ChannelFlowBuilder {
|
||||
|
||||
fun <T> build(localFlow: () -> Flow<T>, remoteFlow: () -> Flow<T>): Flow<T> {
|
||||
fun <T> build(localFlow: () -> Flow<T>, remoteFlow: () -> Flow<T>, source: String = "remote"): Flow<T> {
|
||||
return channelFlow {
|
||||
var job: Job? = null
|
||||
|
||||
@@ -20,10 +19,14 @@ object ChannelFlowBuilder {
|
||||
}
|
||||
} else {
|
||||
launch {
|
||||
remoteFlow().cancellable().retryWhen { _, _ ->
|
||||
delay(1000)
|
||||
true
|
||||
}.collect { if (isActive) send(it) }
|
||||
try {
|
||||
remoteFlow().cancellable().collect { if (isActive) send(it) }
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (t: Throwable) {
|
||||
t.printStackTrace()
|
||||
GuiEventBus.publishEvent(GuiEventBus.RemoteError(source, t.message ?: t.toString()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ package me.vripper.gui.utils
|
||||
|
||||
import javafx.scene.input.Clipboard
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.retryWhen
|
||||
import me.vripper.gui.event.GuiEventBus
|
||||
import me.vripper.model.Settings
|
||||
import me.vripper.services.IAppEndpointService
|
||||
import me.vripper.utilities.LoggerDelegate
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
import org.koin.core.qualifier.named
|
||||
import tornadofx.runLater
|
||||
|
||||
object ClipboardManager : KoinComponent {
|
||||
@@ -14,35 +16,44 @@ object ClipboardManager : KoinComponent {
|
||||
private var current: String? = null
|
||||
private var coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
private var pollJob: Job? = null
|
||||
private var settingsUpdateJob: Job? = null
|
||||
private val systemClipboard = Clipboard.getSystemClipboard()
|
||||
val localAppEndpointService: IAppEndpointService by inject(named("localAppEndpointService"))
|
||||
val remoteAppEndpointService: IAppEndpointService by inject(named("remoteAppEndpointService"))
|
||||
var initialized = false
|
||||
|
||||
@Synchronized
|
||||
fun init() {
|
||||
coroutineScope.launch {
|
||||
GuiEventBus.events.collect { event ->
|
||||
when (event) {
|
||||
GuiEventBus.LocalSession, GuiEventBus.RemoteSession -> {
|
||||
logger.info("Clipboard manager initialized")
|
||||
while (isActive) {
|
||||
if (initialized) {
|
||||
return
|
||||
}
|
||||
runBlocking {
|
||||
val updateSettings = ChannelFlowBuilder.build(
|
||||
localAppEndpointService::onUpdateSettings,
|
||||
remoteAppEndpointService::onUpdateSettings
|
||||
)
|
||||
updateSettings.let { flow ->
|
||||
coroutineScope.launch {
|
||||
flow.collect {
|
||||
update(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
coroutineScope.launch {
|
||||
GuiEventBus.events.collect { event ->
|
||||
when (event) {
|
||||
GuiEventBus.LocalSession, GuiEventBus.RemoteSession -> {
|
||||
logger.info("Clipboard manager initialized")
|
||||
val result = runCatching { AppEndpointManager.currentAppEndpointService().getSettings() }
|
||||
if (result.isSuccess) {
|
||||
update(result.getOrNull()!!)
|
||||
break
|
||||
}
|
||||
delay(1000)
|
||||
}
|
||||
settingsUpdateJob?.cancelAndJoin()
|
||||
settingsUpdateJob = launch {
|
||||
AppEndpointManager.currentAppEndpointService().onUpdateSettings()
|
||||
.retryWhen { _, _ -> delay(1000); true }.collect {
|
||||
update(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else -> {}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
initialized = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user