-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.cpp
55 lines (49 loc) · 1.46 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
#include "common/Config.hpp"
#include "core/Database.hpp"
#include "core/DatabaseConfig.hpp"
#include "query/result/QueryResultCollection.hpp"
#include "util/Utility.hpp"
#include "zmq/zmq.hpp"
#include <assert.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
using namespace std;
int main(int, char**)
{
if(!dbi::util::createFile("bin/swap_file", 100 * dbi::kPageSize)) {
cout << "failed" << endl;
throw;
}
auto db = dbi::util::make_unique<dbi::Database>(dbi::DatabaseConfig{"bin/swap_file", 128}, true);
// Socket to talk to clients
zmq::context_t context(2);
zmq::socket_t responder(context, ZMQ_REP);
responder.bind("tcp://*:7854");
cout << "started" << endl;
while(true) {
zmq::message_t msg;
responder.recv(&msg);
string query((char*)msg.data(), msg.size());
cout << query << endl;
if(query == "!!!CLEAR") {
db = dbi::util::make_unique<dbi::Database>(dbi::DatabaseConfig{"bin/swap_file", 128}, true);
string str = "ok";
msg.rebuild(str.size());
memcpy(msg.data(), str.data(), str.size());
responder.send(msg);
continue;
}
if(query == "!!!KILL")
break;
auto result = db->executeQuery(query);
ostringstream os;
result->toJSON(os);
string str = os.str();
msg.rebuild(str.size());
memcpy(msg.data(), str.data(), str.size());
responder.send(msg);
}
return 0;
}