-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
207 lines (176 loc) · 5.17 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
/*
* File: main.cpp
* Author: mayur
*
* Created on 13 July, 2016, 11:58 PM
*/
#include "Poco/Notification.h"
#include "Poco/NotificationQueue.h"
#include "Poco/ThreadPool.h"
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include "Poco/Mutex.h"
#include "Poco/Random.h"
#include "Poco/AutoPtr.h"
#include "Poco/URI.h"
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/StreamCopier.h"
#include <iostream>
using Poco::Notification;
using Poco::NotificationQueue;
using Poco::ThreadPool;
using Poco::Thread;
using Poco::Runnable;
using Poco::FastMutex;
using Poco::Mutex;
using Poco::AutoPtr;
using Poco::Net::HTTPClientSession;
using Poco::URI;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPMessage;
using Poco::Exception;
using Poco::Net::HTTPResponse;
using Poco::StreamCopier;
/*
*
*/
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;
};
class ResponseNotification: public Notification
// The notification sent to worker threads.
{
public:
typedef AutoPtr<ResponseNotification> Ptr;
ResponseNotification(Response *res):
response(res)
{
}
std::string getResponse() const{
return response->getResponseString();
}
private:
Response* response;
};
class CommonMutex{
public:
static FastMutex commonMutex;
};
class Worker: public Runnable
// A worker thread that gets work items
// from a NotificationQueue.
{
public:
Worker(const std::string& name, NotificationQueue& queue):
_name(name),
_queue(queue)
{
}
void run()
{
Poco::Random rnd;
for (;;)
{
Notification::Ptr pNf(_queue.waitDequeueNotification());
if (pNf)
{
ResponseNotification::Ptr pResponseNf = pNf.cast<ResponseNotification>();
if (pResponseNf)
{
{
FastMutex::ScopedLock lock(CommonMutex::commonMutex);
std::cout << _name << " got response " << pResponseNf->getResponse() << std::endl;
}
}
}
else break;
}
}
private:
std::string _name;
NotificationQueue& _queue;
};
class Post{
public:
std::string doGet(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 "";
}
};
class MyProducer: public Runnable
// A worker thread that gets work items
// from a NotificationQueue.
{
private:
std::string _name;
NotificationQueue& _queue;
public:
MyProducer(const std::string& name, NotificationQueue& queue):
_name(name),
_queue(queue)
{
}
void run()
{
Post p;
Poco::Random rnd;
for(int i = 1; i < 50; i++){
{
int id = rnd.next(100);
Response *res = new Response(p.doGet(id));
_queue.enqueueNotification(new ResponseNotification(res));
{
FastMutex::ScopedLock lock(CommonMutex::commonMutex);
std::cout<<_name<<" created request for id "<<id<<std::endl;
}
}
}
std::cout<<"Producer completed it's job"<<std::endl;
}
};
FastMutex CommonMutex::commonMutex;
int main(int argc, char** argv)
{
NotificationQueue queue;
// create some worker threads
Worker worker1("Worker 1", queue);
Worker worker2("Worker 2", queue);
Worker worker3("Worker 3", queue);
MyProducer producer1("Producer1", queue);
Thread tWorker1, tWorker2, tWorker3, tProducer1;
tWorker1.start(worker1);
tWorker2.start(worker2);
tWorker3.start(worker3);
//Thread::sleep(3000);
tProducer1.start(producer1);
tProducer1.join();
Thread::sleep(20000);
// stop all worker threads
queue.wakeUpAll();
return 0;
}