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"
34 std::string HOSTING_TAG = "/hosting";
35 size_t HOSTING_TAG_SIZE = (size_t)HOSTING_TAG.size();
36 std::string MULTICAST_PRESENCE_ADDRESS = std::string("coap://") + OC_MULTICAST_PREFIX;
37 std::string HOSTING_RESOURSE_TYPE = "resource.hosting";
40 ResourceHosting * ResourceHosting::s_instance(nullptr);
41 std::mutex ResourceHosting::s_mutexForCreation;
43 ResourceHosting::ResourceHosting()
44 : hostingObjectList(),
45 discoveryManager(nullptr),
47 pPresenceCB(nullptr), pDiscoveryCB(nullptr)
51 ResourceHosting * ResourceHosting::getInstance()
55 s_mutexForCreation.lock();
58 s_instance = new ResourceHosting();
59 s_instance->initializeResourceHosting();
61 s_mutexForCreation.unlock();
66 void ResourceHosting::startHosting()
70 requestMulticastPresence();
71 requestMulticastDiscovery();
72 }catch(const RCSPlatformException &e)
74 OIC_HOSTING_LOG(DEBUG,
75 "[ResourceHosting::startHosting]PlatformException:%s", e.what());
77 }catch(const RCSInvalidParameterException &e)
79 OIC_HOSTING_LOG(DEBUG,
80 "[ResourceHosting::startHosting]InvalidParameterException:%s", e.what());
82 }catch(const std::exception &e)
84 OIC_HOSTING_LOG(DEBUG,
85 "[ResourceHosting::startHosting]std::exception:%s", e.what());
90 void ResourceHosting::stopHosting()
92 // clear list hostingObjectList
93 if(presenceHandle.isSubscribing())
95 presenceHandle.unsubscribe();
98 hostingObjectList.clear();
101 void ResourceHosting::initializeResourceHosting()
103 pPresenceCB = std::bind(&ResourceHosting::presenceHandler, this,
104 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
105 pDiscoveryCB = std::bind(&ResourceHosting::discoverHandler, this,
106 std::placeholders::_1);
108 discoveryManager = RCSDiscoveryManager::getInstance();
111 void ResourceHosting::requestMulticastPresence()
115 presenceHandle = PresenceSubscriber(MULTICAST_PRESENCE_ADDRESS,
116 OCConnectivityType::CT_DEFAULT, pPresenceCB);
123 void ResourceHosting::presenceHandler(OCStackResult ret, const unsigned int /*seq*/,
124 const std::string & address)
129 case OC_STACK_CONTINUE:
130 case OC_STACK_RESOURCE_CREATED:
132 // TODO start discovery
133 requestDiscovery(address);
137 case OC_STACK_RESOURCE_DELETED:
138 case OC_STACK_COMM_ERROR:
139 case OC_STACK_TIMEOUT:
140 case OC_STACK_PRESENCE_STOPPED:
141 case OC_STACK_PRESENCE_TIMEOUT:
142 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
144 // TODO presence error
147 case OC_STACK_INVALID_URI:
148 case OC_STACK_INVALID_QUERY:
149 case OC_STACK_INVALID_IP:
150 case OC_STACK_INVALID_PORT:
151 case OC_STACK_INVALID_CALLBACK:
152 case OC_STACK_INVALID_METHOD:
153 case OC_STACK_INVALID_PARAM:
154 case OC_STACK_INVALID_OBSERVE_PARAM:
155 case OC_STACK_NO_MEMORY:
156 case OC_STACK_ADAPTER_NOT_ENABLED:
157 case OC_STACK_NOTIMPL:
158 case OC_STACK_NO_RESOURCE:
159 case OC_STACK_RESOURCE_ERROR:
160 case OC_STACK_SLOW_RESOURCE:
161 case OC_STACK_DUPLICATE_REQUEST:
162 case OC_STACK_NO_OBSERVERS:
163 case OC_STACK_OBSERVER_NOT_FOUND:
164 case OC_STACK_INVALID_OPTION:
165 case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
166 case OC_STACK_MALFORMED_RESPONSE:
167 case OC_STACK_PERSISTENT_BUFFER_REQUIRED:
168 case OC_STACK_INVALID_REQUEST_HANDLE:
169 case OC_STACK_INVALID_DEVICE_INFO:
170 case OC_STACK_INVALID_JSON:
173 // TODO unknown presence result
178 void ResourceHosting::requestMulticastDiscovery()
182 void ResourceHosting::requestDiscovery(std::string address)
184 std::string host = address;
185 RCSAddress rcsAddress = RCSAddress::unicast(host);
186 discoveryTask = discoveryManager->discoverResourceByType(
187 rcsAddress, OC_RSRVD_WELL_KNOWN_URI, HOSTING_RESOURSE_TYPE, pDiscoveryCB);
190 void ResourceHosting::discoverHandler(RemoteObjectPtr remoteResource)
192 std::string discoverdUri = remoteResource->getUri();
193 if(discoverdUri.compare(
194 discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0)
199 HostingObjectPtr foundHostingObject = findRemoteResource(remoteResource);
200 if(foundHostingObject == nullptr)
204 foundHostingObject = std::make_shared<HostingObject>();
205 foundHostingObject->initializeHostingObject(remoteResource,
206 std::bind(&ResourceHosting::destroyedHostingObject, this,
207 HostingObjectWeakPtr(foundHostingObject)));
208 hostingObjectList.push_back(foundHostingObject);
209 }catch(const RCSInvalidParameterException &e)
211 OIC_HOSTING_LOG(DEBUG,
212 "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what());
217 ResourceHosting::HostingObjectPtr ResourceHosting::findRemoteResource(
218 RemoteObjectPtr remoteResource)
220 HostingObjectPtr retObject = nullptr;
222 for(auto it : hostingObjectList)
224 RemoteObjectPtr inListPtr = it->getRemoteResource();
225 if(inListPtr != nullptr && isSameRemoteResource(inListPtr, remoteResource))
234 bool ResourceHosting::isSameRemoteResource(
235 RemoteObjectPtr remoteResource_1, RemoteObjectPtr remoteResource_2)
238 if(remoteResource_1->getAddress() == remoteResource_2->getAddress() &&
239 remoteResource_1->getUri() == remoteResource_2->getUri())
246 void ResourceHosting::destroyedHostingObject(HostingObjectWeakPtr destroyedWeakPtr)
248 auto destroyedPtr = destroyedWeakPtr.lock();
249 if (destroyedPtr) return;
251 std::unique_lock<std::mutex> lock(mutexForList);
252 hostingObjectList.remove(destroyedPtr);
255 } /* namespace Service */
256 } /* namespace OIC */