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 "put_request_generator.h"
22 #include "request_model.h"
23 #include "simulator_exceptions.h"
26 #define TAG "PUT_REQUEST_GEN"
28 PUTRequestGenerator::PUTRequestGenerator(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 PUTRequestGenerator::startSending()
39 // Create thread and start sending requests in dispatched thread
40 m_thread.reset(new std::thread(&PUTRequestGenerator::SendAllRequests, this));
44 void PUTRequestGenerator::stopSending()
46 m_stopRequested = true;
49 void PUTRequestGenerator::SendAllRequests()
51 OC_LOG(DEBUG, TAG, "Sending OP_START event");
52 m_callback(m_id, OP_START);
54 std::shared_ptr<SimulatorResourceModelSchema> repSchema =
55 m_requestSchema->getRequestRepSchema();
59 OC_LOG(ERROR, TAG, "Request representation model is null!");
60 m_callback(m_id, OP_ABORT);
64 SimulatorResourceModel representation = repSchema->buildResourceModel();
66 // Create attribute combination generator for generating resource model
67 // with different attribute values
68 std::vector<SimulatorResourceAttribute> attributes;
69 for (auto &attributeElement : representation.getAttributeValues())
71 SimulatorResourceAttribute attribute;
72 attribute.setName(attributeElement.first);
73 attribute.setValue(attributeElement.second);
74 attribute.setProperty(repSchema->get(attributeElement.first));
75 attributes.push_back(attribute);
78 if (!attributes.size())
80 OC_LOG(ERROR, TAG, "Zero attribute found from resource model!");
81 m_callback(m_id, OP_COMPLETE);
85 QPGenerator queryParamGen(m_requestSchema->getQueryParams());
88 // Get the next possible queryParameter
89 std::map<std::string, std::string> queryParam = queryParamGen.next();
91 AttributeCombinationGen attrCombGen(attributes);
93 // Get the new model from attribute combination generator
94 SimulatorResourceModel resModel;
95 while (!m_stopRequested && attrCombGen.next(resModel))
98 m_requestSender.send(queryParam, resModel,
99 std::bind(&PUTRequestGenerator::onResponseReceived, this,
100 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
105 while (!m_stopRequested && queryParamGen.hasNext());
110 void PUTRequestGenerator::onResponseReceived(SimulatorResult result,
111 const SimulatorResourceModel &repModel, const RequestInfo &reqInfo)
113 OC_LOG(DEBUG, TAG, "Response recieved");
118 void PUTRequestGenerator::completed()
120 if (m_requestCnt == m_responseCnt)
124 OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
125 m_callback(m_id, OP_ABORT);
129 OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
130 m_callback(m_id, OP_COMPLETE);