-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cc
141 lines (118 loc) · 4.32 KB
/
config.cc
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
#include <iostream>
#include <cstring>
#include <map>
#include <io-buffer.h>
#include <json-stream-analyzer.h>
#include "config.h"
TarantoolConfig::TarantoolConfig()
{
this->host = "127.0.0.1";
this->port = 3301;
}
char* TarantoolConfig::getUri()
{
int n_size = std::strlen(this->host.c_str()) + 2 + 6;
char* uri = new char[n_size];
memset(uri, 0, sizeof(char) * n_size);
std::sprintf(uri, "%s:%d", this->host.c_str(), this->port);
return uri;
}
void TarantoolConfig::parseJsonObject(JsonObject* jsonObject)
{
if (jsonObject->find("host") != jsonObject->end()) {
JsonStreamAnalyzer::Element* hostTarantool = jsonObject->at("host");
if (hostTarantool->getType() == ELEMENT_TYPE_TEXT) {
std::string *host = (std::string*) hostTarantool->getData();
this->host = *host;
}
}
if (jsonObject->find("port") != jsonObject->end()) {
JsonStreamAnalyzer::Element* portTarantool = jsonObject->at("port");
if (portTarantool->getType() == ELEMENT_TYPE_NUMERIC) {
std::string *sPort = (std::string*) portTarantool->getData();
this->port = atoi(sPort->c_str());
}
}
}
Config::Config(int argc, char** argv) {
// initialize defaults
this->is_help = false;
this->tarantoolConfig = new TarantoolConfig;
this->listen = std::string("127.0.0.1");
this->port = 50051;
if (argc == 1) {
// run without params
return;
}
for (int i = 1; i < argc; i++) {
std::string paramName(argv[i]);
if (paramName.compare("-h") == 0 || paramName.compare("--help") == 0) {
this->is_help = true;
continue;
}
if (paramName.compare("-c") == 0 || paramName.compare("--config") == 0) {
if (i + 1 == argc) {
// todo: error missed argument value
continue;
}
std::string paramValue(argv[i+1]);
this->parse_config_file(paramValue);
i++;
continue;
}
}
}
bool Config::isHelp()
{
return this->is_help;
}
TarantoolConfig* Config::getTarantoolConfig()
{
return this->tarantoolConfig;
}
char* Config::getUri() {
int n_size = strlen(this->listen.c_str()) + 2 + 6;
char* uri = new char[n_size];
memset(uri, 0, sizeof(char) * n_size);
std::sprintf(uri, "%s:%d", this->listen.c_str(), this->port);
return uri;
}
void Config::parse_config_file(std::string file_name)
{
// todo: file analyze (json/yml/cfg/ini)
IOBuffer::IOFileReader fileReader(file_name.c_str());
IOBuffer::CharStream charStream(&fileReader);
JsonStreamAnalyzer::Stream stream(&charStream);
JsonStreamAnalyzer::Decoder decoder(&stream);
JsonStreamAnalyzer::Element* element = decoder.decode();
if (element->getType() == ELEMENT_TYPE_OBJECT) {
JsonObject* obj = (JsonObject*) element->getData();
if (obj->find("tarantool") != obj->end()) {
JsonStreamAnalyzer::Element* tarantoolConfig = obj->at("tarantool");
if (tarantoolConfig->getType() == ELEMENT_TYPE_OBJECT) {
this->tarantoolConfig->parseJsonObject((JsonObject*) tarantoolConfig->getData());
}
}
if (obj->find("server") != obj->end()) {
JsonStreamAnalyzer::Element* serverConfig = obj->at("server");
if (serverConfig->getType() == ELEMENT_TYPE_OBJECT) {
// parse server object
JsonObject* serverJsonObj = (JsonObject*) serverConfig->getData();
if (serverJsonObj->find("listen") != serverJsonObj->end()) {
JsonStreamAnalyzer::Element* eListenIp = serverJsonObj->at("listen");
if (eListenIp->getType() == ELEMENT_TYPE_TEXT) {
std::string* listen = (std::string*) eListenIp->getData();
this->listen = *listen;
}
}
if (serverJsonObj->find("port") != serverJsonObj->end()) {
JsonStreamAnalyzer::Element* ePort = serverJsonObj->at("port");
if (ePort->getType() == ELEMENT_TYPE_NUMERIC) {
std::string *sPort = (std::string*) ePort->getData();
this->port = atoi(sPort->c_str());
}
}
}
}
}
}