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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
// -*- mode: c++; c-basic-offset: 2; -*-
#include "common.hh"
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <deque>
#include <mutex>
#include <string.h>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "gui_attrtext.hh"
#include "gui_plainattrtext.hh"
#include "http_protocol.hh"
#include "io.hh"
#include "looper.hh"
#include "protocol.hh"
#include "protocols.hh"
namespace {
class ProtocolsImpl : public Protocols {
public:
ProtocolsImpl(size_t workers, size_t buffer, size_t cache, Looper* looper,
Listener* listener)
: listener_(listener), workers_size_(
std::max(static_cast<size_t>(1), workers)), buffer_size_(buffer),
cache_size_(cache), looper_(looper),
wanted_(std::string::npos), quit_(false) {
protocols_.emplace_back(HttpProtocol::create());
wanted_poke_.open();
if (wanted_poke_) {
looper_->add(wanted_poke_.read(), Looper::EVENT_READ,
std::bind(&ProtocolsImpl::notify, this,
std::placeholders::_1,
std::placeholders::_2));
}
}
~ProtocolsImpl() override {
{
std::unique_lock<std::mutex> lock(mutex_);
quit_ = true;
cond_.notify_all();
}
for (auto& worker : workers_) {
worker.join();
}
if (wanted_poke_) {
looper_->remove(wanted_poke_.read());
}
}
void add(size_t id, void const* data, size_t size) override {
std::unique_lock<std::mutex> lock(mutex_);
auto& entry = entries_[id];
if (entry.data_) {
assert(false);
return;
}
entry.data_ = data;
entry.size_ = size;
if (size) reschedule_with_lock();
}
void update(size_t id, void const* data, size_t size) override {
std::unique_lock<std::mutex> lock(mutex_);
auto& entry = entries_[id];
entry.data_ = data;
if (entry.size_ == size) return;
auto old = entry.size_;
entry.size_ = size;
if (old >= buffer_size_) return;
for (auto it = cache_.begin(); it != cache_.end(); ++it) {
if (it->id_ == id) {
queue_.emplace_back(id, it->size_,
std::move(it->match_), std::move(it->text_));
cache_.erase(it);
if (active_.size() < workers_size_) cond_.notify_one();
return;
}
}
for (auto const& active : active_) {
if (active == id) {
invalidate_.emplace(id);
return;
}
}
if (old == 0) reschedule_with_lock();
}
void remove(size_t id) override {
auto it = entries_.find(id);
if (it == entries_.end()) {
assert(false);
return;
}
std::unique_lock<std::mutex> lock(mutex_);
for (auto it = cache_.begin(); it != cache_.end(); ++it) {
if (it->id_ == id) {
cache_.erase(it);
reschedule_with_lock();
return;
}
}
if (wanted_ == id) wanted_ = std::string::npos;
for (auto it = active_.begin(); it != active_.end(); ++it) {
if (*it == id) {
active_.erase(it);
reschedule_with_lock();
return;
}
}
for (auto it = queue_.begin(); it != queue_.end(); ++it) {
if (it->id_ == id) {
queue_.erase(it);
reschedule_with_lock();
return;
}
}
}
void clear() override {
std::unique_lock<std::mutex> lock(mutex_);
entries_.clear();
cache_.clear();
wanted_ = std::string::npos;
active_.clear();
queue_.clear();
}
void text(size_t id) override {
auto it = entries_.find(id);
if (it == entries_.end()) {
assert(false);
return;
}
std::unique_lock<std::mutex> lock(mutex_);
for (auto& entry : cache_) {
if (entry.id_ == id) {
entry.last_ = std::chrono::steady_clock::now();
if (!entry.text_) {
// Already an active entry
assert(false);
return;
}
std::unique_ptr<AttributedText> text(std::move(entry.text_));
std::string name(entry.match_ ? entry.match_->name() : "");
lock.unlock();
listener_->text(this, id, name, std::move(text));
return;
}
}
wanted_ = id;
for (auto& active : active_) {
if (active == id) {
// Already working on it
return;
}
}
for (auto it = queue_.begin(); it != queue_.end(); ++it) {
if (it->id_ == id) {
// Already in queue, remove so we can add it first
if (it == queue_.begin()) {
return;
}
auto offset = it->offset_;
std::unique_ptr<Protocol::Match> match(std::move(it->match_));
std::unique_ptr<AttributedText> text(std::move(it->text_));
queue_.erase(it);
queue_.emplace_front(id, offset, std::move(match), std::move(text));
return;
}
}
queue_.emplace_front(id);
if (active_.size() < workers_size_) cond_.notify_one();
}
void free(size_t id, std::unique_ptr<AttributedText>&& text) override {
std::unique_lock<std::mutex> lock(mutex_);
for (auto& entry : cache_) {
if (entry.id_ == id) {
if (entry.text_) {
// Entry updated while shown
return;
}
entry.text_.swap(text);
break;
}
}
}
void content(size_t id, std::ostream* out) override {
auto it = entries_.find(id);
if (it == entries_.end()) {
assert(false);
return;
}
std::unique_lock<std::mutex> lock(mutex_);
content_queue_.emplace_back(id, out);
if (active_.size() < workers_size_) cond_.notify_one();
}
private:
struct Entry {
void const* data_;
size_t size_;
Entry()
: data_(nullptr), size_(0) {
}
};
struct CacheEntry {
std::chrono::steady_clock::time_point last_;
size_t id_;
size_t size_;
std::unique_ptr<Protocol::Match> match_;
std::unique_ptr<AttributedText> text_;
CacheEntry(size_t id, size_t size, std::unique_ptr<Protocol::Match>&& match,
std::unique_ptr<AttributedText>&& text)
: last_(std::chrono::steady_clock::now()), id_(id), size_(size),
match_(std::move(match)), text_(std::move(text)) {
}
};
struct QueueEntry {
size_t id_;
size_t offset_;
std::unique_ptr<Protocol::Match> match_;
std::unique_ptr<AttributedText> text_;
QueueEntry(size_t id)
: id_(id), offset_(0) {
}
QueueEntry(size_t id, size_t offset,
std::unique_ptr<Protocol::Match>&& match,
std::unique_ptr<AttributedText>&& text)
: id_(id), offset_(offset), match_(std::move(match)),
text_(std::move(text)) {
}
};
struct ContentEntry {
size_t id_;
std::string name_;
std::ostream* out_;
ContentEntry(size_t id, std::ostream* out)
: id_(id), out_(out) {
}
ContentEntry(size_t id, std::string const& name, std::ostream* out)
: id_(id), name_(name), out_(out) {
}
};
void reschedule_with_lock() {
if (cache_.size() < cache_size_) {
size_t want = cache_size_ - cache_.size();
size_t queued = active_.size() + queue_.size();
if (want < queued) {
want = 0;
} else {
want -= queued;
}
if (want) {
std::unordered_set<size_t> taken;
for (auto const& cache : cache_) taken.insert(cache.id_);
for (auto const& active : active_) taken.insert(active);
for (auto const& queue : queue_) taken.insert(queue.id_);
for (auto const& pair : entries_) {
if (pair.second.size_ == 0) continue; // No point caching 0 bytes
if (taken.count(pair.first)) continue; // Already in cache or queues
queue_.emplace_back(pair.first);
if (!--want) break;
}
}
}
if (queue_.empty()) return;
if (active_.size() == workers_size_) return;
while (workers_.size() < workers_size_) {
workers_.emplace_back(&ProtocolsImpl::worker, this, buffer_size_);
}
cond_.notify_all();
}
void worker(size_t buffer_size) {
std::unique_ptr<AttributedText> text;
std::unique_ptr<Protocol::Match> match;
std::ostream* out = nullptr;
size_t id = 0;
size_t offset = 0;
size_t size;
std::unique_ptr<char[]> buf(new char[buffer_size]);
char const* ptr;
size_t fill;
while (true) {
{
std::unique_lock<std::mutex> lock(mutex_);
if (out) {
content_done_with_lock(id, match ? match->name() : "", out);
out = nullptr;
id = 0;
match.reset();
} else if (text) {
cache_entry_with_lock(id, size, std::move(match), std::move(text));
assert(!match);
assert(!text);
id = 0;
offset = 0;
}
while (true) {
if (quit_) return;
if (!content_queue_.empty()) {
id = content_queue_.front().id_;
out = content_queue_.front().out_;
content_queue_.pop_front();
break;
}
if (!queue_.empty()) {
id = queue_.front().id_;
offset = queue_.front().offset_;
match.swap(queue_.front().match_);
text.swap(queue_.front().text_);
queue_.pop_front();
active_.emplace_back(id);
break;
}
cond_.wait(lock);
}
auto const& entry = entries_[id];
size = entry.size_;
if (out) {
fill = size;
ptr = reinterpret_cast<char const*>(entry.data_);
} else {
fill = std::min(size, buffer_size);
memcpy(buf.get(), entry.data_, fill);
ptr = buf.get();
}
}
if (!out && !text) text.reset(AttributedText::create());
if (!match) {
for (auto const& protocol : protocols_) {
match.reset(protocol->match(ptr, fill));
if (match) break;
}
offset = 0;
}
if (match) {
if (out) {
if (!match->content(ptr, fill, out)) {
out->write(ptr, fill);
}
} else {
if (offset == 0) {
match->full(ptr, fill, text.get());
} else {
match->append(ptr, offset, fill, text.get());
}
}
}
}
}
void cache_entry_with_lock(size_t id, size_t size,
std::unique_ptr<Protocol::Match>&& match,
std::unique_ptr<AttributedText>&& text) {
active_.erase(std::find(active_.begin(), active_.end(), id));
if (invalidate_.erase(id)) {
if (wanted_ == id) {
queue_.emplace_front(id, size, std::move(match), std::move(text));
} else {
queue_.emplace_back(id, size, std::move(match), std::move(text));
}
return;
}
while (cache_.size() >= cache_size_) {
auto oldest = cache_.end();
for (auto it = cache_.begin(); it != cache_.end(); ++it) {
if (!it->text_) continue; // Do not remove active
if (oldest == cache_.end() || it->last_ < oldest->last_) {
oldest = it;
}
}
if (oldest == cache_.end()) break;
cache_.erase(oldest);
}
cache_.emplace_back(id, size, std::move(match), std::move(text));
if (wanted_ == id && wanted_poke_) {
io::write(wanted_poke_.write(), "a", 1);
}
}
void content_done_with_lock(size_t id, std::string const& name,
std::ostream* out) {
content_done_.emplace_back(id, name, out);
if (wanted_poke_) {
io::write(wanted_poke_.write(), "b", 1);
}
}
void notify(int fd, uint8_t) {
char tmp[10];
io::read(fd, tmp, sizeof(tmp));
std::unique_lock<std::mutex> lock(mutex_);
while (!content_done_.empty()) {
ContentEntry entry(content_done_.front());
content_done_.pop_front();
lock.unlock();
listener_->content(this, entry.id_, entry.name_, entry.out_);
lock.lock();
}
if (wanted_ == std::string::npos) return;
for (auto& entry : cache_) {
if (entry.id_ == wanted_) {
wanted_ = std::string::npos;
entry.last_ = std::chrono::steady_clock::now();
if (!entry.text_) {
// Already an active entry
assert(false);
return;
}
auto id = entry.id_;
std::unique_ptr<AttributedText> text(std::move(entry.text_));
std::string name(entry.match_ ? entry.match_->name() : "");
lock.unlock();
listener_->text(this, id, name, std::move(text));
return;
}
}
}
Listener* const listener_;
size_t const workers_size_;
size_t const buffer_size_;
size_t const cache_size_;
Looper* const looper_;
std::unordered_map<size_t, Entry> entries_;
std::vector<std::unique_ptr<Protocol>> protocols_;
std::vector<std::thread> workers_;
io::auto_pipe wanted_poke_;
std::mutex mutex_;
std::condition_variable cond_;
size_t wanted_;
bool quit_;
std::vector<CacheEntry> cache_;
std::vector<size_t> active_;
std::deque<QueueEntry> queue_;
std::unordered_set<size_t> invalidate_;
std::deque<ContentEntry> content_done_;
std::deque<ContentEntry> content_queue_;
};
} // namespace
// static
Protocols* Protocols::create(size_t workers, size_t buffer, size_t cache,
Looper* looper, Listener* listener) {
assert(listener);
return new ProtocolsImpl(workers, buffer, cache, looper, listener);
}
|