US1313, abillity to create an OCResource object without discovering it
[platform/upstream/iotivity.git] / OCLib / OCPlatform.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation 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 //******************************************************************
22 // File name:
23 //     OCPlatform.cpp
24 //
25 // Description: Implementation of the OCPlatform.
26 //
27 //
28 //
29 //*********************************************************************
30
31 #include <random>
32
33 #include "OCPlatform.h"
34 #include "OCApi.h"
35 #include "OCException.h"
36
37 namespace OC
38 {
39     // Constructor. Internally calls private init function
40     OCPlatform::OCPlatform(const PlatformConfig& config)
41     {
42         init(config);
43     }
44
45     // Destructor
46     OCPlatform::~OCPlatform(void)
47     {
48         std::cout << "platform destructor called" << std::endl;
49         cleanup();
50     }
51
52     OCStackResult OCPlatform::notifyObservers(OCResourceHandle resourceHandle)
53     {
54         return OCNotifyObservers(resourceHandle);
55     }
56
57     void OCPlatform::init(const PlatformConfig& config)
58     {
59         std::unique_ptr<WrapperFactory> wrapperInstance(new WrapperFactory());
60         m_WrapperInstance = std::move(wrapperInstance);
61
62         if(config.mode == ModeType::Server)
63         {
64             // Call server wrapper init
65             m_server = m_WrapperInstance->CreateServerWrapper(config);
66         }
67         else if(config.mode == ModeType::Client)
68         {
69             // Call client wrapper init
70             m_client = m_WrapperInstance->CreateClientWrapper(config);
71         }
72         else
73         {
74             // This must be both server and client
75             m_server = m_WrapperInstance->CreateServerWrapper(config);
76             m_client = m_WrapperInstance->CreateClientWrapper(config);
77         }
78     }
79
80     void OCPlatform::cleanup()
81     {
82         if(m_server)
83         {
84             //delete m_server;
85         }
86
87         if(m_client)
88         {
89             //delete m_client;
90         }
91     }
92
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)
96     {
97         if(m_client)
98         {
99             return std::shared_ptr<OCResource>(new OCResource(m_client, host, uri, isObservable, resourceTypes, interfaces));
100         }
101
102         else
103         {
104             return std::shared_ptr<OCResource>();
105         }
106     }
107
108     OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName,
109                 std::function<void(OCResource::Ptr)> resourceHandler)
110     {
111         if(m_client)
112         {
113             return m_client->ListenForResource(host, resourceName, resourceHandler);
114         }
115         return OC_STACK_ERROR;
116     }
117
118
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)
125     {
126         OCStackResult result = OC_STACK_OK;
127
128         if(m_server)
129         {
130             try{
131                 result = m_server->registerResource(resourceHandle, resourceURI, resourceTypeName, resourceInterface, entityHandler, resourceProperty);
132             }
133             catch(std::exception e) // define our own expception.
134             {
135                 throw e;
136             }
137         }
138
139         return result;
140     }
141
142     // TODO: Implement
143     OCStackResult OCPlatform::unbindResource(OCResourceHandle containerHandle, OCResourceHandle resourceHandle)
144     {
145         OCStackResult result = OC_STACK_OK;
146         return result;
147     }
148
149     // TODO: Implement
150     OCStackResult OCPlatform::unbindResources(OCResourceHandle containerHandle, std::vector<OCResourceHandle>& resourceHandleList)
151     {
152         OCStackResult result = OC_STACK_OK;
153         return result;
154     }
155
156     OCStackResult bindResource(const OCResourceHandle containerHandle, const OCResourceHandle resourceHandle)
157     {
158         OCStackResult result = OC_STACK_OK;
159
160         try {
161             result = OCBindContainedResourceToResource(containerHandle, resourceHandle);
162         }
163         catch(std::exception e)
164         {
165             throw e;
166         }
167
168         return result;
169     }
170
171     OCStackResult bindResources(const OCResourceHandle containerHandle, const std::vector<OCResourceHandle>& resourceHandles)
172     {
173         OCStackResult result = OC_STACK_OK;
174
175         try {
176
177             std::vector<OCResourceHandle>::const_iterator it;
178
179             for(it = resourceHandles.begin(); it != resourceHandles.end(); it++)
180             {
181                 result = OCBindContainedResourceToResource(containerHandle, *it);
182
183                 if(result != OC_STACK_OK)
184                 {
185                     // TODO Should we unbind the previous successful ones?
186                     // TODO should we return which are succesful
187                     // Currently just returns with any failure
188                     return result;
189                 }
190             }
191         }
192         catch(std::exception e)
193         {
194             throw e;
195         }
196
197         return result;
198     }
199
200     OCStackResult OCPlatform::bindTypeToResource(const OCResourceHandle& resourceHandle,
201                      const std::string& resourceTypeName) const
202     {
203         OCStackResult result = OC_STACK_ERROR;
204         if(m_server)
205         {
206             try
207             {
208                 result = m_server->bindTypeToResource(resourceHandle, resourceTypeName);
209             }
210             catch (OCException& e)
211             {
212                 cout << "Caught an exception..." << endl;
213                 cout << "\tMessage: " << e.what()  << endl;
214                 cout << "\t Reason: " << e.reason() << endl;
215             }
216         }
217         return result;
218     }
219
220     OCStackResult OCPlatform::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
221                      const std::string& resourceInterfaceName) const
222     {
223         OCStackResult result = OC_STACK_ERROR;
224         if(m_server)
225         {
226             try
227             {
228                 result = m_server->bindInterfaceToResource(resourceHandle, resourceInterfaceName);
229             }
230             catch (OCException& e)
231             {
232                 cout << "Caught an exception..." << endl;
233                 cout << "\tMessage: " << e.what()  << endl;
234                 cout << "\t Reason: " << e.reason() << endl;
235             }
236         }
237         return result;
238
239     }
240
241 } //namespace OC