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 
16 #ifndef ENGINE_MANAGER_H
17 #define ENGINE_MANAGER_H
18 
19 #include <cstddef>
20 #include <map>
21 
22 #include "platform/lock/include/rw_lock.h"
23 #include "server_executor/include/engine.h"
24 
25 namespace OHOS {
26 namespace AI {
27 // Synchronization message default size
28 const size_t MAX_SYNC_MSG_NUM = 64;
29 
30 // Asynchronous message default size
31 const size_t MAX_ASYNC_MSG_NUM = 64;
32 
33 struct EngineKey {
34     std::string aid;
35     long long version;
36 
EngineKeyEngineKey37     EngineKey(const std::string &aid, long long version) : aid(aid), version(version)
38     {
39     }
40 
41     inline bool operator < (const EngineKey &another) const
42     {
43         if (aid < another.aid) {
44             return true;
45         }
46         if (aid == another.aid && version < another.version) {
47             return true;
48         }
49         return false;
50     }
51 };
52 
53 class EngineManager {
54 public:
55     EngineManager();
56     ~EngineManager();
57 
58     /**
59      * Initialize the engine manager.
60      *
61      * @return Returns RETCODE_SUCCESS(0) if the operation is successful, returns a non-zero value otherwise.
62      */
63     int Initialize();
64 
65     /**
66      * Start engine.
67      *
68      * @param [in] transactionId Transaction ID.
69      * @param [in] algoInfo Algorithm information.
70      * @param [in] inputInfo Data information needed to start engine.
71      * @param [out] outputInfo The returned data information after starting the algorithm plugin.
72      * @return Returns RETCODE_SUCCESS(0) if the operation is successful, returns a non-zero value otherwise.
73      */
74     int StartEngine(long long transactionId, const AlgorithmInfo &algoInfo, const DataInfo &inputInfo,
75         DataInfo &outputInfo);
76 
77     /**
78      * Stop engine.
79      *
80      * @param [in] transactionId Transaction ID.
81      * @param [in] inputInfo Data information needed to stop engine.
82      * @return Returns RETCODE_SUCCESS(0) if the operation is successful, returns a non-zero value otherwise.
83      */
84     int StopEngine(long long transactionId, const DataInfo &inputInfo);
85 
86     /**
87      * Find the engine based on transaction ID.
88      *
89      * @param [in] transactionId Transaction ID.
90      * @return Engine corresponding to transaction ID.
91      */
92     std::shared_ptr<Engine> FindEngine(long long transactionId);
93 
94     /**
95      * Set the configuration parameters of the plugin algorithm.
96      *
97      * @param [in] transactionId Transaction ID.
98      * @param [in] optionType The type of setting option.
99      * @param [in] inputInfo Configuration parameter needed to set up the plugin.
100      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
101      */
102     int SetOption(long long transactionId, int optionType, const DataInfo &inputInfo);
103 
104     /**
105      * Get the configuration parameters of the plugin algorithm.
106      *
107      * @param [in] transactionId Transaction ID.
108      * @param [in] optionType The type of getting option.
109      * @param [in] inputInfo Parameter information for getting options.
110      * @param [out] outputInfo The configuration parameter information.
111      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
112      */
113     int GetOption(long long transactionId, int optionType, const DataInfo &inputInfo,
114         DataInfo &outputInfo);
115 
116 private:
117     void Uninitialize();
118     void RecordClient(long long transactionId, const std::shared_ptr<Engine> &engine);
119     void UnRecordClient(long long transactionId);
120     int CreateEngine(const EngineKey &engineKey, std::shared_ptr<Engine> &engine);
121     int CreateEngineWithCheck(const EngineKey &engineKey, std::shared_ptr<Engine> &engine);
122     std::shared_ptr<Engine> FindEngine(const EngineKey &engineKey);
123     void DelEngine(const EngineKey &engineKey);
124 
125 private:
126     RwLock rwLock_;
127     using Engines = std::map<EngineKey, std::shared_ptr<Engine>>;
128     Engines engines_;
129     using ClientEngines = std::map<long long, std::shared_ptr<Engine>>;
130     ClientEngines clientEngines_;
131 };
132 } // namespace AI
133 } // namespace OHOS
134 
135 #endif // ENGINE_MANAGER_H