Imported Upstream version 1.0.0
[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 generator for value manipulation
79     std::vector<AttributeGenerator> attributeGenList;
80     for (auto &attributeElement : m_rep->getAttributes())
81         attributeGenList.push_back(AttributeGenerator(attributeElement.second));
82
83     if (!attributeGenList.size())
84     {
85         OC_LOG(ERROR, TAG, "Zero attribute found from resource model!");
86         return;
87     }
88
89     bool hasNext = false;
90
91     do
92     {
93         if (!m_stopRequested)
94         {
95             // Get the next possible queryParameter
96             std::map<std::string, std::string> queryParam = m_queryParamGen.next();
97
98             while (true)
99             {
100                 SimulatorResourceModelSP repModel(new SimulatorResourceModel);
101                 for (auto & attributeGen : attributeGenList)
102                 {
103                     if (attributeGen.hasNext())
104                     {
105                         SimulatorResourceModel::Attribute attribute;
106                         if (true == attributeGen.next(attribute))
107                             repModel->addAttribute(attribute);
108
109                         hasNext = true;
110                     }
111                     else
112                     {
113                         SimulatorResourceModel::Attribute attribute;
114                         if (true == attributeGen.previous(attribute))
115                             repModel->addAttribute(attribute);
116                     }
117                 }
118
119                 if (hasNext)
120                 {
121                     // Send the request
122                     m_requestSender->sendRequest(queryParam, repModel,
123                             std::bind(&PUTRequestGenerator::onResponseReceived, this,
124                             std::placeholders::_1, std::placeholders::_2), true);
125
126                     m_requestCnt++;
127                     hasNext = false;
128                     continue;
129                 }
130                 else
131                 {
132                     break;
133                 }
134             }
135         }
136     }
137     while (m_queryParamGen.hasNext());
138
139     m_requestsSent = true;
140     completed();
141 }
142
143 void PUTRequestGenerator::onResponseReceived(SimulatorResult result,
144         SimulatorResourceModelSP repModel)
145 {
146     OC_LOG_V(INFO, TAG, "Response recieved result:%d", result);
147     m_responseCnt++;
148     completed();
149 }
150
151 void PUTRequestGenerator::completed()
152 {
153     if (m_requestCnt == m_responseCnt)
154     {
155         if (m_stopRequested)
156         {
157             OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
158             m_callback(m_id, OP_ABORT);
159         }
160         else
161         {
162             OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
163             m_callback(m_id, OP_COMPLETE);
164         }
165     }
166 }