forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowableSubscriber.cpp
More file actions
76 lines (63 loc) · 1.84 KB
/
FlowableSubscriber.cpp
File metadata and controls
76 lines (63 loc) · 1.84 KB
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
#include "tck-test/FlowableSubscriber.h"
#include <thread>
#include <folly/Format.h>
using namespace folly;
namespace rsocket {
namespace tck {
FlowableSubscriber::FlowableSubscriber(int initialRequestN)
: initialRequestN_(initialRequestN) {}
void FlowableSubscriber::request(int n) {
LOG(INFO) << "... requesting " << n;
while (!subscription_) {
;
}
subscription_->request(n);
}
void FlowableSubscriber::cancel() {
LOG(INFO) << "... canceling ";
canceled_ = true;
if (auto subscription = std::move(subscription_)) {
subscription->cancel();
}
}
void FlowableSubscriber::onSubscribe(
yarpl::Reference<yarpl::flowable::Subscription> subscription) noexcept {
VLOG(4) << "OnSubscribe in FlowableSubscriber";
subscription_ = subscription;
if (initialRequestN_ > 0) {
subscription_->request(initialRequestN_);
}
}
void FlowableSubscriber::onNext(Payload element) noexcept {
LOG(INFO) << "... received onNext from Publisher: " << element;
{
std::unique_lock<std::mutex> lock(mutex_);
std::string data =
element.data ? element.data->moveToFbString().toStdString() : "";
std::string metadata = element.metadata
? element.metadata->moveToFbString().toStdString()
: "";
values_.push_back(std::make_pair(data, metadata));
++valuesCount_;
}
valuesCV_.notify_one();
}
void FlowableSubscriber::onComplete() noexcept {
LOG(INFO) << "... received onComplete from Publisher";
{
std::unique_lock<std::mutex> lock(mutex_);
completed_ = true;
}
terminatedCV_.notify_one();
}
void FlowableSubscriber::onError(folly::exception_wrapper ex) noexcept {
LOG(INFO) << "... received onError from Publisher";
{
std::unique_lock<std::mutex> lock(mutex_);
errors_.push_back(std::move(ex));
errored_ = true;
}
terminatedCV_.notify_one();
}
} // tck
} // reactivesocket