1import router from '@ohos.router'; 2import request from '@ohos.request'; 3import common from '@ohos.app.ability.common'; 4import taskpool from '@ohos.taskpool'; 5import systemDateTime from '@ohos.systemDateTime'; 6 7let context = getContext(this) as common.UIAbilityContext; 8 9@Entry 10@Component 11struct download { 12 @State message: string = ''; 13 @State prgValue: number = 1; 14 @State prgTotal: number = 100; 15 @State taskState: string = '未开始'; 16 @State timeCard: string = "0"; 17 @State mode: request.agent.Mode = request.agent.Mode.FOREGROUND; 18 @State method: string = 'GET'; 19 @State overwrite: boolean = true; 20 @State downloadUrl: string = 'http://192.168.3.2:35620'; 21 @State retry: boolean = true; 22 @State priority: number = 0; 23 @State startTime: number = 0; 24 @State endTime: number = 0; 25 controller: TextAreaController = new TextAreaController(); 26 private context = getContext(this) as common.UIAbilityContext; 27 private task: undefined|request.agent.Task; 28 taskGroup: taskpool.TaskGroup = new taskpool.TaskGroup(); 29 @State config: request.agent.Config = { 30 action: request.agent.Action.DOWNLOAD, 31 url: 'http://192.168.232.162:35620', 32 title: 'taskpool_test', 33 description: 'taskpool_test', 34 saveas: './', 35 mode: request.agent.Mode.FOREGROUND, 36 overwrite: true, 37 retry: true, 38 }; 39 40 build() { 41 Row() { 42 Column() { 43 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center}){ 44 Row(){ 45 TextInput({ text: this.downloadUrl, placeholder: 'input your word...', controller: this.controller }) 46 .placeholderColor(Color.Grey) 47 .placeholderFont({ size: 14, weight: 400 }) 48 .caretColor(Color.Blue) 49 .width('95%') 50 .height(40) 51 .fontSize(14) 52 .fontColor(Color.Black) 53 .onChange((value: string) => { 54 this.config.url = value 55 }) 56 } 57 58 Row(){ 59 Button('创建并发任务') 60 .fontSize(20) 61 .fontWeight(FontWeight.Bold) 62 .onClick(async ()=>{ 63 this.taskAdd(); 64 }) 65 66 Button('开始并发任务') 67 .fontSize(20) 68 .fontWeight(FontWeight.Bold) 69 .onClick(async ()=>{ 70 this.startTime = systemDateTime.getTime(); 71 this.message += `====>任务开始时间:${this.startTime} (ms)\n` 72 this.TaskGroupExec(); 73 }) 74 }.margin({ top: '1%'}).width('100%').align(Alignment.Center) 75 76 TextArea({ 77 text: this.message, 78 placeholder: '默认全量订阅', 79 controller: this.controller 80 }) 81 .placeholderColor(Color.Grey) 82 .placeholderFont({ size: 14, weight: 400 }) 83 .caretColor(Color.Blue) 84 .width(400) 85 .height('70%') 86 .margin(20) 87 .fontSize(14) 88 .focusable(false) 89 .fontColor(Color.Black) 90 .onChange((value: string) => { 91 this.message = value; 92 this.controller.caretPosition(this.message.length) 93 }) 94 .margin({top:'5%'}) 95 } 96 } 97 .width('100%') 98 } 99 .height('100%') 100 } 101 102 responseCallback = (response: request.agent.HttpResponse)=>{ 103 response.headers.forEach((value, key) => { 104 console.info(`====> key:${key} value:${value}`) 105 }); 106 this.message += `====>response.statusCode: ${response.statusCode}\n` 107 this.message += `====>response.headers.get('content-type'): ${response.headers.get('content-type')}\n` 108 109 } 110 111 progressCallback = (progress: request.agent.Progress) => { 112 this.prgValue = progress.processed; 113 this.prgTotal = progress.sizes[0] 114 console.info('====>on_progress progress.processed:' + progress.processed) 115 } 116 117 completedCallback = (progress: request.agent.Progress) => { 118 console.info('====>on_complete_callback:' + JSON.stringify(progress)) 119 this.message += `====>completed progress.state: ${progress.state}\n` 120 } 121 122 failedCallback = (progress: request.agent.Progress) => { 123 console.info('====>on_failed_callback:' +JSON.stringify(progress)) 124 this.message += `====>failed progress.state: ${progress.state}\n` 125 } 126 127 pauseCallback = (progress: request.agent.Progress) => { 128 console.info('====>on_pause_callback:' +JSON.stringify(progress)) 129 this.message += `====>pause progress.state: ${progress.state}\n` 130 } 131 132 removeCallback = (progress: request.agent.Progress) => { 133 console.info('====>on_remove_callback:' +JSON.stringify(progress)) 134 this.message += `====>remove progress.state: ${progress.state}\n` 135 } 136 137 async taskAdd(): Promise<void> { 138 try { 139 let task1: taskpool.Task = new taskpool.Task(createTask, this.context, 1, this.downloadUrl); 140 this.taskGroup.addTask(task1); 141 142 let task2: taskpool.Task = new taskpool.Task(createTask, this.context, 2, this.downloadUrl); 143 this.taskGroup.addTask(task2); 144 145 let task3: taskpool.Task = new taskpool.Task(createTask, this.context, 3, this.downloadUrl); 146 this.taskGroup.addTask(task3); 147 148 let task4: taskpool.Task = new taskpool.Task(createTask, this.context, 4, this.downloadUrl); 149 this.taskGroup.addTask(task4); 150 151 let task5: taskpool.Task = new taskpool.Task(createTask, this.context, 5, this.downloadUrl); 152 this.taskGroup.addTask(task5); 153 } catch (err) { 154 console.info('====>taskAdd throw err:' + JSON.stringify(err.message)) 155 } 156 } 157 158 async TaskGroupExec(): Promise<void> { 159 try { 160 let res: string[] = await taskpool.execute(this.taskGroup) as string[]; 161 this.endTime = systemDateTime.getTime(); 162 this.message += `====>任务结束时间:${this.endTime} (ms)\n` 163 this.message += `====>任务用时:${this.endTime- this.startTime} (ms)\n` 164 } catch(err) { 165 console.info('====>TaskGroupExec throw err:' + JSON.stringify(err.message)) 166 } 167 } 168 169} 170 171@Concurrent 172async function createTask(context: common.Context, index: number, downloadUrl: string) { 173 let downloadFilePath = context.cacheDir 174 try{ 175 let downloadConfig: request.DownloadConfig = { 176 url: downloadUrl, 177 header: { 178 headers: 'http' 179 }, 180 enableMetered: false, 181 enableRoaming: false, 182 description: 'XTS download test!', 183 networkType: request.NETWORK_WIFI, 184 filePath: `${downloadFilePath}/`, 185 title: 'XTS download test!', 186 background: false 187 } 188 for(let task_index=1; task_index<=240; task_index++){ 189 downloadConfig.filePath = `${downloadFilePath}/taskpool_${index}_${task_index}.txt` 190 console.info('====>task.config:' + JSON.stringify(downloadConfig)) 191 let task = await request.downloadFile(context, downloadConfig) 192 } 193 194 }catch(err){ 195 console.info('====>createTask catch err:' + JSON.stringify(err.message)) 196 } 197}