-
Notifications
You must be signed in to change notification settings - Fork 0
/
liberal-news-aggregator.cc
74 lines (63 loc) · 2.33 KB
/
liberal-news-aggregator.cc
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
/**
* File: liberal-news-aggregator.cc
* --------------------------------
* Presents the implementation of the LiberalNewsAggregator class.
*/
#include "liberal-news-aggregator.h"
using namespace std;
static const int kNumFeedThreads = 6;
static const int kNumThreadsPerServer = 6;
static const int kMaxThreads = 24;
static semaphore feedThreads(kNumFeedThreads);
static semaphore totalThreads(kMaxThreads);
//Server Locks
LiberalNewsAggregator::LiberalNewsAggregator(const string& rssFeedListURI, bool verbose) :
NewsAggregator(rssFeedListURI, verbose) {}
void LiberalNewsAggregator::processArticle(Article article){
totalThreads.signal(on_thread_exit);
mapLock.lock();
serverLocks[getURLServer(article.url)]->signal(on_thread_exit);
mapLock.unlock();
vector<string> tokens = NewsAggregator::getTokenVector(article.url);
if(tokens.empty()) return;
NewsAggregator::updateIndex(article, tokens);
}
void LiberalNewsAggregator::processFeed(const string& Url){
feedThreads.signal(on_thread_exit);
totalThreads.signal(on_thread_exit);
RSSFeed feed(Url);
if(!NewsAggregator::processFeed(&feed)) return;
vector<Article>& articles = (vector<Article>&)feed.getArticles();
sort(articles.begin(), articles.end());
if(articles.empty()) return;
vector<thread> v;
for(Article article : articles){
if(NewsAggregator::containsUrl(article.url)) continue;
string server = getURLServer(article.url);
mapLock.lock();
unique_ptr<semaphore>& up = serverLocks[server];
if(up == nullptr) up.reset(new semaphore(kNumThreadsPerServer));
mapLock.unlock();
totalThreads.wait();
up->wait();
v.push_back(thread([this](Article article){
LiberalNewsAggregator::processArticle(article);
}, article));
}
for(thread& child : v)
child.join();
}
void LiberalNewsAggregator::processAllFeeds() {
map<string, string> feeds = NewsAggregator::getFeedMap();
if(feeds.empty()) return;
vector<thread> v;
for(auto& feed : feeds){
feedThreads.wait();
totalThreads.wait();
v.push_back(thread([this](const string& Url){
LiberalNewsAggregator::processFeed(Url);
}, feed.first));
}
for(thread& child : v)
child.join();
}