f7e9913f67784e75e626a969545ded0b97c7c627
[platform/upstream/iotivity.git] / service / resource-hosting / src / ResourceHosting.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "ResourceHosting.h"
22
23 #include "PresenceSubscriber.h"
24 #include "OCPlatform.h"
25 #include "RCSDiscoveryManager.h"
26 #include "RCSAddress.h"
27
28 namespace OIC
29 {
30 namespace Service
31 {
32
33 namespace
34 {
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";
39 }
40
41 ResourceHosting::ResourceHosting()
42 : m_mutexForList(),
43   m_isStartedHosting(false),
44   m_hostingObjects(),
45   m_discoveryTask()
46 {
47 }
48
49 ResourceHosting * ResourceHosting::getInstance()
50 {
51     static ResourceHosting instance;
52     return & instance;
53 }
54
55 void ResourceHosting::startHosting()
56 {
57     if(m_isStartedHosting) return;
58     m_isStartedHosting = true;
59     createDiscoveryListener();
60 }
61
62 void ResourceHosting::stopHosting()
63 {
64     if(!m_isStartedHosting) return;
65
66     if(!m_discoveryTask->isCanceled())
67     {
68         m_discoveryTask->cancel();
69     }
70
71     m_isStartedHosting = false;
72
73     RHLock lock(m_mutexForList);
74     m_hostingObjects.clear();
75 }
76
77 void ResourceHosting::createDiscoveryListener()
78 {
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));
83 }
84
85 void ResourceHosting::discoveryHandler(RemoteObjectPtr remoteResource)
86 {
87     auto discoverdUri = remoteResource->getUri();
88     if(discoverdUri.compare(
89             discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0)
90     {
91         return;
92     }
93
94     auto foundHostingObject = findRemoteResource(remoteResource);
95     if(foundHostingObject != nullptr) return;
96
97     try
98     {
99         HostingObjectKey key = generateHostingObjectKey(remoteResource);
100         foundHostingObject = HostingObject::createHostingObject(remoteResource,
101                 std::bind(&ResourceHosting::destroyedHostingObject, this, key));
102
103         RHLock lock(m_mutexForList);
104         m_hostingObjects.insert(std::make_pair(key, foundHostingObject));
105
106     }catch(const RCSException &e)
107     {
108         OIC_HOSTING_LOG(DEBUG,
109                 "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what());
110     }
111 }
112
113 HostingObject::Ptr ResourceHosting::findRemoteResource(RemoteObjectPtr remoteResource)
114 {
115     RHLock lock(m_mutexForList);
116
117     auto iter = m_hostingObjects.find(generateHostingObjectKey(remoteResource));
118     if(iter != m_hostingObjects.end()) return iter->second;
119
120     return nullptr;
121 }
122
123 void ResourceHosting::destroyedHostingObject(const HostingObjectKey & key)
124 {
125     RHLock lock(m_mutexForList);
126     m_hostingObjects.erase(key);
127 }
128
129 } /* namespace Service */
130 } /* namespace OIC */