2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file src/service/agent/AgentManager.cpp
18 * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
20 * @brief Definition of AgentManager class
25 #include <attributes/attributes.h>
26 #include <exceptions/UnexpectedErrorException.h>
29 #include "AgentManager.h"
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;
39 if (m_agents.insert(std::make_pair(agentType, linkId)).second) {
40 LOGI("Registered agent: <%s>", agentType.c_str());
41 return AgentRegisterResponse::DONE;
44 LOGE("Error in registering agent: <%s>", agentType.c_str());
45 return AgentRegisterResponse::ERROR;
48 bool AgentManager::getAgentType(const LinkId &linkId, AgentType &agentType) const {
49 for (const auto &x : m_agents) {
50 if (x.second == linkId) {
58 void AgentManager::unregisterAgent(const LinkId &linkId) {
60 if (!getAgentType(linkId, agentType)) {
61 LOGD("Trying to unregister not registered agent");
64 m_agents.erase(agentType);
65 m_talkers.erase(linkId);
66 LOGI("Unregistered agent: <%s>", agentType.c_str());
69 AgentTalkerPtr AgentManager::createTalker(const AgentType &agentType) {
71 ProtocolFrameSequenceNumber checkId = generateSequenceNumber(agentType);
72 const LinkId &linkId = m_agents.at(agentType);
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);
79 } catch (const std::out_of_range &) {
80 LOGE("Required agent is not registered: <%s>", agentType.c_str());
83 return AgentTalkerPtr();
86 ProtocolFrameSequenceNumber AgentManager::generateSequenceNumber(const AgentType &agentType UNUSED)
88 // TODO: implement smart sequence number generation, maybe unique per agent
89 return m_sequenceNumber++;
92 AgentTalkerPtr AgentManager::getTalker(const LinkId &linkId, ProtocolFrameSequenceNumber requestId)
94 const auto talkerMap = m_talkers.find(linkId);
95 if (talkerMap == m_talkers.end()) {
96 return AgentTalkerPtr();
99 const auto talker = talkerMap->second.find(requestId);
100 return talker != talkerMap->second.end() ? talker->second : AgentTalkerPtr();
103 void AgentManager::removeTalker(const AgentTalkerPtr &agentTalker) {
104 m_talkers[agentTalker->linkId()].erase(agentTalker->checkId());
107 void AgentManager::cleanupAgent(const LinkId &linkId, TalkerCleanupFunction cleanupFunction) {
108 auto talkerMap = m_talkers.find(linkId);
109 if (talkerMap == m_talkers.end())
112 if (cleanupFunction) {
113 for (auto p : talkerMap->second) {
114 cleanupFunction(p.second);
117 unregisterAgent(linkId);
120 } // namespace Cynara