Line data Source code
1 : #include "Server.h"
2 :
3 : #define DEFAULT_MILISECS_POLL 500
4 :
5 5 : std::string Server::linkToShared = DEFAULT_SHARED;
6 :
7 0 : Server::Server() : databases(new server_databases_t()) {
8 0 : std::string port;
9 0 : intToString(DEFAULT_PORT_NUMBER, port);
10 0 : mg_mgr_init(&manager_, NULL);
11 0 : connection_ = mg_bind(&manager_, port.c_str(), handleEvent);
12 0 : mg_set_protocol_http_websocket(connection_);
13 0 : mg_enable_multithreading(connection_);
14 0 : connection_->user_data = databases;
15 0 : }
16 :
17 3 : Server::Server(std::string port,std::string sharedlink) : databases(new server_databases_t()) {
18 3 : Server::linkToShared = sharedlink;
19 3 : mg_mgr_init(&manager_, NULL);
20 3 : connection_ = mg_bind(&manager_, port.c_str(), handleEvent);
21 3 : mg_set_protocol_http_websocket(connection_);
22 3 : mg_enable_multithreading(connection_);
23 3 : connection_->user_data = databases;
24 3 : }
25 :
26 3 : Server::~Server() {
27 3 : mg_mgr_free(&manager_);
28 3 : delete databases->usersDB ;
29 3 : delete databases->likesDB;
30 3 : delete databases->chatDB;
31 3 : }
32 :
33 3 : void Server::updateServer(){
34 3 : MessageHandler msg(databases);
35 3 : msg.setSharedLink(linkToShared);
36 3 : msg.updateUsersWithSS();
37 3 : }
38 :
39 1859 : void Server::run() {
40 1859 : if (! databases->usersDB) {
41 0 : LOGG(ERROR) << "DB not set";
42 : }
43 1859 : mg_mgr_poll(&manager_, DEFAULT_MILISECS_POLL);
44 1859 : }
45 :
46 158 : void Server::handleEvent(struct mg_connection* act_connection, int new_event, void* ev_data) {
47 158 : struct http_message *http_msg = (struct http_message *) ev_data;
48 :
49 158 : RequestHandler requestHandler(http_msg, act_connection, linkToShared);
50 :
51 158 : switch (new_event) {
52 : case MG_EV_HTTP_REQUEST:
53 38 : LOGG(DEBUG) << "Recieved http msg";
54 38 : if (is_equal(&http_msg->uri, USERS_URI)) {
55 6 : requestHandler.listenUsersRequest();
56 32 : } else if (is_equal(&http_msg->uri, INTEREST_URI)) {
57 1 : requestHandler.listenInterestRequest();
58 31 : } else if (is_equal(&http_msg->uri, CHAT_URI)) {
59 4 : requestHandler.listenChatRequest();
60 27 : } else if (is_equal(&http_msg->uri, USERS_PHOTO_URI)) {
61 2 : requestHandler.listenPhotoRequest();
62 25 : } else if (is_equal(&http_msg->uri, USER_URI)) {
63 18 : requestHandler.listenUserRequest();
64 7 : } else if (is_equal(&http_msg->uri, MATCHES_URI)) {
65 4 : requestHandler.listenMatchesRequest();
66 3 : } else if (is_equal(&http_msg->uri, CHAT_NEW_URI)) {
67 2 : requestHandler.listenChatNewRequest();
68 : }else{
69 1 : requestHandler.sendHttpLine(NOT_IMPLEMENTED);
70 : }
71 38 : break;
72 : default:
73 120 : break;
74 158 : }
75 158 : }
76 :
77 3 : void Server::setUsersDB(rocksdb::DB *database) {
78 3 : databases->usersDB = database;
79 3 : }
80 :
81 3 : void Server::setChatDB(rocksdb::DB *db) {
82 3 : databases->chatDB = db;
83 3 : }
84 :
85 3 : void Server::setLikesDB(rocksdb::DB *database) {
86 3 : databases->likesDB = database;
87 18 : }
88 :
89 :
90 :
91 :
|