Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / simulator / src / client-controller / get_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 "get_request_generator.h"
22 #include "simulator_resource_model.h"
23 #include "logger.h"
24
25 #define TAG "GET_REQUEST_GEN"
26
27 GETRequestGenerator::GETRequestGenerator(int id, RequestSenderSP &requestSender,
28         AutoRequestGeneration::ProgressStateCallback callback)
29     :   AutoRequestGeneration(RequestType::RQ_TYPE_GET, id, requestSender, callback),
30         m_status(false),
31         m_stopRequested(false) {}
32
33 GETRequestGenerator::GETRequestGenerator(int id, RequestSenderSP &requestSender,
34         const std::map<std::string, std::vector<std::string>> &queryParams,
35         AutoRequestGeneration::ProgressStateCallback callback)
36     :   AutoRequestGeneration(RequestType::RQ_TYPE_GET, id, requestSender, callback),
37         m_queryParamGen(queryParams),
38         m_status(false),
39         m_stopRequested(false) {}
40
41 void GETRequestGenerator::startSending()
42 {
43     // Check if the operation is already in progress
44     std::lock_guard<std::mutex> lock(m_statusLock);
45     if (m_status)
46     {
47         OC_LOG(ERROR, TAG, "Operation already in progress !");
48         throw OperationInProgressException("Another GET request generation session is already in progress!");
49     }
50
51     // Create thread and start sending requests in dispatched thread
52     m_thread = std::make_shared<std::thread>(&GETRequestGenerator::SendAllRequests, this);
53     m_status = true;
54     m_thread->detach();
55 }
56
57 void GETRequestGenerator::stopSending()
58 {
59     m_stopRequested = true;
60 }
61
62 void GETRequestGenerator::SendAllRequests()
63 {
64     // Notify the progress status
65     OC_LOG(DEBUG, TAG, "Sending OP_START event");
66     m_callback(m_id, OP_START);
67
68     do
69     {
70         if (!m_stopRequested)
71         {
72             // Get the next possible queryParameter
73             std::map<std::string, std::string> queryParam = m_queryParamGen.next();
74
75             // Send the request
76             try
77             {
78                 m_requestSender->sendRequest(queryParam, std::bind(&GETRequestGenerator::onResponseReceived, this,
79                                              std::placeholders::_1, std::placeholders::_2), true);
80                 m_requestCnt++;
81             }
82             catch (SimulatorException &e)
83             {
84                 m_stopRequested = true;
85                 return completed();
86             }
87         }
88     }
89     while (m_queryParamGen.hasNext());
90
91     m_requestsSent = true;
92     completed();
93 }
94
95 void GETRequestGenerator::onResponseReceived(SimulatorResult result,
96         SimulatorResourceModelSP repModel)
97 {
98     OC_LOG_V(INFO, TAG, "Response recieved result:%d", result);
99     m_responseCnt++;
100     completed();
101 }
102
103 void GETRequestGenerator::completed()
104 {
105     if (m_requestCnt == m_responseCnt)
106     {
107         if (m_stopRequested)
108         {
109             OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
110             m_callback(m_id, OP_ABORT);
111         }
112         else
113         {
114             OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
115             m_callback(m_id, OP_COMPLETE);
116         }
117     }
118 }