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 "PresenceSubscriber.h"
24 #include "OCPlatform.h"
25 #include "RCSDiscoveryManager.h"
26 #include "RCSAddress.h"
35 const std::string HOSTING_TAG = "/hosting";
36 const auto HOSTING_TAG_SIZE = HOSTING_TAG.size();
37 const std::string MULTICAST_PRESENCE_ADDRESS = std::string("coap://") + OC_MULTICAST_PREFIX;
38 const std::string HOSTING_RESOURSE_TYPE = "oic.r.resourcehosting";
41 ResourceHosting::ResourceHosting()
43 m_isStartedHosting(false),
49 ResourceHosting * ResourceHosting::getInstance()
51 static ResourceHosting instance;
55 void ResourceHosting::startHosting()
57 if(m_isStartedHosting) return;
58 m_isStartedHosting = true;
59 createDiscoveryListener();
62 void ResourceHosting::stopHosting()
64 if(!m_isStartedHosting) return;
66 if(!m_discoveryTask->isCanceled())
68 m_discoveryTask->cancel();
71 m_isStartedHosting = false;
73 RHLock lock(m_mutexForList);
74 m_hostingObjects.clear();
77 void ResourceHosting::createDiscoveryListener()
79 m_discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
80 RCSAddress::multicast(), OC_RSRVD_WELL_KNOWN_URI, HOSTING_RESOURSE_TYPE,
81 std::bind(&ResourceHosting::discoveryHandler, this,
82 std::placeholders::_1));
85 void ResourceHosting::discoveryHandler(RemoteObjectPtr remoteResource)
87 auto discoverdUri = remoteResource->getUri();
88 if(discoverdUri.compare(
89 discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0)
94 auto foundHostingObject = findRemoteResource(remoteResource);
95 if(foundHostingObject != nullptr) return;
99 HostingObjectKey key = generateHostingObjectKey(remoteResource);
100 foundHostingObject = HostingObject::createHostingObject(remoteResource,
101 std::bind(&ResourceHosting::destroyedHostingObject, this, key));
103 RHLock lock(m_mutexForList);
104 m_hostingObjects.insert(std::make_pair(key, foundHostingObject));
106 }catch(const RCSException &e)
108 OIC_HOSTING_LOG(DEBUG,
109 "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what());
113 HostingObject::Ptr ResourceHosting::findRemoteResource(RemoteObjectPtr remoteResource)
115 RHLock lock(m_mutexForList);
117 auto iter = m_hostingObjects.find(generateHostingObjectKey(remoteResource));
118 if(iter != m_hostingObjects.end()) return iter->second;
123 void ResourceHosting::destroyedHostingObject(const HostingObjectKey & key)
125 RHLock lock(m_mutexForList);
126 m_hostingObjects.erase(key);
129 } /* namespace Service */
130 } /* namespace OIC */