-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cpp
68 lines (56 loc) · 1.77 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
#include <iostream>
#include "abstractstubserver.h"
#include <jsonrpccpp/server/connectors/httpserver.h>
#include <sqlite3.h>
using namespace jsonrpc;
using namespace std;
sqlite3 *db;
Json::Value object;
static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
Json::Value row;
for (int i = 0; i < argc; i++) {
row[azColName[i]] = argv[i] ? argv[i] : NULL;
}
object.append(row);
return 0;
}
class MyStubServer : public AbstractStubServer {
public:
MyStubServer(AbstractServerConnector &connector);
virtual Json::Value query(const std::string& sql);
virtual std::string ping();
};
MyStubServer::MyStubServer(AbstractServerConnector &connector) : AbstractStubServer(connector) {}
Json::Value MyStubServer::query(const std::string& sql) {
cout << "Query received: " << sql << endl;
object.clear();
char *zErrMsg = 0;
int rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
cout << "SQL error: " << zErrMsg << endl;
string error_message(zErrMsg);
sqlite3_free(zErrMsg);
throw JsonRpcException(1, error_message);
}
return object;
}
string MyStubServer::ping() {
cout << "Got ping!" << endl;
return "pong";
}
int main(int argc, char **argv) {
if (argc != 2) {
cout << "Please supply path to an SQLite3 database file" << endl;
return EXIT_FAILURE;
}
int rc = sqlite3_open(argv[1], &db);
if (rc) {
cout << "Can't open database: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return EXIT_FAILURE;
}
HttpServer httpserver(8383);
MyStubServer s(httpserver);
s.StartListening();
while(1) {} //Listen forever
}