1 /******************************************************************
3 * Copyright 2015 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 "post_request_generator.h"
22 #include "request_model.h"
23 #include "simulator_exceptions.h"
26 #define TAG "POST_REQUEST_GEN"
28 POSTRequestGenerator::POSTRequestGenerator(int id,
29 const std::shared_ptr<OC::OCResource> &ocResource,
30 const std::shared_ptr<RequestModel> &requestSchema,
31 RequestGeneration::ProgressStateCallback callback)
32 : RequestGeneration(RequestType::RQ_TYPE_GET, id, callback),
33 m_stopRequested(false),
34 m_requestSchema(requestSchema),
35 m_requestSender(ocResource) {}
37 void POSTRequestGenerator::startSending()
39 // Create thread and start sending requests in dispatched thread
40 m_thread.reset(new std::thread(&POSTRequestGenerator::SendAllRequests, this));
44 void POSTRequestGenerator::stopSending()
46 m_stopRequested = true;
49 void POSTRequestGenerator::SendAllRequests()
51 // Notify the progress status
52 OIC_LOG(DEBUG, TAG, "Sending OP_START event");
53 m_callback(m_id, OP_START);
55 std::shared_ptr<SimulatorResourceModelSchema> repSchema =
56 m_requestSchema->getRequestRepSchema();
60 OIC_LOG(ERROR, TAG, "Request representation model is null!");
61 m_callback(m_id, OP_COMPLETE);
65 // Create attribute generator for value manipulation
66 std::vector<AttributeGenerator> attributeGenList;
67 for (auto &propertyElement : repSchema->getChildProperties())
68 attributeGenList.push_back(AttributeGenerator(propertyElement.first, propertyElement.second));
70 if (!attributeGenList.size())
72 OIC_LOG(ERROR, TAG, "Zero attribute found from resource model!");
76 QPGenerator queryParamGen(m_requestSchema->getQueryParams());
80 // Get the next possible queryParameter
81 std::map<std::string, std::string> queryParam = queryParamGen.next();
83 for (auto &attributeGen : attributeGenList)
85 if (m_stopRequested) break;
87 while (!m_stopRequested && attributeGen.hasNext())
89 SimulatorResourceModel repModel;
90 SimulatorResourceAttribute attribute;
91 if (true == attributeGen.next(attribute))
92 repModel.add(attribute.getName(), attribute.getValue());
95 m_requestSender.send(queryParam, repModel,
96 std::bind(&POSTRequestGenerator::onResponseReceived,
97 this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
103 while (!m_stopRequested && queryParamGen.hasNext());
108 void POSTRequestGenerator::onResponseReceived(SimulatorResult result,
109 const SimulatorResourceModel &repModel, const RequestInfo &reqInfo)
111 OIC_LOG(DEBUG, TAG, "Response recieved");
116 void POSTRequestGenerator::completed()
118 if (m_requestCnt == m_responseCnt)
122 OIC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
123 m_callback(m_id, OP_ABORT);
127 OIC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
128 m_callback(m_id, OP_COMPLETE);