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 generator for value manipulation
79 std::vector<AttributeGenerator> attributeGenList;
80 for (auto &attributeElement : m_rep->getAttributes())
81 attributeGenList.push_back(AttributeGenerator(attributeElement.second));
83 if (!attributeGenList.size())
85 OC_LOG(ERROR, TAG, "Zero attribute found from resource model!");
95 // Get the next possible queryParameter
96 std::map<std::string, std::string> queryParam = m_queryParamGen.next();
100 SimulatorResourceModelSP repModel(new SimulatorResourceModel);
101 for (auto & attributeGen : attributeGenList)
103 if (attributeGen.hasNext())
105 SimulatorResourceModel::Attribute attribute;
106 if (true == attributeGen.next(attribute))
107 repModel->addAttribute(attribute);
113 SimulatorResourceModel::Attribute attribute;
114 if (true == attributeGen.previous(attribute))
115 repModel->addAttribute(attribute);
122 m_requestSender->sendRequest(queryParam, repModel,
123 std::bind(&PUTRequestGenerator::onResponseReceived, this,
124 std::placeholders::_1, std::placeholders::_2), true);
137 while (m_queryParamGen.hasNext());
139 m_requestsSent = true;
143 void PUTRequestGenerator::onResponseReceived(SimulatorResult result,
144 SimulatorResourceModelSP repModel)
146 OC_LOG_V(INFO, TAG, "Response recieved result:%d", result);
151 void PUTRequestGenerator::completed()
153 if (m_requestCnt == m_responseCnt)
157 OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
158 m_callback(m_id, OP_ABORT);
162 OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
163 m_callback(m_id, OP_COMPLETE);