1 /*
2  * Copyright (c) 2024 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 "AppTimerAdapter.h"
17 
AppTimerAdapter(int userId,ISceneTimerInfrastructure * infra)18 AppTimerAdapter::AppTimerAdapter(int userId, ISceneTimerInfrastructure* infra) : userId(userId)
19 {
20     this->impl = infra;
21     impl->RegUser(userId, this);
22 }
23 
~AppTimerAdapter()24 AppTimerAdapter::~AppTimerAdapter()
25 {
26     impl->UnRegUser(userId);
27 }
28 
Start(std::string key)29 void AppTimerAdapter::Start(std::string key)
30 {
31     std::lock_guard<std::mutex> uniqueLock(mut);
32     int id = AssignId(key);
33     ValidateDuplication(id);
34     impl->Start(userId, id, timeoutPeriod);
35     sessions.push_back(id);
36 }
37 
Stop(std::string key)38 void AppTimerAdapter::Stop(std::string key)
39 {
40     std::lock_guard<std::mutex> uniqueLock(mut);
41     int id = KeyToId(key);
42     ValidateExistence(id);
43     impl->Stop(userId, id);
44     sessions.erase(std::find(sessions.begin(), sessions.end(), id));
45     RecycleId(id);
46 }
47 
Expired(int id)48 void AppTimerAdapter::Expired(int id)
49 {
50     std::lock_guard<std::mutex> uniqueLock(mut);
51     std::string key = IdToKey(id);
52     if (!key.empty()) {
53         cb->Expired(key);
54     }
55     sessions.erase(std::find(sessions.begin(), sessions.end(), id));
56     RecycleId(id);
57 }
58 
SetCb(IAppTimer::ICb * cb)59 void AppTimerAdapter::SetCb(IAppTimer::ICb* cb)
60 {
61     this->cb = cb;
62 }
63 
ValidateDuplication(const int id)64 void AppTimerAdapter::ValidateDuplication(const int id)
65 {
66     if (std::find(sessions.begin(), sessions.end(), id) != sessions.end()) {
67         throw std::invalid_argument("duplicated id");
68     }
69 }
70 
ValidateExistence(const int id)71 void AppTimerAdapter::ValidateExistence(const int id)
72 {
73     if (std::find(sessions.begin(), sessions.end(), id) == sessions.end()) {
74         throw std::invalid_argument("non-existing id");
75     }
76 }
77 
78 
AssignId(const std::string & key)79 int AppTimerAdapter::AssignId(const std::string& key)
80 {
81     if (ids.empty()) {
82         throw std::invalid_argument("container ids is empty");
83     }
84     ValidateExistKey(key);
85     int id = ids.back();
86     ids.pop_back();
87     idToBundle[id] = key;
88     bundleToId[key] = id;
89     return id;
90 }
91 
RecycleId(const int id)92 void AppTimerAdapter::RecycleId(const int id)
93 {
94     std::string bundle = idToBundle[id];
95     bundleToId.erase(bundle);
96     idToBundle.erase(id);
97     ids.push_back(id);
98 }
99 
IdToKey(const int id)100 std::string AppTimerAdapter::IdToKey(const int id)
101 {
102     if (idToBundle.find(id) != idToBundle.end()) {
103         return idToBundle[id];
104     }
105     return "";
106 }
107 
KeyToId(const std::string & key)108 int AppTimerAdapter::KeyToId(const std::string& key)
109 {
110     if (bundleToId.find(key) != bundleToId.end()) {
111         return bundleToId[key];
112     }
113     return -1;
114 }
115 
ValidateExistKey(const std::string & key)116 void AppTimerAdapter::ValidateExistKey(const std::string& key)
117 {
118     if (bundleToId.find(key) != bundleToId.end()) {
119         throw std::invalid_argument("bundleToId key exist duplicated this record");
120     }
121 }
122