parent
74c2e032c5
commit
c23a774dd8
@ -0,0 +1,45 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.arialyy.aria.core.command |
||||
|
||||
import com.arialyy.aria.core.inf.ITaskQueue |
||||
import com.arialyy.aria.core.task.ITask |
||||
|
||||
/** |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 11:06 AM 2023/1/27 |
||||
**/ |
||||
internal class CmdChain( |
||||
private val interceptors: List<ICmdInterceptor>, |
||||
private val index: Int = 0, |
||||
private val task: ITask, |
||||
private val queue: ITaskQueue<ITask> |
||||
) : ICmdInterceptor.IChain { |
||||
override fun getQueue(): ITaskQueue<ITask> { |
||||
return queue |
||||
} |
||||
|
||||
override fun getTask(): ITask { |
||||
return task |
||||
} |
||||
|
||||
override fun proceed(task: ITask): CmdResp { |
||||
val next = CmdChain(interceptors, index, task, queue) |
||||
val interceptor = interceptors[index] |
||||
return interceptor.interceptor(next) |
||||
} |
||||
} |
@ -0,0 +1,36 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.arialyy.aria.core.command |
||||
|
||||
/** |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 10:39 AM 2023/1/27 |
||||
**/ |
||||
class CmdResp(val code: Int = CODE_DEF) { |
||||
companion object { |
||||
const val CODE_COMPLETE = 1 |
||||
const val CODE_INTERRUPT = 999 |
||||
const val CODE_DEF = 0 |
||||
} |
||||
|
||||
/** |
||||
* Whether to interrupt or not |
||||
*/ |
||||
fun isInterrupt() = code == CODE_INTERRUPT |
||||
|
||||
fun isComplete() = code == CODE_COMPLETE |
||||
} |
@ -0,0 +1,38 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.arialyy.aria.core.command |
||||
|
||||
import com.arialyy.aria.core.inf.ITaskQueue |
||||
import com.arialyy.aria.core.task.ITask |
||||
|
||||
/** |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 10:37 AM 2023/1/27 |
||||
**/ |
||||
interface ICmdInterceptor { |
||||
|
||||
/** |
||||
* interceptor task |
||||
*/ |
||||
fun interceptor(chain: IChain): CmdResp |
||||
|
||||
interface IChain { |
||||
fun getQueue(): ITaskQueue<ITask> |
||||
fun getTask(): ITask |
||||
fun proceed(task: ITask): CmdResp |
||||
} |
||||
} |
@ -0,0 +1,44 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.arialyy.aria.core.command |
||||
|
||||
import com.arialyy.aria.core.command.ICmdInterceptor.IChain |
||||
import timber.log.Timber |
||||
|
||||
/** |
||||
* check task state |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 4:15 PM 2023/1/27 |
||||
**/ |
||||
internal class TaskCheckInterceptor : ICmdInterceptor { |
||||
/** |
||||
* check task state |
||||
* 1、if task already in queue, interrupt cmd |
||||
* 2、if task already complete, interrupt cmd |
||||
*/ |
||||
override fun interceptor(chain: IChain): CmdResp { |
||||
if (chain.getQueue().taskExists(chain.getTask().taskId)) { |
||||
Timber.d("task already in queue") |
||||
return CmdResp(CmdResp.CODE_INTERRUPT) |
||||
} |
||||
if (chain.getTask().taskState.isCompleted()) { |
||||
Timber.d("task already complete") |
||||
return CmdResp(CmdResp.CODE_INTERRUPT) |
||||
} |
||||
return chain.proceed(chain.getTask()) |
||||
} |
||||
} |
@ -0,0 +1,37 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.arialyy.aria.core.inf |
||||
|
||||
/** |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 10:26 PM 2023/1/26 |
||||
**/ |
||||
abstract class BaseEntity : IEntity { |
||||
val createTime: Long = System.currentTimeMillis() |
||||
|
||||
var updateTime: Long = createTime |
||||
|
||||
/** |
||||
* task state, [IEntity.STATE_WAIT] |
||||
*/ |
||||
var state: Int = IEntity.STATE_WAIT |
||||
|
||||
/** |
||||
* current progress |
||||
*/ |
||||
var progress: Long = 0L |
||||
} |
@ -0,0 +1,67 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.arialyy.aria.orm |
||||
|
||||
import com.arialyy.aria.core.inf.BaseEntity |
||||
import com.arialyy.aria.core.inf.IEntity |
||||
import timber.log.Timber |
||||
|
||||
/** |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 21:43 AM 2023/1/22 |
||||
**/ |
||||
object EntityCachePool { |
||||
/** |
||||
* key: taskId |
||||
*/ |
||||
private val entityMap = hashMapOf<Int, BaseEntity>() |
||||
|
||||
fun putEntity(taskId: Int, entity: BaseEntity) { |
||||
if (taskId <= 0) { |
||||
Timber.e("invalid taskId: ${taskId}") |
||||
return |
||||
} |
||||
entityMap[taskId] = entity |
||||
} |
||||
|
||||
/** |
||||
* get entity by taskId |
||||
*/ |
||||
fun getEntity(taskId: Int): IEntity? { |
||||
return entityMap[taskId] |
||||
} |
||||
|
||||
/** |
||||
* update entity state, if [entityMap] no cache, update fail |
||||
* @param state [IEntity] |
||||
* @param currentProgress task current progress |
||||
*/ |
||||
fun updateState(taskId: Int, state: Int, currentProgress: Long) { |
||||
if (taskId <= 0) { |
||||
Timber.e("invalid taskId: $taskId") |
||||
return |
||||
} |
||||
val cacheE = entityMap[taskId] |
||||
if (cacheE == null) { |
||||
Timber.e("update state fail, taskId not found, taskId: $taskId") |
||||
return |
||||
} |
||||
cacheE.state = state |
||||
cacheE.progress = currentProgress |
||||
cacheE.update() |
||||
} |
||||
} |
Loading…
Reference in new issue