Repository navigation
Expand file tree
/
Copy pathEventPoller.h
More file actions
140 lines (114 loc) · 4.29 KB
/
Copy pathEventPoller.h
File metadata and controls
140 lines (114 loc) · 4.29 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
#pragma once
#include <cstdint>
#include <functional>
#include <memory>
#include "Disruptor/FixedSequenceGroup.h"
#include "Disruptor/IDataProvider.h"
#include "Disruptor/ISequencer.h"
#include "Disruptor/ISequence.h"
#include "Disruptor/ProducerType.h"
namespace Disruptor
{
enum class PollState
{
Processing,
Gating,
Idle
};
template <class T>
class EventPoller
{
public:
EventPoller(const std::shared_ptr< IDataProvider< T > >& dataProvider,
const std::shared_ptr< ISequencer< T > >& sequencer,
const std::shared_ptr< ISequence >& sequence,
const std::shared_ptr< ISequence >& gatingSequence)
: m_dataProvider(dataProvider)
, m_sequencer(sequencer)
, m_sequence(sequence)
, m_gatingSequence(gatingSequence)
{}
PollState poll(const std::function< bool(T&, std::int64_t, bool) >& eventHandler)
{
auto currentSequence = m_sequence->value();
auto nextSequence = currentSequence + 1;
auto availableSequence = m_sequencer->getHighestPublishedSequence(nextSequence, m_gatingSequence->value());
if (nextSequence <= availableSequence)
{
bool processNextEvent;
auto processedSequence = currentSequence;
try
{
do
{
auto& event = (*m_dataProvider)[nextSequence];
processNextEvent = eventHandler(event, nextSequence, nextSequence == availableSequence);
processedSequence = nextSequence;
nextSequence++;
}
while (nextSequence <= availableSequence && processNextEvent);
}
catch (...)
{
m_sequence->setValue(processedSequence);
throw;
}
m_sequence->setValue(processedSequence);
return PollState::Processing;
}
if (m_sequencer->cursor() >= nextSequence)
{
return PollState::Gating;
}
return PollState::Idle;
}
static std::shared_ptr< EventPoller< T > > newInstance(const std::shared_ptr< IDataProvider< T > >& dataProvider,
const std::shared_ptr< ISequencer< T > >& sequencer,
const std::shared_ptr< ISequence >& sequence,
const std::shared_ptr< ISequence >& cursorSequence,
const std::vector< std::shared_ptr< ISequence > >& gatingSequences)
{
std::shared_ptr< ISequence > gatingSequence;
if (gatingSequences.empty())
{
gatingSequence = cursorSequence;
}
else if (gatingSequences.size() == 1)
{
gatingSequence = *gatingSequences.begin();
}
else
{
gatingSequence = std::make_shared< FixedSequenceGroup >(gatingSequences);
}
return std::make_shared< EventPoller < T > >(dataProvider, sequencer, sequence, gatingSequence);
}
std::shared_ptr< ISequence > sequence() const
{
return m_sequence;
};
private:
std::shared_ptr< IDataProvider< T > > m_dataProvider;
std::shared_ptr< ISequencer< T > > m_sequencer;
std::shared_ptr< ISequence > m_sequence;
std::shared_ptr< ISequence > m_gatingSequence;
};
} // namespace Disruptor
#include <ostream>
namespace std
{
inline ostream& operator<<(ostream& stream, const Disruptor::PollState& value)
{
switch(value)
{
case Disruptor::PollState::Processing:
return stream << "Processing";
case Disruptor::PollState::Gating:
return stream << "Gating";
case Disruptor::PollState::Idle:
return stream << "Idle";
default:
return stream << static_cast< int >(value);
}
}
} // namespace std