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 #ifndef SCENE_TIMER_OH_IMPL_H 16 #define SCENE_TIMER_OH_IMPL_H 17 18 #include "ISceneTimerInfrastructure.h" 19 #include <map> 20 #include <mutex> 21 #include <condition_variable> 22 23 /* this design is difficult to UT, that's a problem */ 24 class SceneTimerOhImpl : public ISceneTimerInfrastructure { 25 public: 26 const static unsigned int checkInterval = 500; // this means accuracy 27 28 SceneTimerOhImpl(); 29 void RegUser(int userId, ICb* cb) override; 30 void UnRegUser(int userId) override; 31 void Start(int user, int id, long interval) override; 32 void Stop(int user, int id) override; 33 34 private: 35 const static int maxUserId = 16; 36 const static int maxRecordPerUser = 128; 37 38 std::map<int, long long> records; 39 std::mutex mut; 40 std::condition_variable cv; 41 std::map<int, ICb*> callbacks; // userId as key, potentially multi-thread problem, but for now, lock is not needed 42 43 void Loop(); 44 void FillRecordAndNotify(int key, long long expireTs); 45 void ValidateDuplication(int key); 46 void ValidateExistence(int key); 47 long long GetCurTimeStamp(); 48 void CheckRecordsAndTrigger(); 49 static int BuildRecordKey(int user, int id); 50 long long CalcExpireTimeStamp(long delay); 51 void TriggerCallbak(int recordKey); 52 static int ExtractUserFromRecordKey(int key); 53 static int ExtractIdFromRecordKey(int key); 54 void RemoveRecordAndNotify(int key); 55 }; 56 #endif 57