-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
297 lines (260 loc) · 8.52 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* File: main.cpp
* Author: mayur
*
* Created on 14 July, 2016, 5:50 PM
*/
#include <cstdlib>
#include <iostream>
#include <Poco/NotificationQueue.h>
#include <Poco/ThreadPool.h>
#include <Poco/Thread.h>
#include <Poco/Mutex.h>
#include <Poco/URI.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPMessage.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/StreamCopier.h>
using Poco::NotificationQueue;
using Poco::ThreadPool;
using Poco::Notification;
using Poco::Thread;
using Poco::FastMutex;
using Poco::URI;
using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPMessage;
using Poco::Net::HTTPResponse;
using Poco::StreamCopier;
using Poco::AutoPtr;
/*
*
*
*/
/*
* Class to hold the response object.
* The response string will be the one obtained from jsonplaceholder.
*/
class Response {
public:
Response(std::string s){
resStr = s;
}
void setResponseString(std::string s){
resStr = s;
}
std::string getResponseString(){
return resStr;
}
private:
std::string resStr;
};
/*
* ResponseNotification class derives the Poco::Notification class.
* These objects are notifications to the NotificationQueue.
*/
class ResponseNotification: public Notification
// The notification sent to worker threads.
{
public:
typedef AutoPtr<ResponseNotification> Ptr;
ResponseNotification(Response *res):
response(res)
{
}
Response* getResponse() const{
return response;
}
private:
Response* response;
};
/*
* Request class holds the requestID and the address of the response queue where the response needs to be sent.
* When the subscriber thread receives the response, it will notify the observer thread on this responseQueue.
*/
class Request {
public:
int id;
Poco::NotificationQueue* resQ;
std::string observerName;
Request(int reqID, Poco::NotificationQueue* resQueue, std::string obsName):
id(reqID),
resQ(resQueue),
observerName(obsName)
{
}
};
/*
* RequestNotification class derives the Poco::Notification class.
* These objects are notifications to the NotificationQueue.
*/
class RequestNotification: public Poco::Notification{
public:
typedef Poco::AutoPtr<RequestNotification> Ptr;
RequestNotification(Request* req):
request(req)
{
}
Request* getRequest(){
return request;
}
private:
Request* request;
};
/*
* The Post class is used for performing the GET/UPDATE/DELETE/POST operations.
* The doMethod functions only queues the request objects to the Request Notification queue and returns.
* The subscriber threads waiting on the requestNotification queue picks up the Request notification.
* The actual sending and receiving of response is done in the Publisher thread.
*/
class Post{
public:
Post(NotificationQueue& requestQ):
reqQ(requestQ)
{
}
int doGet(int id, NotificationQueue* resQ, std::string obsName){
//std::cout<<"Called doGet with id : "<<id<<" and responseQueue address: "<<resQ<<" and request queue address is : "<<&reqQ<<std::endl;
reqQ.enqueueNotification(new RequestNotification(new Request(id, resQ, obsName)));
return 0;
}
private:
NotificationQueue& reqQ;
};
/*
* Mutex to synchronize printing between multiple threads.
*/
class CommonMutex{
public:
static FastMutex commonMutex;
};
FastMutex CommonMutex::commonMutex;
/*
* Observer thread class. This will call the API and wait on their response queue for response notifications.
*/
class Observer : public Poco::Runnable{
public:
Observer(const std::string& name, NotificationQueue* resQ, NotificationQueue& reqQ, Post& p):
_name(name),
_resQ(resQ),
_reqQ(reqQ),
post(p)
{
}
virtual void run() {
//std::cout<<"Observer thread created"<<std::endl;
//std::cout<<"Name is : "<<_name<<" request queue address is : "<<&_reqQ<<" resQ address is : "<<_resQ<<std::endl;
post.doGet(80, _resQ, _name);
post.doGet(90, _resQ, _name);
post.doGet(10, _resQ, _name);
post.doGet(30, _resQ, _name);
post.doGet(35, _resQ, _name);
while(1){
Notification::Ptr pNf(_resQ->waitDequeueNotification());
if(pNf){
ResponseNotification::Ptr pResponseNf = pNf.cast<ResponseNotification>();
if (pResponseNf){
FastMutex::ScopedLock lock(CommonMutex::commonMutex);
Response* res = pResponseNf->getResponse();
std::cout << _name << " got response " << res->getResponseString() << std::endl;
}
}
else
break;
}
}
private:
std::string _name;
NotificationQueue* _resQ;
NotificationQueue& _reqQ;
Post& post;
static FastMutex observerMutex;
};
FastMutex Observer::observerMutex;
/*
* Publisher Thread class. These threads will wait for Request Notifications.
* Once obtained, this will do the actual request and queue the response in the Response Notification queue
* which is obtained from the request object.
*/
class Publisher : public Poco::Runnable{
public:
Publisher(const std::string& name, NotificationQueue& queue):
_name(name),
_queue(queue)
{
}
virtual void run(){
while(1){
//std::cout<<"Publisher thread created"<<std::endl;
//std::cout<<"Name is "<<_name<<" request queue address is : "<<&_queue<<std::endl;
Notification::Ptr pNf(_queue.waitDequeueNotification());
if(pNf){
RequestNotification::Ptr pRequestNf = pNf.cast<RequestNotification>();
if(pRequestNf){
//FastMutex::ScopedLock lock(publisherMutex);
Request *req = pRequestNf->getRequest();
NotificationQueue* responseQueue = req->resQ;
int reqID = req->id;
{
FastMutex::ScopedLock lock(CommonMutex::commonMutex);
std::cout<< _name<<" got request with id : "<<reqID<<" from "<<req->observerName<<std::endl;
}
//delete[] req;
std::string result = getResponse(reqID);
responseQueue->enqueueNotification(new ResponseNotification(new Response(result)));
//std::cout << _name << " got request with id " << req->id <<" and response queue address for it is : "<<req->resQ<< std::endl;
}
}
else
break;
}
}
std::string getResponse(int id){
std::string result;
URI uri("http://jsonplaceholder.typicode.com/posts/" + std::to_string(id));
std::string path(uri.getPathAndQuery());
HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
HTTPResponse response;
session.sendRequest(request);
std::istream& rs = session.receiveResponse(response);
if(response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK){
StreamCopier::copyToString(rs, result);
return result;
}
else
return "";
}
private:
std::string _name;
NotificationQueue& _queue;
static FastMutex publisherMutex;
};
FastMutex Publisher::publisherMutex;
/*
* Entry function main.
* Publishers and Observers can be created here.
* There will be a NotificationQueue for each observer and only one NotificationQueue for Publisher.
*/
int main(int argc, char** argv) {
NotificationQueue reqQ;
NotificationQueue *resQ = new NotificationQueue;
NotificationQueue *resQ2 = new NotificationQueue;
std::cout<<"In main, request queue address is : "<<&reqQ<<" response queue address is : "<<resQ<<std::endl;
Post po(reqQ);
Publisher pub1("Publisher1", reqQ);
//Publisher pub2("Publisher2", reqQ);
Observer observer1("Observer1", resQ, reqQ, po);
Observer observer2("Observer2", resQ2, reqQ, po);
ThreadPool::defaultPool().start(pub1);
//ThreadPool::defaultPool().start(pub2);
ThreadPool::defaultPool().start(observer1);
ThreadPool::defaultPool().start(observer2);
Thread::sleep(6000);
reqQ.wakeUpAll();
resQ->wakeUpAll();
resQ2->wakeUpAll();
ThreadPool::defaultPool().joinAll();
return 0;
}