Apply to Resource Hosting to RE resource server added feature.
[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 "RCSDiscoveryManager.h"
24 #include "RCSAddress.h"
25
26 namespace OIC
27 {
28     namespace Service
29     {
30
31         namespace
32         {
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";
36         }
37
38         ResourceHosting::ResourceHosting()
39         : m_mutexForList(),
40           m_isStartedHosting(false),
41           m_hostingObjects(),
42           m_discoveryTask()
43         {
44         }
45
46         ResourceHosting * ResourceHosting::getInstance()
47         {
48             static ResourceHosting instance;
49             return & instance;
50         }
51
52         void ResourceHosting::startHosting()
53         {
54             if (m_isStartedHosting) return;
55             m_isStartedHosting = true;
56             createDiscoveryListener();
57         }
58
59         void ResourceHosting::stopHosting()
60         {
61             if (!m_isStartedHosting) return;
62
63             if (!m_discoveryTask->isCanceled())
64             {
65                 m_discoveryTask->cancel();
66             }
67
68             m_isStartedHosting = false;
69
70             RHLock lock(m_mutexForList);
71             m_hostingObjects.clear();
72         }
73
74         void ResourceHosting::createDiscoveryListener()
75         {
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));
80         }
81
82         void ResourceHosting::discoveryHandler(RemoteObjectPtr remoteResource)
83         {
84             auto discoverdUri = remoteResource->getUri();
85             if (discoverdUri.compare(
86                     discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0)
87             {
88                 return;
89             }
90
91             auto foundHostingObject = findRemoteResource(remoteResource);
92             if (foundHostingObject != nullptr) return;
93
94             try
95             {
96                 HostingObjectKey key = generateHostingObjectKey(remoteResource);
97                 foundHostingObject = HostingObject::createHostingObject(remoteResource,
98                         std::bind(&ResourceHosting::destroyedHostingObject, this, key));
99
100                 RHLock lock(m_mutexForList);
101                 m_hostingObjects.insert(std::make_pair(key, foundHostingObject));
102
103             } catch (const RCSException & e)
104             {
105                 OIC_HOSTING_LOG(DEBUG,
106                         "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what());
107             }
108         }
109
110         HostingObject::Ptr ResourceHosting::findRemoteResource(RemoteObjectPtr remoteResource)
111         {
112             RHLock lock(m_mutexForList);
113
114             auto iter = m_hostingObjects.find(generateHostingObjectKey(remoteResource));
115             if (iter != m_hostingObjects.end()) return iter->second;
116
117             return nullptr;
118         }
119
120         void ResourceHosting::destroyedHostingObject(const HostingObjectKey & key)
121         {
122             RHLock lock(m_mutexForList);
123             m_hostingObjects.erase(key);
124         }
125
126         ResourceHosting::HostingObjectKey ResourceHosting::generateHostingObjectKey(
127                 std::string && address, std::string && uri)
128         {
129             return HostingObjectKey(address + uri);
130         }
131
132         ResourceHosting::HostingObjectKey ResourceHosting::generateHostingObjectKey(
133                 const std::string & address, const std::string & uri)
134         {
135             return generateHostingObjectKey(std::string(address), std::string(uri));
136         }
137         ResourceHosting::HostingObjectKey ResourceHosting::generateHostingObjectKey(
138                 RemoteObjectPtr rResource)
139         {
140             return generateHostingObjectKey(rResource->getAddress(), rResource->getUri());
141         }
142
143     } /* namespace Service */
144 } /* namespace OIC */