Line data Source code
1 : #include <iostream>
2 : #include <thread>
3 : #include <mutex>
4 :
5 : #include "../libs/loger/easylogging++.h"
6 : #include "Server.h"
7 3 : INITIALIZE_EASYLOGGINGPP
8 : #define ELPP_THREAD_SAFE
9 :
10 : #define QUIT_LINE "quit"
11 :
12 : /* Check std::cin for QUIT_LINE. */
13 3 : void hasToQuit(bool& result, std::mutex& result_mutex) {
14 3 : std::string line;
15 9 : while (line.compare(QUIT_LINE) != 0) {
16 3 : std::cin >> line;
17 : }
18 3 : result_mutex.lock();
19 3 : result = true;
20 3 : result_mutex.unlock();
21 3 : }
22 :
23 9 : void setUpDatabase(rocksdb::DB** db, std::string db_name) {
24 9 : rocksdb::Options options;
25 9 : options.create_if_missing = true;
26 :
27 9 : LOGG(INFO) << "Opening " << db_name;
28 :
29 18 : rocksdb::Status status = rocksdb::DB::Open(options, db_name, db);
30 9 : if (! status.ok()) {
31 0 : LOGG(ERROR) << "Could not open database";
32 : }else{
33 9 : LOGG(INFO) << "Conexion exitosa a la base de datos";
34 9 : }
35 9 : }
36 :
37 : /* Set up server and listen to incomming connections. */
38 3 : int main(int argc, char**argv) {
39 3 : RestClient::init();
40 6 : std::string port,shared;
41 3 : configure(argc,argv,port,shared);
42 6 : Server server(port,shared);
43 :
44 : rocksdb::DB* usersDB;
45 3 : setUpDatabase(&usersDB, "usersDB");
46 :
47 3 : server.setUsersDB(usersDB);
48 :
49 : rocksdb::DB* chatDB;
50 3 : setUpDatabase(&chatDB, "chatDB");
51 : rocksdb::DB* likesDB;
52 3 : setUpDatabase(&likesDB, "likesDB");
53 :
54 3 : server.setChatDB(chatDB);
55 3 : server.setLikesDB(likesDB);
56 3 : server.updateServer();
57 :
58 3 : bool quit = false;
59 3 : LOGG(INFO) << "Opening server";
60 3 : std::mutex quit_mutex;
61 6 : std::thread quit_control_thread(hasToQuit, std::ref(quit), std::ref(quit_mutex));
62 :
63 3 : quit_mutex.lock();
64 1865 : while (! quit) {
65 1859 : quit_mutex.unlock();
66 1859 : server.run();
67 1859 : quit_mutex.lock();
68 : }
69 3 : quit_mutex.unlock();
70 :
71 3 : quit_control_thread.join();
72 3 : RestClient::disable();
73 3 : LOGG(INFO) << "Closing server";
74 6 : return 0;
75 9 : }
|