Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / simulator / src / client-controller / put_request_generator.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "put_request_generator.h"
22 #include "simulator_resource_model.h"
23 #include "logger.h"
24
25 #define TAG "PUT_REQUEST_GEN"
26
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),
32         m_status(false),
33         m_stopRequested(false) {}
34
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),
42         m_status(false),
43         m_stopRequested(false) {}
44
45 void PUTRequestGenerator::startSending()
46 {
47     // Check the representation
48     if (!m_rep)
49     {
50         OC_LOG(ERROR, TAG, "Invalid Representation given!");
51         throw SimulatorException(SIMULATOR_ERROR, "Invalid representation detected!");
52     }
53
54     // Check if the operation is already in progress
55     std::lock_guard<std::mutex> lock(m_statusLock);
56     if (m_status)
57     {
58         OC_LOG(ERROR, TAG, "Operation already in progress !");
59         throw OperationInProgressException("Another PUT request generation session is already in progress!");
60     }
61
62     // Create thread and start sending requests in dispatched thread
63     m_thread = std::make_shared<std::thread>(&PUTRequestGenerator::SendAllRequests, this);
64     m_status = true;
65     m_thread->detach();
66 }
67
68 void PUTRequestGenerator::stopSending()
69 {
70     m_stopRequested = true;
71 }
72
73 void PUTRequestGenerator::SendAllRequests()
74 {
75     OC_LOG(DEBUG, TAG, "Sending OP_START event");
76     m_callback(m_id, OP_START);
77
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())
82     {
83         attributes.push_back(attributeElement.second);
84     }
85
86     if (!attributes.size())
87     {
88         OC_LOG(ERROR, TAG, "Zero attribute found from resource model!");
89         return;
90     }
91
92     do
93     {
94         if (m_stopRequested)
95         {
96             break;
97         }
98
99         // Get the next possible queryParameter
100         std::map<std::string, std::string> queryParam = m_queryParamGen.next();
101
102         AttributeCombinationGen attrCombGen(attributes);
103
104         // Get the new model from attribute combination generator
105         SimulatorResourceModel resModel;
106         while (!m_stopRequested && attrCombGen.next(resModel))
107         {
108             SimulatorResourceModelSP repModel(new SimulatorResourceModel(resModel));
109
110             // Send the request
111             m_requestSender->sendRequest(queryParam, repModel,
112                     std::bind(&PUTRequestGenerator::onResponseReceived, this,
113                     std::placeholders::_1, std::placeholders::_2), true);
114
115             m_requestCnt++;
116         }
117     }
118     while (m_queryParamGen.hasNext());
119
120     m_requestsSent = true;
121     completed();
122 }
123
124 void PUTRequestGenerator::onResponseReceived(SimulatorResult result,
125         SimulatorResourceModelSP repModel)
126 {
127     OC_LOG_V(INFO, TAG, "Response recieved result:%d", result);
128     m_responseCnt++;
129     completed();
130 }
131
132 void PUTRequestGenerator::completed()
133 {
134     if (m_requestCnt == m_responseCnt)
135     {
136         if (m_stopRequested)
137         {
138             OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
139             m_callback(m_id, OP_ABORT);
140         }
141         else
142         {
143             OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
144             m_callback(m_id, OP_COMPLETE);
145         }
146     }
147 }