-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscriberChannel.h
More file actions
154 lines (129 loc) · 4.3 KB
/
Copy pathSubscriberChannel.h
File metadata and controls
154 lines (129 loc) · 4.3 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
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
#pragma once
#include"RxTransport/CommonDefines.h"
#include"RxTransport/MessageController.h"
#include "ChannelFeedbackEvent.h"
namespace BaseLib { namespace Concurrent {
/* ----------------------------------------------------------------------------------
Functionality needed:
- Late arriving subscribers - replay n most recent messages to these
- Let this functionality be implemented in RxObserver, use this->DataCache().SelectLastN();
- Feedback as events to individual subscribers for policy violations, message arrival, etc
- Request(int n) - for pull based communication
- Feedback on discovery of writer/publisher
----------------------------------------------------------------------------------*/
template <typename T>
class SubscriberChannel
: public Status::ActivationStatus
, public RxObserver<T>
, public ChannelFeedbackForSubscriber
, public MessageFeedbackForSubscriber
, public Templates::Key1<ReaderId>
, protected Templates::ContextObjectShared<MessagePolicy, MessageChannel<T>>
{
public:
SubscriberChannel(std::shared_ptr<MessageChannel<T>> channel, ReaderId readerId, MessagePolicy messagePolicy)
: Templates::Key1<ReaderId>(readerId)
, Templates::ContextObjectShared<MessagePolicy, MessageChannel<T>>
(
new MessagePolicy(messagePolicy), channel->shared_from_this()
)
{ }
virtual ~SubscriberChannel()
{
this->data()->RemoveReader(this->Id());
}
RxData::ObjectAccessProxy<T, InstanceHandle> DataCache()
{
std::shared_ptr<MessageReader<T>> reader = this->data()->FindReader(this->Id());
ASSERT(reader);
return reader->DataCache();
}
ChannelHandle Handle() const
{
return this->data()->Id();
}
void Request(int n)
{
this->data()->Request(this->Id(), n);
}
// void RequestFrom(int n, WriterId writerId)
// {
// this->data()->Request(this->Id(), n);
// }
// --------------------------------------------------------
// Interface RxObserver
// --------------------------------------------------------
virtual void OnError(GeneralException exception)
{
if(onError)
{
onError(exception);
}
}
virtual void OnComplete()
{
if(onComplete)
{
onComplete();
}
}
virtual void OnNext(T value)
{
if(onData)
{
onData(value);
}
}
// --------------------------------------------------------
// Set callback functions
// --------------------------------------------------------
SubscriberChannel<T>& OnErrorDo(std::function<void(GeneralException)> onErrorDo)
{
onError = onErrorDo;
return *this;
}
SubscriberChannel<T>& OnCompleteDo(std::function<void()> onCompleteDo)
{
onComplete = onCompleteDo;
return *this;
}
SubscriberChannel<T>& OnDataDo(std::function<void(T)> onDataDo)
{
onData = onDataDo;
return *this;
}
SubscriberChannel<T>& OnMessageFeedbackDo(MessageFeedbackFunctionForSubscriber func)
{
messageFeedbackFunction = func;
return *this;
}
SubscriberChannel<T>& OnChannelFeedbackDo(ChannelFeedbackFunctionForSubscriber func)
{
channelFeedbackFunction = func;
return *this;
}
// --------------------------------------------------------
// Interfaces for feedback
// --------------------------------------------------------
void OnMessageFeedback(MessageReaderFeedbackKind kind, SequenceNumber number, MessageWriterProxyStatus status)
{
if(messageFeedbackFunction)
{
messageFeedbackFunction(kind, number, status);
}
}
virtual void OnChannelFeedback(MessageReaderFeedbackKind kind, std::shared_ptr<Status::StateStatusTimestampedType<MessageKind, int>> value)
{
if(channelFeedbackFunction)
{
channelFeedbackFunction(kind, value);
}
}
private:
MessageFeedbackFunctionForSubscriber messageFeedbackFunction;
ChannelFeedbackFunctionForSubscriber channelFeedbackFunction;
std::function<void(T)> onData;
std::function<void()> onComplete;
std::function<void(GeneralException)> onError;
};
}}