Handle check request with agent usage
[platform/core/security/cynara.git] / src / service / agent / AgentManager.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file        src/service/agent/AgentManager.cpp
18  * @author      Adam Malinowski <a.malinowsk2@partner.samsung.com>
19  * @version     1.0
20  * @brief       Definition of AgentManager class
21  */
22
23 #include <cstdint>
24
25 #include <attributes/attributes.h>
26 #include <exceptions/UnexpectedErrorException.h>
27 #include <log/log.h>
28
29 #include "AgentManager.h"
30
31 namespace Cynara {
32
33 AgentRegisterResponse::Code AgentManager::registerAgent(const AgentType &agentType,
34                                                         const LinkId &linkId) {
35     if (m_agents.find(agentType) != m_agents.end()) {
36         return AgentRegisterResponse::REJECTED;
37     }
38
39     if (m_agents.insert(std::make_pair(agentType, linkId)).second) {
40         LOGI("Registered agent: <%s>", agentType.c_str());
41         return AgentRegisterResponse::DONE;
42     }
43
44     LOGE("Error in registering agent: <%s>", agentType.c_str());
45     return AgentRegisterResponse::ERROR;
46 }
47
48 bool AgentManager::getAgentType(const LinkId &linkId, AgentType &agentType) const {
49     for (const auto &x : m_agents) {
50         if (x.second == linkId) {
51            agentType = x.first;
52            return true;
53         }
54     }
55     return false;
56 }
57
58 void AgentManager::unregisterAgent(const LinkId &linkId) {
59     AgentType agentType;
60     if (!getAgentType(linkId, agentType)) {
61         LOGD("Trying to unregister not registered agent");
62         return;
63     }
64     m_agents.erase(agentType);
65     m_talkers.erase(linkId);
66     LOGI("Unregistered agent: <%s>", agentType.c_str());
67 }
68
69 AgentTalkerPtr AgentManager::createTalker(const AgentType &agentType) {
70     try {
71         ProtocolFrameSequenceNumber checkId = generateSequenceNumber(agentType);
72         const LinkId &linkId = m_agents.at(agentType);
73
74         AgentTalkerPtr talker = std::make_shared<AgentTalker>(agentType, linkId, checkId);
75         if (m_talkers[linkId].insert(std::make_pair(checkId, talker)).second) {
76             LOGD("Created talker for: <%s>:[%" PRIu16 "]", agentType.c_str(), checkId);
77             return talker;
78         }
79     } catch (const std::out_of_range &) {
80         LOGE("Required agent is not registered: <%s>", agentType.c_str());
81     }
82
83     return AgentTalkerPtr();
84 }
85
86 ProtocolFrameSequenceNumber AgentManager::generateSequenceNumber(const AgentType &agentType UNUSED)
87 {
88     // TODO: implement smart sequence number generation, maybe unique per agent
89     return m_sequenceNumber++;
90 }
91
92 AgentTalkerPtr AgentManager::getTalker(const LinkId &linkId, ProtocolFrameSequenceNumber requestId)
93                              const {
94     const auto talkerMap = m_talkers.find(linkId);
95     if (talkerMap == m_talkers.end()) {
96         return AgentTalkerPtr();
97     }
98
99     const auto talker = talkerMap->second.find(requestId);
100     return talker != talkerMap->second.end() ? talker->second : AgentTalkerPtr();
101 }
102
103 void AgentManager::removeTalker(const AgentTalkerPtr &agentTalker) {
104     m_talkers[agentTalker->linkId()].erase(agentTalker->checkId());
105 }
106
107 void AgentManager::cleanupAgent(const LinkId &linkId, TalkerCleanupFunction cleanupFunction) {
108     auto talkerMap = m_talkers.find(linkId);
109     if (talkerMap == m_talkers.end())
110         return;
111
112     if (cleanupFunction) {
113         for (auto p : talkerMap->second) {
114             cleanupFunction(p.second);
115         }
116     }
117     unregisterAgent(linkId);
118 }
119
120 } // namespace Cynara