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 "simulator_resource_model.h"
25 #define TAG "PUT_REQUEST_GEN"
27 PUTRequestGenerator::PUTRequestGenerator(int id, RequestSenderSP &requestSender,
28 SimulatorResourceModelSP &representation,
29 AutoRequestGeneration::ProgressStateCallback callback)
30 : AutoRequestGeneration(RequestType::RQ_TYPE_GET, id, requestSender, callback),
31 m_rep(representation),
33 m_stopRequested(false) {}
35 PUTRequestGenerator::PUTRequestGenerator(int id, RequestSenderSP &requestSender,
36 const std::map<std::string, std::vector<std::string>> &queryParams,
37 SimulatorResourceModelSP &representation,
38 AutoRequestGeneration::ProgressStateCallback callback)
39 : AutoRequestGeneration(RequestType::RQ_TYPE_GET, id, requestSender, callback),
40 m_queryParamGen(queryParams),
41 m_rep(representation),
43 m_stopRequested(false) {}
45 void PUTRequestGenerator::startSending()
47 // Check the representation
50 OC_LOG(ERROR, TAG, "Invalid Representation given!");
51 throw SimulatorException(SIMULATOR_ERROR, "Invalid representation detected!");
54 // Check if the operation is already in progress
55 std::lock_guard<std::mutex> lock(m_statusLock);
58 OC_LOG(ERROR, TAG, "Operation already in progress !");
59 throw OperationInProgressException("Another PUT request generation session is already in progress!");
62 // Create thread and start sending requests in dispatched thread
63 m_thread = std::make_shared<std::thread>(&PUTRequestGenerator::SendAllRequests, this);
68 void PUTRequestGenerator::stopSending()
70 m_stopRequested = true;
73 void PUTRequestGenerator::SendAllRequests()
75 OC_LOG(DEBUG, TAG, "Sending OP_START event");
76 m_callback(m_id, OP_START);
78 // Create attribute combination generator for generating resource model
79 // with different attribute values
80 std::vector<SimulatorResourceModel::Attribute> attributes;
81 for (auto &attributeElement : m_rep->getAttributes())
83 attributes.push_back(attributeElement.second);
86 if (!attributes.size())
88 OC_LOG(ERROR, TAG, "Zero attribute found from resource model!");
99 // Get the next possible queryParameter
100 std::map<std::string, std::string> queryParam = m_queryParamGen.next();
102 AttributeCombinationGen attrCombGen(attributes);
104 // Get the new model from attribute combination generator
105 SimulatorResourceModel resModel;
106 while (!m_stopRequested && attrCombGen.next(resModel))
108 SimulatorResourceModelSP repModel(new SimulatorResourceModel(resModel));
111 m_requestSender->sendRequest(queryParam, repModel,
112 std::bind(&PUTRequestGenerator::onResponseReceived, this,
113 std::placeholders::_1, std::placeholders::_2), true);
118 while (m_queryParamGen.hasNext());
120 m_requestsSent = true;
124 void PUTRequestGenerator::onResponseReceived(SimulatorResult result,
125 SimulatorResourceModelSP repModel)
127 OC_LOG_V(INFO, TAG, "Response recieved result:%d", result);
132 void PUTRequestGenerator::completed()
134 if (m_requestCnt == m_responseCnt)
138 OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
139 m_callback(m_id, OP_ABORT);
143 OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
144 m_callback(m_id, OP_COMPLETE);