-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_consumer_forward_task.hpp
136 lines (111 loc) · 4.16 KB
/
tcp_consumer_forward_task.hpp
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
#ifndef VI_TCP_CONSUMER_FORWARD_TASK_HPP
#define VI_TCP_CONSUMER_FORWARD_TASK_HPP
#include "container_impl.h"
#include "tcp_client.hpp"
#include "message_constants.h"
#define FORWARD_BATCH_SIZE 128
namespace vi
{
class tcp_consumer_forward_task
{
private:
struct message_list_node
{
message_list_node* next;
uint64_t messageId;
message_list_node() : next(NULL), messageId(0)
{
}
message_list_node(uint64_t m) : next(NULL), messageId(m)
{
}
};
private:
container_hashset* mStorage;
container_queue* mQueue;
tcp_client* mTcpConsumer;
bool mTerminationMessage;
message_list_node* mListHead;
message_list_node* mListTail;
public:
tcp_consumer_forward_task(container_hashset* storage,
container_queue* queue,
tcp_client* tcpConsumer) : mStorage(storage),
mQueue(queue),
mTcpConsumer(tcpConsumer),
mListHead(NULL),
mListTail(NULL),
mTerminationMessage(false)
{
}
tcp_consumer_forward_task(const tcp_consumer_forward_task& other) : mStorage(other.mStorage),
mQueue(other.mQueue),
mTcpConsumer(other.mTcpConsumer),
mListHead(NULL),
mListTail(NULL),
mTerminationMessage(false)
{
}
tcp_consumer_forward_task& operator=(const tcp_consumer_forward_task& other)
{
if (this != &other)
{
mStorage = other.mStorage;
mQueue = other.mQueue;
mTcpConsumer = other.mTcpConsumer;
mListHead = NULL; //we don't copy it
mListTail = NULL; //we don't copy it
mTerminationMessage = false; //we don't copy it
}
return *this;
}
void begin()
{
}
bool step()
{
drain_queue();
tcp_client::client_result sendResult = tcp_client::ok;
while (mListHead != NULL && tcp_client::ok == sendResult)
{
const table_entry<Message>& entry = mStorage->find(mListHead->messageId);
if (entry.status != table_entry<Message>::empty)
{
mTerminationMessage = mTerminationMessage || TERMINATION_MESSAGE_TYPE == entry.payload.MessageType;
sendResult = mTcpConsumer->send(entry.payload);
}
if (tcp_client::ok == sendResult)
{
message_list_node* oldHead = mListHead;
mListHead = oldHead->next;
delete oldHead;
}
}
return !mTerminationMessage || (mQueue->has_tail() || mListHead != NULL);
}
void end()
{
}
private:
void drain_queue()
{
int drainCount = 0;
while (mQueue->has_tail() && drainCount < FORWARD_BATCH_SIZE)
{
uint64_t messageId = mQueue->pop_tail();
if (NULL == mListHead)
{
mListHead = new message_list_node(messageId);
mListTail = mListHead;
}
else
{
mListTail->next = new message_list_node(messageId);;
mListTail = mListTail->next;
}
drainCount++;
}
}
};
};
#endif