forked from libswift/libswift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsgw.cpp
283 lines (228 loc) · 9.06 KB
/
statsgw.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* statsgw.cpp
* HTTP server for showing some DL stats via SwarmPlayer 3000's webUI,
* libevent based
*
* Created by Victor Grishchenko, Arno Bakker
* Copyright 2010-2012 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved.
*
*/
#include "swift.h"
#include <event2/http.h>
using namespace swift;
int statsgw_reqs_count = 0;
uint64_t statsgw_last_down;
uint64_t statsgw_last_up;
tint statsgw_last_time = 0;
bool statsgw_quit_process=false;
struct evhttp *statsgw_event;
struct evhttp_bound_socket *statsgw_handle;
const char *top_page = "<!doctype html> \
<html style=\"font-family:Verdana,Arial,Helvetica,sans-serif;font-size:14px;background-color:white;\"> \
<head> \
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> \
<meta http-equiv=\"cache-control\" content=\"Private\" /> \
<meta http-equiv=\"Refresh\" content=\"2;url=http://127.0.0.1:6876/webUI\" /> \
<title>Swift Web Interface</title> \
</head> \
<body style=\"font-family:Verdana,Arial,Helvetica,sans-serif;font-size:14px;background-color:white;padding:20px; margin:20px;\"> \
<div style=\"padding:40;\"> \
<div style=\"width:400; float:left; padding:20px\"> \
<h1 style=\"font-size: 20px;\"> Swift swarms: </h1>";
const char *swarm_page_templ = " \
<h2 style=\"font-size: 18px; padding-top: 10px; padding-left: 20px; margin-bottom: 0px; color:#30bf00;\">Root hash: %s</h2> \
<ul style=\"padding-left: 40px;\"> \
<li>Progress: %d%c \
<li>Download speed: %d KB/s \
<li>Upload speed: %d KB/s \
</ul>";
const char *bottom_page = " \
<button style=\"color:white; background-color:#4f84dc; width:80;height:50; font-size:18px; font-weight:bold; text-shadow: #6374AB 2px 2px 2px;\" \
onClick=\"window.location='http://127.0.0.1:6876/webUI/exit';\">Quit Swift</button> \
</div> \
</div> \
</body> \
</html>";
const char *exit_page = "<!doctype html> \
<html style=\"font-family:Verdana,Arial,Helvetica,sans-serif;font-size:14px;background-color:white;\"> \
<head> \
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> \
<meta http-equiv=\"cache-control\" content=\"Private\" /> \
<title>Swift Web Interface</title> \
</head> \
<body style=\"font-family:Verdana,Arial,Helvetica,sans-serif;font-size:14px;background-color:white;padding:20px; margin:20px;\"> \
<div style=\"padding:40;\"> \
<div style=\"width:400; float:left; padding:20px\"> \
<h1 style=\"font-size: 20px;\"> Swift is no longer running. </h1> \
</div> \
</div> \
</body> \
</html>";
static void StatsGwNewRequestCallback (struct evhttp_request *evreq, void *arg);
void StatsExitCallback(struct evhttp_request *evreq)
{
char contlenstr[1024];
sprintf(contlenstr,PRISIZET,strlen(exit_page));
struct evkeyvalq *headers = evhttp_request_get_output_headers(evreq);
evhttp_add_header(headers, "Connection", "close" );
evhttp_add_header(headers, "Content-Type", "text/html" );
evhttp_add_header(headers, "Content-Length", contlenstr );
evhttp_add_header(headers, "Accept-Ranges", "none" );
// Construct evbuffer and send via chunked encoding
struct evbuffer *evb = evbuffer_new();
int ret = evbuffer_add(evb,exit_page,strlen(exit_page));
if (ret < 0) {
print_error("statsgw: ExitCallback: error evbuffer_add");
return;
}
evhttp_send_reply(evreq, 200, "OK", evb);
evbuffer_free(evb);
}
bool StatsQuit()
{
return statsgw_quit_process;
}
void StatsOverviewCallback(struct evhttp_request *evreq)
{
tint nu = NOW;
uint64_t down = Channel::global_raw_bytes_down;
uint64_t up = Channel::global_raw_bytes_up;
int dspeed = 0, uspeed = 0;
tint tdiff = (nu - statsgw_last_time)/1000000;
if (tdiff > 0) {
dspeed = (int)(((down-statsgw_last_down)/1024) / tdiff);
uspeed = (int)(((up-statsgw_last_up)/1024) / tdiff);
}
//statsgw_last_down = down;
//statsgw_last_up = up;
char bodystr[102400];
strcpy(bodystr,"");
strcat(bodystr,top_page);
tdlist_t tds = swift::GetTransferDescriptors();
tdlist_t::iterator iter;
for (iter = tds.begin(); iter != tds.end(); iter++)
{
int td = *iter;
uint64_t total = (int)swift::Size(td);
uint64_t down = (int)swift::Complete(td);
int perc = (int)((down * 100) / total);
char roothashhexstr[1024];
sprintf(roothashhexstr,"%s", GetSwarmID(td).hex().c_str() );
char templ[2048];
sprintf(templ,swarm_page_templ,roothashhexstr, perc, '%', dspeed, uspeed );
strcat(bodystr,templ);
}
strcat(bodystr,bottom_page);
char contlenstr[1024];
sprintf(contlenstr,PRISIZET,strlen(bodystr));
struct evkeyvalq *headers = evhttp_request_get_output_headers(evreq);
evhttp_add_header(headers, "Connection", "close" );
evhttp_add_header(headers, "Content-Type", "text/html" );
evhttp_add_header(headers, "Content-Length", contlenstr );
evhttp_add_header(headers, "Accept-Ranges", "none" );
// Construct evbuffer and send via chunked encoding
struct evbuffer *evb = evbuffer_new();
int ret = evbuffer_add(evb,bodystr,strlen(bodystr));
if (ret < 0) {
print_error("statsgw: OverviewCallback: error evbuffer_add");
return;
}
evhttp_send_reply(evreq, 200, "OK", evb);
evbuffer_free(evb);
}
void StatsGetSpeedCallback(struct evhttp_request *evreq)
{
if (statsgw_last_time == 0)
{
statsgw_last_time = NOW-1000000;
}
tint nu = Channel::Time();
uint64_t down = Channel::global_raw_bytes_down;
uint64_t up = Channel::global_raw_bytes_up;
int dspeed = 0, uspeed = 0;
tint tdiff = (nu - statsgw_last_time)/1000000;
if (tdiff > 0) {
dspeed = (int)(((down-statsgw_last_down)/1024) / tdiff);
uspeed = (int)(((up-statsgw_last_up)/1024) / tdiff);
}
statsgw_last_down = down;
statsgw_last_up = up;
statsgw_last_time = nu;
// Arno: PDD+ wants content speeds too
double contentdownspeed = 0.0, contentupspeed = 0.0;
uint32_t nleech=0,nseed=0;
tdlist_t tds = swift::GetTransferDescriptors();
tdlist_t::iterator iter;
for (iter = tds.begin(); iter != tds.end(); iter++)
{
int td = *iter;
contentdownspeed += swift::GetCurrentSpeed(td,DDIR_DOWNLOAD);
contentupspeed += swift::GetCurrentSpeed(td,DDIR_UPLOAD);
nleech += swift::GetNumLeechers(td);
nseed += swift::GetNumSeeders(td);
// TODO: Are these active leechers and seeders, or potential seeders and leechers? In the latter case these can be retrieved when cached peers are implemented
}
int cdownspeed = (int)(contentdownspeed/1024.0);
int cupspeed = (int)(contentupspeed/1024.0);
char speedstr[1024];
sprintf(speedstr,"{\"downspeed\": %d, \"success\": \"true\", \"upspeed\": %d, \"cdownspeed\": %d, \"cupspeed\": %d, \"nleech\": %d, \"nseed\": %d}", dspeed, uspeed, cdownspeed, cupspeed, nleech, nseed );
char contlenstr[1024];
sprintf(contlenstr,PRISIZET,strlen(speedstr));
struct evkeyvalq *headers = evhttp_request_get_output_headers(evreq);
evhttp_add_header(headers, "Connection", "close" );
evhttp_add_header(headers, "Content-Type", "application/json" );
evhttp_add_header(headers, "Content-Length", contlenstr );
evhttp_add_header(headers, "Accept-Ranges", "none" );
// Construct evbuffer and send via chunked encoding
struct evbuffer *evb = evbuffer_new();
int ret = evbuffer_add(evb,speedstr,strlen(speedstr));
if (ret < 0) {
print_error("statsgw: GetSpeedCallback: error evbuffer_add");
return;
}
evhttp_send_reply(evreq, 200, "OK", evb);
evbuffer_free(evb);
}
void StatsGwNewRequestCallback (struct evhttp_request *evreq, void *arg) {
dprintf("%s @%i http new request\n",tintstr(),statsgw_reqs_count);
statsgw_reqs_count++;
if (evhttp_request_get_command(evreq) != EVHTTP_REQ_GET) {
return;
}
// Parse URI
const char *uri = evhttp_request_get_uri(evreq);
//struct evkeyvalq *headers = evhttp_request_get_input_headers(evreq);
//const char *contentrangestr =evhttp_find_header(headers,"Content-Range");
fprintf(stderr,"statsgw: GOT %s\n", uri);
if (strstr(uri,"get_speed_info") != NULL)
{
StatsGetSpeedCallback(evreq);
}
else if (!strncmp(uri,"/webUI/exit",strlen("/webUI/exit")) || statsgw_quit_process)
{
statsgw_quit_process = true;
StatsExitCallback(evreq);
}
else if (!strncmp(uri,"/webUI",strlen("/webUI")))
{
StatsOverviewCallback(evreq);
}
}
bool InstallStatsGateway (struct event_base *evbase,Address bindaddr) {
// Arno, 2011-10-04: From libevent's http-server.c example
/* Create a new evhttp object to handle requests. */
statsgw_event = evhttp_new(evbase);
if (!statsgw_event) {
print_error("statsgw: evhttp_new failed");
return false;
}
/* Install callback for all requests */
evhttp_set_gencb(statsgw_event, StatsGwNewRequestCallback, NULL);
/* Now we tell the evhttp what port to listen on */
statsgw_handle = evhttp_bind_socket_with_handle(statsgw_event, bindaddr.ipstr().c_str(), bindaddr.port());
if (!statsgw_handle) {
print_error("statsgw: evhttp_bind_socket_with_handle failed");
return false;
}
return true;
}