1 /*
2  * Copyright (c) 2023-2023 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 "command_server_impl.h"
17 
18 #include "dp_log.h"
19 
20 namespace OHOS {
21 namespace CameraStandard {
22 namespace DeferredProcessing {
23 namespace {
24     constexpr uint32_t MAX_THREAD_NUM = 1;
25     constexpr int32_t DEFAULT_PRIORITY = 0;
26 }
27 
CommandServerImpl(const std::string & cmdServerName)28 CommandServerImpl::CommandServerImpl(const std::string& cmdServerName)
29     : commandServerName_(cmdServerName)
30 {
31     DP_DEBUG_LOG("entered.");
32     threadPool_ = ThreadPool::Create(commandServerName_, MAX_THREAD_NUM);
33 }
34 
~CommandServerImpl()35 CommandServerImpl::~CommandServerImpl()
36 {
37     DP_DEBUG_LOG("entered.");
38     threadPool_.reset();
39 }
40 
AddCommand(const CmdSharedPtr & cmd)41 int32_t CommandServerImpl::AddCommand(const CmdSharedPtr& cmd)
42 {
43     DP_CHECK_ERROR_RETURN_RET_LOG(threadPool_ == nullptr, DP_NOT_AVAILABLE, "threadPool is nullptr.");
44     if (threadPool_->Submit([cmd]() {cmd->Do();})) {
45         return DP_OK;
46     }
47     DP_ERR_LOG("dps command server not start.");
48     return DP_NOT_AVAILABLE;
49 }
50 
AddUrgentCommand(const CmdSharedPtr & cmd)51 int32_t CommandServerImpl::AddUrgentCommand(const CmdSharedPtr& cmd)
52 {
53     DP_CHECK_ERROR_RETURN_RET_LOG(threadPool_ == nullptr, DP_NOT_AVAILABLE, "threadPool is nullptr.");
54     if (threadPool_->Submit([cmd]() {cmd->Do();}, true)) {
55         return DP_OK;
56     }
57     DP_ERR_LOG("dps command server not start.");
58     return DP_NOT_AVAILABLE;
59 }
60 
SetThreadPriority(int32_t priority)61 void CommandServerImpl::SetThreadPriority(int32_t priority)
62 {
63     DP_CHECK_ERROR_RETURN_LOG(threadPool_ == nullptr, "threadPool is nullptr.");
64     threadPool_->SetThreadPoolPriority(priority);
65 }
66 
GetThreadPriority() const67 int32_t CommandServerImpl::GetThreadPriority() const
68 {
69     DP_CHECK_ERROR_RETURN_RET_LOG(threadPool_ == nullptr, DEFAULT_PRIORITY, "threadPool is nullptr.");
70     return threadPool_->GetThreadPoolPriority();
71 }
72 } // namespace DeferredProcessing
73 } // namespace CameraStandard
74 } // namespace OHOS