-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathnotification.cpp
181 lines (171 loc) · 5.43 KB
/
notification.cpp
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
/*
*
* Copyright (C) Max <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "notification.h"
#include <rte_config.h>
#include <rte_cycles.h>
#include <Poco/Net/IPAddress.h>
#include "sender.h"
NotifyManager *NotifyManager::_instance;
Poco::NotificationQueue NotifyManager::queue;
NotifyManager::NotifyManager(int size, std::map<int, struct NotificationParams> &prms) :
Task("NotifyManager"),
_logger(Poco::Logger::get("NotifyManager")),
_params(prms)
{
subs.reserve(size);
_timeout = 300;
_instance = this;
for(auto const &prm : prms)
{
_senders.insert(std::make_pair(prm.first, new CSender((struct CSender::params &)prm.second.prm)));
}
}
NotifyManager::~NotifyManager()
{
for(auto const &i : _senders)
{
delete i.second;
}
}
bool NotifyManager::needNotify(uint32_t ip, int group_id)
{
auto got = subs.find(ip);
if(got == subs.end())
{
queue.enqueueNotification(new UpdateNotification(ip, group_id));
return true;
} else {
if(got->second.need_redirect)
{
got->second.need_redirect = false;
got->second.redirects++;
queue.enqueueNotification(new UpdateNotification(ip, group_id));
return true;
}
}
return false;
}
void NotifyManager::runTask()
{
_logger.debug("Starting NotifyManager...");
uint64_t last_check = time(NULL);
while(!isCancelled())
{
Poco::Notification::Ptr pNf(queue.waitDequeueNotification(_timeout));
if (pNf)
{
UpdateNotification::Ptr pUpdateNf = pNf.cast<UpdateNotification>();
if (pUpdateNf)
{
int group_id = pUpdateNf->group_id();
auto prm = _params.find(group_id);
if(prm != _params.end())
{
auto sub = subs.find(pUpdateNf->getIP());
if(sub != subs.end())
{
uint64_t tm = time(NULL);
sub->second.last_redirect = tm;
sub->second.next_redirect = tm + prm->second.period;
} else {
uint64_t tm = time(NULL);
struct subscriber s;
s.ipv4 = pUpdateNf->getIP();
s.need_redirect = false;
s.last_redirect = tm;
s.next_redirect = tm + prm->second.period;
s.redirects++;
s.repeat = prm->second.repeat;
std::pair<uint32_t, struct subscriber> entry(pUpdateNf->getIP(), s);
subsLock.lock();
subs.insert(entry);
subsLock.unlock();
_logger.debug("Subscriber with ip %d successfully inserted to the subscriber database", (int) pUpdateNf->getIP());
}
} else {
_logger.error("Unable to find notify group %d", group_id);
}
}
NotifyRedirect::Ptr pNotifyNf = pNf.cast<NotifyRedirect>();
if (pNotifyNf)
{
int notify_group = pNotifyNf->notify_group();
auto const r = _senders.find(notify_group);
if(r == _senders.end())
{
_logger.error("Unable to find sender for group with id %d", notify_group);
} else {
r->second->HTTPRedirect(pNotifyNf->user_port(), pNotifyNf->dst_port(), pNotifyNf->user_ip(), pNotifyNf->dst_ip(), pNotifyNf->ip_version(), pNotifyNf->acknum(), pNotifyNf->seqnum(), pNotifyNf->f_psh(), pNotifyNf->additional_param().c_str(), pNotifyNf->additional_param().length());
// struct redirect_params rp = r->second;
// std::string full_url("@HTTP/1.1 "+rp.code+"\r\nLocation: " + rp.redirect_url + pNotifyNf->additional_param() + "\r\nConnection: close\r\n");
// sender->Redirect(pNotifyNf->user_port(), pNotifyNf->dst_port(), pNotifyNf->user_ip(), pNotifyNf->dst_ip(), pNotifyNf->ip_version(), pNotifyNf->acknum(), pNotifyNf->seqnum(), pNotifyNf->f_psh(), full_url);
}
}
}
uint64_t tm = time(NULL);
if(last_check + 1 < tm)
{
for(auto &sub : subs)
{
if(!sub.second.need_redirect && sub.second.next_redirect < tm)
{
if(sub.second.repeat)
{
if(sub.second.repeat < sub.second.redirects)
sub.second.need_redirect = true;
} else {
sub.second.need_redirect = true;
}
}
}
}
}
_logger.debug("Stopping NotifyManager...");
}
static inline void printSub(struct cmdline *cl, uint32_t ip, struct subscriber &sub)
{
char buff[20];
Poco::Net::IPAddress ipp((void *)&ip,sizeof(in_addr));
cmdline_printf(cl, "Subscriber with ip %s:\n", ipp.toString().c_str());
strftime(buff, 20, "%d-%m-%Y %H:%M:%S", localtime((time_t *)&sub.last_redirect));
cmdline_printf(cl, "Last redirect: %s\n", buff);
strftime(buff, 20, "%d-%m-%Y %H:%M:%S", localtime((time_t *)&sub.next_redirect));
cmdline_printf(cl, "Next redirect: %s\n", buff);
cmdline_printf(cl, "Redirected times: %lu\n", sub.redirects);
}
void NotifyManager::printSubscribers(struct cmdline* cl, uint32_t ip)
{
NotifyManager *nm = _instance;
if(ip)
{
auto got = nm->subs.find(ip);
if(got == nm->subs.end())
{
cmdline_printf(cl, "Subscriber not found\n");
} else {
printSub(cl, ip, got->second);
}
} else {
for(auto const &sub : nm->subs)
{
printSub(cl, sub.first, (struct subscriber &)sub.second);
}
cmdline_printf(cl, "Displayed %lu entries\n", nm->subs.size());
}
}