1 /*
2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "task.h"
17
18 #include "dh_utils_tool.h"
19
20 namespace OHOS {
21 namespace DistributedHardware {
22 namespace {
23 const std::string DH_TASK_NAME_PREFIX = "Task_";
24 }
25
Task(const std::string & networkId,const std::string & uuid,const std::string & udid,const std::string & dhId,const DHType dhType)26 Task::Task(const std::string &networkId, const std::string &uuid, const std::string &udid, const std::string &dhId,
27 const DHType dhType) : id_(DH_TASK_NAME_PREFIX + GetRandomID()), networkId_(networkId), uuid_(uuid), udid_(udid),
28 dhId_(dhId), dhType_(dhType)
29 {}
30
~Task()31 Task::~Task()
32 {
33 std::vector<std::shared_ptr<Task>>().swap(this->childrenTasks_);
34 }
35
GetId()36 std::string Task::GetId()
37 {
38 return this->id_;
39 }
40
GetNetworkId()41 std::string Task::GetNetworkId()
42 {
43 return this->networkId_;
44 }
45
GetUUID()46 std::string Task::GetUUID()
47 {
48 return this->uuid_;
49 }
50
GetUDID()51 std::string Task::GetUDID()
52 {
53 return this->udid_;
54 }
55
GetDhId()56 std::string Task::GetDhId()
57 {
58 return this->dhId_;
59 }
60
GetDhType()61 DHType Task::GetDhType()
62 {
63 return this->dhType_;
64 }
65
GetTaskType()66 TaskType Task::GetTaskType()
67 {
68 return this->taskType_;
69 }
70
SetTaskType(TaskType taskType)71 void Task::SetTaskType(TaskType taskType)
72 {
73 this->taskType_ = taskType;
74 }
75
SetTaskSteps(std::vector<TaskStep> taskSteps)76 void Task::SetTaskSteps(std::vector<TaskStep> taskSteps)
77 {
78 this->taskSteps_.swap(taskSteps);
79 }
80
GetTaskSteps()81 const std::vector<TaskStep> Task::GetTaskSteps()
82 {
83 return this->taskSteps_;
84 }
85
GetTaskState()86 TaskState Task::GetTaskState()
87 {
88 return this->taskState_;
89 }
90
SetTaskState(TaskState taskState)91 void Task::SetTaskState(TaskState taskState)
92 {
93 this->taskState_ = taskState;
94 }
95
AddChildrenTask(std::shared_ptr<Task> childrenTask)96 void Task::AddChildrenTask(std::shared_ptr<Task> childrenTask)
97 {
98 std::lock_guard<std::mutex> lock(taskMtx_);
99 this->childrenTasks_.push_back(childrenTask);
100 }
101
GetChildrenTasks()102 const std::vector<std::shared_ptr<Task>> Task::GetChildrenTasks()
103 {
104 std::lock_guard<std::mutex> lock(taskMtx_);
105 return this->childrenTasks_;
106 }
107
GetFatherTask()108 const std::weak_ptr<Task> Task::GetFatherTask()
109 {
110 return this->fatherTask_;
111 }
112
SetFatherTask(std::shared_ptr<Task> fatherTask)113 void Task::SetFatherTask(std::shared_ptr<Task> fatherTask)
114 {
115 this->fatherTask_ = fatherTask;
116 }
117 } // namespace DistributedHardware
118 } // namespace OHOS
119