1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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 "SSMResourceServer.h"
23 #include "Common/PlatformLayer.h"
24 #include "Common/InternalInterface.h"
27 static std::vector< std::map<std::string, std::string> > g_vecQueryEventResults;
29 class CQueryEngineEvent: public IQueryEngineEvent
32 std::string m_queryEngineId;
33 OCResourceHandle m_hSSMResource;
37 CQueryEngineEvent(std::string queryEngineId, OCResourceHandle resourceHandle)
39 m_queryEngineId = queryEngineId;
40 m_hSSMResource = resourceHandle;
43 SSMRESULT onQueryEngineEvent(IN int cqid, IN IDataReader *pResult)
46 IModelData *pModelData = NULL;
47 std::vector < std::string > affectedModels;
49 std::map<std::string, std::string> queryEventResult;
51 std::stringstream sstream;
54 queryEventResult["queryEngineId"] = m_queryEngineId;
58 queryEventResult["CQID"] = sstream.str();
61 pResult->getAffectedModels(&affectedModels);
63 // Affected Model Count
64 sstream << affectedModels.size();
65 queryEventResult["modelCount"] = sstream.str();
68 //TODO: we assume that contains only one model at time
69 for (std::vector< std::string >::iterator itor = affectedModels.begin();
70 itor != affectedModels.end(); ++itor)
74 queryEventResult["modelName"] = sstream.str();
77 pResult->getModelDataCount(*itor, &dataCount);
81 queryEventResult["dataCount"] = sstream.str();
84 //FixME: we have to support multiple data count
85 for (int i = 0; i < dataCount; i++)
87 pResult->getModelData(*itor, i, &pModelData);
90 sstream << pModelData->getDataId();
91 queryEventResult["dataId"] = sstream.str();
95 sstream << pModelData->getPropertyCount();
96 queryEventResult["propertyCount"] = sstream.str();
99 for (int j = 0; j < pModelData->getPropertyCount(); j++)
101 // Property Name & Value
102 sstream << pModelData->getPropertyValue(j).c_str();
103 queryEventResult[pModelData->getPropertyName(j).c_str()] = sstream.str();
109 g_vecQueryEventResults.push_back(queryEventResult);
111 //TODO: need to modify for notifying proper clients
112 OCPlatform::notifyAllObservers(m_hSSMResource);
118 SSMResourceServer::SSMResourceServer()
120 m_hSSMResource = NULL;
123 SSMResourceServer::~SSMResourceServer()
127 int SSMResourceServer::initializeManager(std::string &xmlDescription)
129 SSMRESULT res = SSM_E_FAIL;
131 SSM_CLEANUP_ASSERT(InitializeSSMCore(xmlDescription));
132 SSM_CLEANUP_ASSERT(StartSSMCore());
134 if (createResource() != 0)
136 SSM_CLEANUP_ASSERT (SSM_E_FAIL);
146 int SSMResourceServer::terminateManager()
148 SSMRESULT res = SSM_E_FAIL;
150 SSM_CLEANUP_ASSERT(StopSSMCore());
151 SSM_CLEANUP_ASSERT(TerminateSSMCore());
160 int SSMResourceServer::createResource()
162 std::string resourceURI = "/service/SoftSensorManager"; // URI of the resource
163 std::string resourceTypeName = "core.SoftSensorManager"; // resource type name.
164 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
166 // OCResourceProperty is defined ocstack.h
167 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
169 // This will internally create and register the resource.
170 OCStackResult result = OCPlatform::registerResource(m_hSSMResource, resourceURI,
171 resourceTypeName, resourceInterface,
172 std::bind(&SSMResourceServer::entityHandler, this, std::placeholders::_1
173 ), resourceProperty);
175 if (OC_STACK_OK != result)
183 OCEntityHandlerResult SSMResourceServer::entityHandler(std::shared_ptr< OCResourceRequest > request)
185 SSMRESULT res = SSM_E_FAIL;
187 auto response = std::make_shared<OC::OCResourceResponse>();
191 // Get the request type and request flag
192 std::string requestType = request->getRequestType();
193 int requestFlag = request->getRequestHandlerFlag();
195 response->setRequestHandle(request->getRequestHandle());
196 response->setResourceHandle(request->getResourceHandle());
198 if (requestFlag & RequestHandlerFlag::InitFlag)
200 // entity handler to perform resource initialization operations
203 if (requestFlag & RequestHandlerFlag::RequestFlag)
205 cout << "\t\trequestFlag : Request\n";
207 // If the request type is GET
208 if (requestType == "GET")
210 std::map<std::string, std::string> responseAttributeMap;
212 OCRepresentation rep = request->getResourceRepresentation();
214 if (g_vecQueryEventResults.size() > 0)
216 responseAttributeMap = g_vecQueryEventResults.front();
217 g_vecQueryEventResults.erase(g_vecQueryEventResults.begin());
222 for (std::map<std::string, std::string>::iterator itor =
223 responseAttributeMap.begin(); itor != responseAttributeMap.end();
226 rep.setValue(itor->first, itor->second);
229 response->setErrorCode(200);
230 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
233 else if (requestType == "PUT")
235 OCRepresentation rep = request->getResourceRepresentation();
237 IQueryEngine *pQueryEngine = NULL;
239 std::stringstream sstream;
241 // Process query params and do required operations ..
242 if (rep.getValue<std::string>("command") == "CreateQueryEngine")
244 CQueryEngineEvent *queryEngineEvent = NULL;
246 res = CreateQueryEngine(&pQueryEngine);
250 rep.setValue("error", "CreateQueryEngine failed");
254 sstream << reinterpret_cast<intptr_t>(pQueryEngine);
256 // Register QueryEngineEvent
257 queryEngineEvent = new CQueryEngineEvent(sstream.str(), m_hSSMResource);
259 if (queryEngineEvent == NULL)
261 rep.setValue("error", "QueryEngineEvent create failed");
265 res = pQueryEngine->registerQueryEvent(queryEngineEvent);
269 rep.setValue("error", "RegisterQueryEvent failed");
273 rep.setValue("queryEngineId", sstream.str());
275 else if (rep.getValue<std::string>("command") == "ReleaseQueryEngine")
277 pQueryEngine = (IQueryEngine *) std::stoi(
278 rep.getValue<std::string>("queryEngineId"));
280 ReleaseQueryEngine(pQueryEngine);
282 else if (rep.getValue<std::string>("command") == "ExecuteContextQuery")
286 pQueryEngine = (IQueryEngine *) std::stoi(
287 rep.getValue<std::string>("queryEngineId"));
289 res = pQueryEngine->executeContextQuery(
290 rep.getValue<std::string>("contextQuery"), &CQID);
294 rep.setValue("error", "ExecuteContextQuery failed");
300 rep.setValue("CQID", sstream.str());
302 else if (rep.getValue<std::string>("command") == "KillContextQuery")
304 pQueryEngine = (IQueryEngine *) std::stoi(
305 rep.getValue<std::string>("queryEngineId"));
307 res = pQueryEngine->killContextQuery(std::stoi(rep.getValue<std::string>("CQID")));
311 rep.setValue("error", "KillContextQuery failed");
319 response->setErrorCode(200);
320 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
323 else if (requestType == "POST")
325 // POST request operations
327 else if (requestType == "DELETE")
329 // DELETE request operations
333 if (requestFlag & RequestHandlerFlag::ObserverFlag)
335 // perform observe related operations on the resource.
339 return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;