• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "event.h"
16 
17 namespace OHOS {
18 namespace STtools {
Event()19 Event::Event()
20 {
21     waiting_message_ = "";
22     complete_message_.clear();
23 }
24 
~Event()25 Event::~Event()
26 {
27     waiting_message_ = "";
28     std::vector<std::string> tmp_vector;
29     tmp_vector.swap(complete_message_);
30     complete_message_.clear();
31 }
32 
Compare()33 bool Event::Compare()
34 {
35     if (!waiting_message_.empty()) {
36         for (size_t i = 0; i < complete_message_.size(); i++) {
37             if (waiting_message_.compare(complete_message_.at(i)) == 0) {
38                 complete_message_.erase(std::begin(complete_message_) + i, std::begin(complete_message_) + i + 1);
39                 waiting_message_ = "";
40                 return true;
41             }
42         }
43     }
44     return false;
45 }
46 
WaitingMessage(const std::string & message,int timeout_ms,bool locked)47 int Event::WaitingMessage(const std::string &message, int timeout_ms, bool locked)
48 {
49     std::unique_lock<std::mutex> lock(mutex_);
50     HILOG_INFO(" WaitingMessage: [%{public}s]", message.c_str());
51     waiting_message_ = message;
52     if (Compare()) {
53         HILOG_INFO(" WaitingMessage: unlock [%{public}s]", message.c_str());
54         return 0;
55     }
56 
57     if (locked) {
58         HILOG_INFO(" WaitingMessage: locked [%{public}s]", message.c_str());
59         cv_.wait(lock);
60         return 0;
61     }
62 
63     if (cv_.wait_for(lock, std::chrono::seconds(timeout_ms)) == std::cv_status::timeout) {
64         HILOG_INFO("[%{public}s] waiting timeout", waiting_message_.c_str());
65         waiting_message_ = "";
66         return -1;
67     }
68     return 0;
69 }
70 
CompleteMessage(const std::string & message)71 void Event::CompleteMessage(const std::string &message)
72 {
73     std::unique_lock<std::mutex> lock(mutex_);
74     HILOG_INFO("CompleteMessage [%{public}s]", message.c_str());
75     if (waiting_message_.compare(message) == 0) {
76         HILOG_INFO("Completed unlocked: [%{public}s]", message.c_str());
77         waiting_message_ = "";
78         cv_.notify_all();
79         return;
80     }
81     HILOG_INFO("completed message: [%{public}s] does not equal waiting message", message.c_str());
82 
83     complete_message_.push_back(message);
84     return;
85 }
86 
CompleteMessage(const std::string & message,const std::string & data)87 void Event::CompleteMessage(const std::string &message, const std::string &data)
88 {
89     std::unique_lock<std::mutex> lock(mutex_);
90     HILOG_INFO("CompleteMessage [%{public}s]", message.c_str());
91     message_data_[message] = data;
92     if (waiting_message_.compare(message) == 0) {
93         HILOG_INFO("Completed unlocked: [%{public}s]", message.c_str());
94         waiting_message_ = "";
95         cv_.notify_all();
96         return;
97     }
98     HILOG_INFO("completed message: [%{public}s] does not equal waiting message", message.c_str());
99 
100     complete_message_.push_back(message);
101     return;
102 }
103 
GetData(const std::string & message)104 std::string Event::GetData(const std::string &message)
105 {
106     std::string data;
107     std::unique_lock<std::mutex> lock(mutex_);
108     if (message_data_.find(message) != message_data_.end()) {
109         data = message_data_.at(message);
110         message_data_.erase(message);
111     }
112     return data;
113 }
114 
Clean()115 void Event::Clean()
116 {
117     HILOG_INFO("Event::Clean()");
118     std::unique_lock<std::mutex> lock(mutex_);
119     waiting_message_ = "";
120     complete_message_.clear();
121     message_data_.clear();
122 }
123 }  // namespace STtools
124 }  // namespace OHOS