1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "ResourceHosting.h"
23 #include "RCSDiscoveryManager.h"
24 #include "RCSAddress.h"
33 const std::string HOSTING_TAG = "/hosting";
34 const auto HOSTING_TAG_SIZE = HOSTING_TAG.size();
35 const std::string HOSTING_RESOURSE_TYPE = "oic.r.resourcehosting";
38 ResourceHosting::ResourceHosting()
40 m_isStartedHosting(false),
46 ResourceHosting * ResourceHosting::getInstance()
48 static ResourceHosting instance;
52 void ResourceHosting::startHosting()
54 if (m_isStartedHosting) return;
55 m_isStartedHosting = true;
56 createDiscoveryListener();
59 void ResourceHosting::stopHosting()
61 if (!m_isStartedHosting) return;
63 if (!m_discoveryTask->isCanceled())
65 m_discoveryTask->cancel();
68 m_isStartedHosting = false;
70 RHLock lock(m_mutexForList);
71 m_hostingObjects.clear();
74 void ResourceHosting::createDiscoveryListener()
76 m_discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
77 RCSAddress::multicast(), OC_RSRVD_WELL_KNOWN_URI, HOSTING_RESOURSE_TYPE,
78 std::bind(&ResourceHosting::discoveryHandler, this,
79 std::placeholders::_1));
82 void ResourceHosting::discoveryHandler(RemoteObjectPtr remoteResource)
84 auto discoverdUri = remoteResource->getUri();
85 if (discoverdUri.compare(
86 discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0)
91 auto foundHostingObject = findRemoteResource(remoteResource);
92 if (foundHostingObject != nullptr) return;
96 HostingObjectKey key = generateHostingObjectKey(remoteResource);
97 foundHostingObject = HostingObject::createHostingObject(remoteResource,
98 std::bind(&ResourceHosting::destroyedHostingObject, this, key));
100 RHLock lock(m_mutexForList);
101 m_hostingObjects.insert(std::make_pair(key, foundHostingObject));
103 } catch (const RCSException & e)
105 OIC_HOSTING_LOG(DEBUG,
106 "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what());
110 HostingObject::Ptr ResourceHosting::findRemoteResource(RemoteObjectPtr remoteResource)
112 RHLock lock(m_mutexForList);
114 auto iter = m_hostingObjects.find(generateHostingObjectKey(remoteResource));
115 if (iter != m_hostingObjects.end()) return iter->second;
120 void ResourceHosting::destroyedHostingObject(const HostingObjectKey & key)
122 RHLock lock(m_mutexForList);
123 m_hostingObjects.erase(key);
126 ResourceHosting::HostingObjectKey ResourceHosting::generateHostingObjectKey(
127 std::string && address, std::string && uri)
129 return HostingObjectKey(address + uri);
132 ResourceHosting::HostingObjectKey ResourceHosting::generateHostingObjectKey(
133 const std::string & address, const std::string & uri)
135 return generateHostingObjectKey(std::string(address), std::string(uri));
137 ResourceHosting::HostingObjectKey ResourceHosting::generateHostingObjectKey(
138 RemoteObjectPtr rResource)
140 return generateHostingObjectKey(rResource->getAddress(), rResource->getUri());
143 } /* namespace Service */
144 } /* namespace OIC */