parent
1415de11c6
commit
e0c9b0c373
@ -0,0 +1,51 @@ |
||||
/* |
||||
* 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.http.download |
||||
|
||||
import com.arialyy.aria.core.inf.IBlockManager |
||||
|
||||
/** |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 11:37 2023/3/12 |
||||
**/ |
||||
internal interface ITaskAdapterDelegate { |
||||
/** |
||||
* 任务是否正在执行 |
||||
* |
||||
* @return `true` 任务正在执行 |
||||
*/ |
||||
fun isRunning(): Boolean |
||||
|
||||
/** |
||||
* 取消 |
||||
*/ |
||||
fun cancel() |
||||
|
||||
/** |
||||
* 停止 |
||||
*/ |
||||
fun stop() |
||||
|
||||
/** |
||||
* 开始 |
||||
*/ |
||||
fun start() |
||||
|
||||
fun setBlockManager(blockManager: IBlockManager) |
||||
|
||||
fun getBlockManager(): IBlockManager |
||||
} |
@ -0,0 +1,100 @@ |
||||
/* |
||||
* 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.http.download |
||||
|
||||
import android.os.Looper |
||||
import com.arialyy.aria.core.DuaContext |
||||
import com.arialyy.aria.core.inf.IBlockManager |
||||
import com.arialyy.aria.core.task.DBlockManager |
||||
import com.arialyy.aria.core.task.TaskResp |
||||
import com.arialyy.aria.core.task.ThreadTaskManager2 |
||||
import com.arialyy.aria.exception.AriaException |
||||
import com.arialyy.aria.http.HttpTaskOption |
||||
import kotlinx.coroutines.Dispatchers |
||||
import kotlinx.coroutines.launch |
||||
import timber.log.Timber |
||||
|
||||
/** |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 11:32 2023/3/12 |
||||
**/ |
||||
internal class SingleTaskDelegate(val adapter: HttpDTaskAdapter) : ITaskAdapterDelegate { |
||||
private val blockManager = DBlockManager(adapter.getTask()) |
||||
|
||||
init { |
||||
ThreadTaskManager2.putThreadManager(adapter.getTask().taskId, blockManager) |
||||
} |
||||
|
||||
override fun isRunning(): Boolean { |
||||
return blockManager.isRunning() |
||||
} |
||||
|
||||
override fun cancel() { |
||||
if (blockManager.isCanceled()) { |
||||
Timber.w("task already canceled, taskId: ${adapter.getTask().taskId}") |
||||
return |
||||
} |
||||
DuaContext.duaScope.launch(Dispatchers.IO) { |
||||
ThreadTaskManager2.stopThreadTask(adapter.getTask().taskId, true) |
||||
} |
||||
} |
||||
|
||||
override fun stop() { |
||||
if (blockManager.isStopped()) { |
||||
Timber.w("task already stopped, taskId: ${adapter.getTask().taskId}") |
||||
return |
||||
} |
||||
DuaContext.duaScope.launch(Dispatchers.IO) { |
||||
ThreadTaskManager2.stopThreadTask(adapter.getTask().taskId) |
||||
} |
||||
} |
||||
|
||||
override fun start() { |
||||
adapter.getTask().getTaskOption(HttpTaskOption::class.java).taskInterceptor.let { |
||||
if (it.isNotEmpty()) { |
||||
adapter.addInterceptors(it) |
||||
} |
||||
} |
||||
DuaContext.duaScope.launch(Dispatchers.IO) { |
||||
Looper.prepare() |
||||
blockManager.setLooper() |
||||
adapter.addCoreInterceptor(HttpDCheckInterceptor()) |
||||
adapter.addCoreInterceptor(TimerInterceptor()) |
||||
adapter.addCoreInterceptor(HttpDHeaderInterceptor()) |
||||
adapter.addCoreInterceptor(HttpDBlockInterceptor()) |
||||
adapter.addCoreInterceptor(HttpBlockThreadInterceptor()) |
||||
val resp = adapter.interceptor() |
||||
if (resp == null || resp.code != TaskResp.CODE_SUCCESS) { |
||||
adapter.getTask().getTaskOption(HttpTaskOption::class.java).eventListener.onFail( |
||||
false, |
||||
AriaException("start task fail, task interrupt, code: ${resp?.code ?: TaskResp.CODE_INTERRUPT}") |
||||
) |
||||
blockManager.stop() |
||||
return@launch |
||||
} |
||||
Looper.loop() |
||||
} |
||||
} |
||||
|
||||
override fun setBlockManager(blockManager: IBlockManager) { |
||||
throw UnsupportedOperationException("Single task does not support setting up a block manager") |
||||
} |
||||
|
||||
override fun getBlockManager(): IBlockManager { |
||||
return blockManager |
||||
} |
||||
} |
@ -0,0 +1,51 @@ |
||||
/* |
||||
* 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.http.download |
||||
|
||||
import com.arialyy.aria.core.inf.IBlockManager |
||||
|
||||
/** |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 11:35 2023/3/12 |
||||
**/ |
||||
internal class SubTaskDelegate(val adapter: HttpDTaskAdapter) : ITaskAdapterDelegate { |
||||
private lateinit var blockManager: IBlockManager |
||||
|
||||
override fun isRunning(): Boolean { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun cancel() { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun stop() { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun start() { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun setBlockManager(blockManager: IBlockManager) { |
||||
this.blockManager = blockManager |
||||
} |
||||
|
||||
override fun getBlockManager(): IBlockManager { |
||||
return blockManager |
||||
} |
||||
} |
@ -0,0 +1,73 @@ |
||||
/* |
||||
* 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.dua.group |
||||
|
||||
import com.arialyy.aria.core.common.TaskOption |
||||
import com.arialyy.aria.core.task.ITask |
||||
import com.arialyy.aria.core.task.ITaskInterceptor |
||||
import com.arialyy.aria.core.task.SingleDownloadTask |
||||
import com.arialyy.aria.core.task.TaskChain |
||||
import com.arialyy.aria.core.task.TaskResp |
||||
import com.arialyy.aria.http.HttpTaskOption |
||||
import com.arialyy.aria.http.download.HttpDTaskAdapter |
||||
import com.arialyy.aria.orm.entity.DGEntity |
||||
|
||||
/** |
||||
* Subtasks do not support chunking |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 08:51 2023/3/12 |
||||
**/ |
||||
internal class HttpDGSubTaskInterceptor : ITaskInterceptor { |
||||
private lateinit var task: ITask |
||||
private lateinit var optionAdapter: HttpDGOptionAdapter |
||||
private lateinit var taskOption: HttpTaskOption |
||||
|
||||
override suspend fun interceptor(chain: TaskChain): TaskResp { |
||||
task = chain.getTask() |
||||
taskOption = task.getTaskOption(HttpTaskOption::class.java) |
||||
optionAdapter = taskOption.getOptionAdapter(HttpDGOptionAdapter::class.java) |
||||
val subList = createSubTask(chain) |
||||
|
||||
|
||||
return TaskResp(TaskResp.CODE_SUCCESS) |
||||
} |
||||
|
||||
private fun startSubTask(subList: List<SingleDownloadTask>) { |
||||
subList.forEach { |
||||
|
||||
} |
||||
} |
||||
|
||||
private fun createSubTask(chain: TaskChain): List<SingleDownloadTask> { |
||||
val subTaskList = mutableListOf<SingleDownloadTask>() |
||||
task.taskState.getEntity(DGEntity::class.java).subList.forEach { |
||||
val tp = TaskOption() |
||||
tp.sourUrl = it.sourceUrl |
||||
tp.savePathDir = taskOption.savePathDir |
||||
tp.threadNum = 1 |
||||
tp.eventListener = HttpSubListener() |
||||
val subTask = SingleDownloadTask(tp) |
||||
val subAdapter = HttpDTaskAdapter(true) |
||||
subAdapter.setBlockManager(HttpSubBlockManager(chain.blockManager.handler)) |
||||
|
||||
subTask.adapter = subAdapter |
||||
subTaskList.add(subTask) |
||||
} |
||||
return subTaskList |
||||
} |
||||
|
||||
} |
@ -0,0 +1,52 @@ |
||||
/* |
||||
* 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.dua.group |
||||
|
||||
import android.os.Handler |
||||
import com.arialyy.aria.core.inf.IBlockManager |
||||
import com.arialyy.aria.core.task.IThreadTask |
||||
import com.arialyy.aria.orm.entity.BlockRecord |
||||
|
||||
/** |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 10:04 2023/3/12 |
||||
**/ |
||||
class HttpSubBlockManager(val handler: Handler) : IBlockManager { |
||||
private val unfinishedBlock = mutableListOf<BlockRecord>() |
||||
private var blockNum: Int = 1 |
||||
|
||||
override fun putUnfinishedBlock(record: BlockRecord) { |
||||
unfinishedBlock.add(record) |
||||
} |
||||
|
||||
override fun getUnfinishedBlockList(): List<BlockRecord> { |
||||
return unfinishedBlock |
||||
} |
||||
|
||||
override fun getHandler() = handler |
||||
|
||||
override fun start(threadTaskList: List<IThreadTask>) { |
||||
threadTaskList.forEach { tt -> |
||||
tt.run() |
||||
} |
||||
} |
||||
|
||||
override fun setBlockNum(blockNum: Int) { |
||||
this.blockNum = blockNum |
||||
} |
||||
|
||||
} |
@ -0,0 +1,58 @@ |
||||
/* |
||||
* 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.dua.group |
||||
|
||||
import com.arialyy.aria.core.listener.IEventListener |
||||
import com.arialyy.aria.exception.AriaException |
||||
|
||||
/** |
||||
* @Author laoyuyu |
||||
* @Description |
||||
* @Date 10:13 2023/3/12 |
||||
**/ |
||||
internal class HttpSubListener : IEventListener { |
||||
override fun onPre() { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun onStart(startLocation: Long) { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun onResume(resumeLocation: Long) { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun onProgress(currentLocation: Long) { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun onStop(stopLocation: Long) { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun onComplete() { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun onCancel() { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun onFail(needRetry: Boolean, e: AriaException?) { |
||||
TODO("Not yet implemented") |
||||
} |
||||
} |
Loading…
Reference in new issue