1 //******************************************************************
3 // Copyright 2014 Intel Corporation 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 //******************************************************************
25 // Description: Implementation of the OCPlatform.
29 //*********************************************************************
33 #include "OCPlatform.h"
35 #include "OCException.h"
39 // Constructor. Internally calls private init function
40 OCPlatform::OCPlatform(const PlatformConfig& config)
46 OCPlatform::~OCPlatform(void)
48 std::cout << "platform destructor called" << std::endl;
52 OCStackResult OCPlatform::notifyObservers(OCResourceHandle resourceHandle)
54 return OCNotifyObservers(resourceHandle);
57 void OCPlatform::init(const PlatformConfig& config)
59 std::unique_ptr<WrapperFactory> wrapperInstance(new WrapperFactory());
60 m_WrapperInstance = std::move(wrapperInstance);
62 if(config.mode == ModeType::Server)
64 // Call server wrapper init
65 m_server = m_WrapperInstance->CreateServerWrapper(config);
67 else if(config.mode == ModeType::Client)
69 // Call client wrapper init
70 m_client = m_WrapperInstance->CreateClientWrapper(config);
74 // This must be both server and client
75 m_server = m_WrapperInstance->CreateServerWrapper(config);
76 m_client = m_WrapperInstance->CreateClientWrapper(config);
80 void OCPlatform::cleanup()
93 OCResource::Ptr OCPlatform::constructResourceObject(const std::string& host, const std::string& uri,
94 bool isObservable, const std::vector<std::string>& resourceTypes,
95 const std::vector<std::string>& interfaces)
99 return std::shared_ptr<OCResource>(new OCResource(m_client, host, uri, isObservable, resourceTypes, interfaces));
104 return std::shared_ptr<OCResource>();
108 OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName,
109 std::function<void(OCResource::Ptr)> resourceHandler)
113 return m_client->ListenForResource(host, resourceName, resourceHandler);
115 return OC_STACK_ERROR;
119 OCStackResult OCPlatform::registerResource(OCResourceHandle& resourceHandle,
120 std::string& resourceURI,
121 const std::string& resourceTypeName,
122 const std::string& resourceInterface,
123 std::function<void(const OCResourceRequest::Ptr, const OCResourceResponse::Ptr)> entityHandler,
124 uint8_t resourceProperty)
126 OCStackResult result = OC_STACK_OK;
131 result = m_server->registerResource(resourceHandle, resourceURI, resourceTypeName, resourceInterface, entityHandler, resourceProperty);
133 catch(std::exception e) // define our own expception.
143 OCStackResult OCPlatform::unbindResource(OCResourceHandle containerHandle, OCResourceHandle resourceHandle)
145 OCStackResult result = OC_STACK_OK;
150 OCStackResult OCPlatform::unbindResources(OCResourceHandle containerHandle, std::vector<OCResourceHandle>& resourceHandleList)
152 OCStackResult result = OC_STACK_OK;
156 OCStackResult bindResource(const OCResourceHandle containerHandle, const OCResourceHandle resourceHandle)
158 OCStackResult result = OC_STACK_OK;
161 result = OCBindContainedResourceToResource(containerHandle, resourceHandle);
163 catch(std::exception e)
171 OCStackResult bindResources(const OCResourceHandle containerHandle, const std::vector<OCResourceHandle>& resourceHandles)
173 OCStackResult result = OC_STACK_OK;
177 std::vector<OCResourceHandle>::const_iterator it;
179 for(it = resourceHandles.begin(); it != resourceHandles.end(); it++)
181 result = OCBindContainedResourceToResource(containerHandle, *it);
183 if(result != OC_STACK_OK)
185 // TODO Should we unbind the previous successful ones?
186 // TODO should we return which are succesful
187 // Currently just returns with any failure
192 catch(std::exception e)
200 OCStackResult OCPlatform::bindTypeToResource(const OCResourceHandle& resourceHandle,
201 const std::string& resourceTypeName) const
203 OCStackResult result = OC_STACK_ERROR;
208 result = m_server->bindTypeToResource(resourceHandle, resourceTypeName);
210 catch (OCException& e)
212 cout << "Caught an exception..." << endl;
213 cout << "\tMessage: " << e.what() << endl;
214 cout << "\t Reason: " << e.reason() << endl;
220 OCStackResult OCPlatform::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
221 const std::string& resourceInterfaceName) const
223 OCStackResult result = OC_STACK_ERROR;
228 result = m_server->bindInterfaceToResource(resourceHandle, resourceInterfaceName);
230 catch (OCException& e)
232 cout << "Caught an exception..." << endl;
233 cout << "\tMessage: " << e.what() << endl;
234 cout << "\t Reason: " << e.reason() << endl;