-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
374 lines (342 loc) · 10.8 KB
/
server.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "server.h"
#include <boost/algorithm/string/trim.hpp>
#include <cstdlib>
#include <iostream>
#include <netinet/in.h>
#include <signal.h>
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include "load.h"
#include "program_options.h"
#include "stats.h"
#include "thread.h"
#include "utility.h"
class HandleRequestArgs {
public:
Server *server;
int sock;
};
Server::Server(All_data_t& the_data) :
data(the_data), search(the_data), sock(-1) {
create_server();
}
Server::~Server() {
if (this->sock != -1) {
close(this->sock);
}
}
void
Server::create_server() {
// First, shut down any existing server process
pid_t my_pid = getpid();
for (std::vector<pid_t>::const_iterator it = child_pids.begin();
it != child_pids.end();
++it) {
if (*it != my_pid) {
kill(*it, SIGTERM);
}
}
// Create socket
if ((this->sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
throw "Unable to create socket";
}
// Set it reusable
int opt = 1;
setsockopt(this->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
// Bind the socket
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(program_options->port());
/* bind the socket to the port specified above */
bool bound = false;
for (unsigned int retries = 0; retries < 60; ++retries) {
if (bind(this->sock, (struct sockaddr *)&address, sizeof(address)) != -1) {
// Update stats with any data from old child
global_stats->load_stats();
// We loaded our own data, so increase the data-loaded count.
global_stats->incrDataReloadFull();
bound = true;
break;
} else {
sleep(1);
}
}
if (!bound) {
throw "Unable to bind socket";
}
}
void
Server::accept_connections() {
if (listen(this->sock, 8) == -1) {
throw "Unable to listen to socket";
}
socklen_t addrlen = sizeof(struct sockaddr_in);
struct sockaddr_in address;
while (true) {
int new_socket;
new_socket = accept(this->sock, (struct sockaddr *)&address, &addrlen);
if (new_socket < 0) {
throw "Unable to accept connection";
}
HandleRequestArgs *args = new HandleRequestArgs;
args->server = this;
args->sock = new_socket;
Thread::create(server_handle_request, static_cast<void *>(args), false);
}
}
void
Server::handle_request(int incoming_socket) const {
struct timeval tv_start_network;
gettimeofday(&tv_start_network, NULL);
// Open as file, so we can do fgets, etc.
FILE *conn = fdopen(incoming_socket, "r+");
if (conn == NULL) {
throw "Unable to handle incoming connection";
}
char buf[10240];
// Get the arguments
Id_t searcher_userid = 0;
Id_t searcher_school = 0;
Id_t searcher_location = 0;
Params_t params; // General search parameters
std::vector<Id_t> interests;
bool perform_search = false;
while (fgets(buf, sizeof(buf), conn)) {
std::stringstream sbuf(buf);
std::string key;
std::string value;
sbuf >> key;
key = Utility::downcase(key);
if ((key == "end") || (key == "quit") || (key == "exit")) {
break;
} else if (key == "help") {
help(conn);
} else if (key == "stats") {
stats(conn);
fclose(conn);
return;
} else if (key == "terminate") {
exit(0);
} else if (key == "reload") {
fclose(conn);
Load::reload_online_and_new(data);
return;
} else if (key == "searcher_userid") {
sbuf >> searcher_userid;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "searcher_school") {
sbuf >> searcher_school;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "searcher_location") {
sbuf >> searcher_location;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "min_age") {
sbuf >> value;
params["min_age"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "max_age") {
sbuf >> value;
params["max_age"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "sex") {
sbuf >> value;
params["sex"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "name") {
std::getline(sbuf, value);
boost::algorithm::trim(value);
params["name"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "interest") {
sbuf >> value;
char *end_ptr;
interests.push_back(::strtol(value.c_str(), &end_ptr, 10));
if (interests.size() == 1) {
global_stats->incrSearchReq(key);
}
perform_search = true;
} else if (key == "location") {
sbuf >> value;
params["location"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "school") {
sbuf >> value;
params["school"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "sexuality") {
sbuf >> value;
params["sexuality"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "with_picture") {
sbuf >> value;
params["with_picture"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "single") {
sbuf >> value;
params["single"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "birthday") {
sbuf >> value;
params["birthday"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "online") {
sbuf >> value;
params["online"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "new_users") {
sbuf >> value;
params["new_users"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "active_recently") {
sbuf >> value;
params["active_recently"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "might_know") {
sbuf >> value;
params["might_know"] = value;
global_stats->incrSearchReq(key);
perform_search = true;
} else if (key == "no_friends") {
sbuf >> value;
params["no_friends"] = value;
} else {
fprintf(conn, "Unknown command.\n\n");
help(conn);
}
}
std::vector<Id_t> results;
struct timeval tv_start_search, tv_end_search;
if (perform_search) {
// Increase overall searches, because an individual search can have
// multiple parameters.
global_stats->incrSearchReq("search_reqs");
global_stats->incrInFlight();
// Do the search
gettimeofday(&tv_start_search, NULL);
results = this->search.do_search(
searcher_userid, searcher_school, searcher_location,
params, interests);
if (results.size() > 1000) {
results.resize(1000);
}
gettimeofday(&tv_end_search, NULL);
}
// Output the results
if (program_options->verbose() >= 2) {
std::cout << "Found " << results.size() << " matches" << std::endl;
}
for (std::vector<Id_t>::const_iterator it = results.begin();
it != results.end();
++it) {
fprintf(conn, "%d\n", *it);
}
fclose(conn);
// Update the time spent
struct timeval tv_end_network;
gettimeofday(&tv_end_network, NULL);
// In milliseconds
unsigned long time_spent;
time_spent = (tv_end_network.tv_sec - tv_start_network.tv_sec) * 1000;
time_spent += (tv_end_network.tv_usec - tv_start_network.tv_usec) / 1000;
global_stats->incrSearchTimeNetwork(time_spent);
if (perform_search) {
time_spent = (tv_end_search.tv_sec - tv_start_search.tv_sec) * 1000;
time_spent += (tv_end_search.tv_usec - tv_start_search.tv_usec) / 1000;
global_stats->incrSearchTime(time_spent);
global_stats->decrInFlight();
}
}
void
Server::threaded_accept() {
this->thread = Thread::create(server_accept_connections,
static_cast<void *>(this));
}
void
Server::wait_on_threads() {
pthread_join(this->thread, NULL);
}
void
Server::help(FILE *conn) const {
if (conn == NULL) {
throw "Unable to handle incoming connection";
}
fprintf(conn, "help display this info\n");
fprintf(conn, "stats display statistics\n");
fprintf(conn, "\n");
fprintf(conn, "searcher_userid <uid> searcher's userid\n");
fprintf(conn, "searcher_school <id> searcher's school id\n");
fprintf(conn, "searcher_location <id> searcher's location\n");
fprintf(conn, "min_age <age> minimum age in result set\n");
fprintf(conn, "max_age <age> maximum age in result set\n");
fprintf(conn, "sex {m,f} search only for this gender\n");
fprintf(conn, "name <str> username/realname substring search\n");
fprintf(conn, "interest <id> user interest\n");
fprintf(conn, " line can be included multiple times\n");
fprintf(conn, "location <id> only in this location (or children)\n");
fprintf(conn, "school <id> only in this school\n");
fprintf(conn, "sexuality <x> 1 - heterosexual only\n");
fprintf(conn, " 2 - homosexual only\n");
fprintf(conn, " 3 - bisexual only\n");
fprintf(conn, "with_picture true only include users with a profile pic\n");
fprintf(conn, "single true only include single users\n");
fprintf(conn, "birthday true birthday list\n");
fprintf(conn, "online true only users currently online\n");
fprintf(conn, "new_users true only new users\n");
fprintf(conn, "active_recently true only users active in past 30 days\n");
fprintf(conn, "might_know true prioritise users the searcher may know\n");
fprintf(conn, "end perform search\n");
fprintf(conn, "\nInternal commands:\n");
fprintf(conn, "terminate shut down the server\n");
fprintf(conn, "reload reload online and new users\n");
}
void
Server::stats(FILE *conn) const {
if (conn == NULL) {
throw "Unable to handle incoming connection";
}
fprintf(conn, "in_flight %u\n", global_stats->getInFlight());
fprintf(conn, "running_time %d\n", global_stats->getRunningTime());
fprintf(conn, "memory_use %lu\n",
static_cast<long unsigned>(global_stats->getMemoryUse()));
fprintf(conn, "search_time %lu\n", global_stats->getSearchTime());
fprintf(conn, "search_time_network %lu\n",
global_stats->getSearchTimeNetwork());
fprintf(conn, "data_reloads_full %u\n", global_stats->getDataReloadFull());
fprintf(conn, "data_reloads_fast %u\n", global_stats->getDataReloadFast());
std::map<std::string, unsigned int> search_reqs;
search_reqs = global_stats->getSearchReqs();
for (std::map<std::string, unsigned int>::const_iterator it = search_reqs.begin();
it != search_reqs.end();
++it) {
fprintf(conn, "%s %u\n", it->first.c_str(), it->second);
}
}
void* server_accept_connections(void *arg) {
Server *server = static_cast<Server *>(arg);
server->accept_connections();
return NULL;
}
void* server_handle_request(void *arg) {
HandleRequestArgs *hra = static_cast<HandleRequestArgs *>(arg);
hra->server->handle_request(hra->sock);
delete hra;
return NULL;
}