-
Notifications
You must be signed in to change notification settings - Fork 3
/
settings.cc
168 lines (151 loc) · 6.38 KB
/
settings.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2007 Google Inc. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// Author: [email protected] (Stephen Chen)
//
// The internal config file format uses data-centers to generat target addresses
// for RPCs.
//
// The socket config file format uses language-host and language-port pairs as
// target addresses for RPCs. This means that they must be read into two
// different hash maps. The alternative format is a language-host-port tuple,
// but this would require considerable rework to the SExpression Read*
// functions. Consequently, we chose the format of two pairs was for the socket
// config file.
#include "settings.h"
#include "socket_tags_service.h"
#include "datasource.h"
#include "sexpression.h"
#include "sexpression_util-inl.h"
#include "tagsoptionparser.h"
DEFINE_STRING(default_corpus,
"corpus1",
"default query corpus.");
DEFINE_STRING(default_language, "c++", "default query language.");
DEFINE_BOOL(default_callers, false, "default query callgraph.");
Settings* Settings::instance_;
void Settings::Load(const string& config_file) {
Settings::instance_ = new Settings(config_file);
}
void Settings::Free() {
delete Settings::instance_;
Settings::instance_ = NULL;
}
Settings::Settings(const string& config_file) {
default_corpus_ = GET_FLAG(default_corpus);
default_language_ = GET_FLAG(default_language);
default_callers_ = GET_FLAG(default_callers);
FileReader<SExpression> file_reader(config_file);
hash_map<string, bool> has_callgraph;
hash_map<string, string> language_hostnames;
hash_map<string, string> callgraph_hostnames;
hash_map<string, int> language_ports;
hash_map<string, int> callgraph_ports;
while (!file_reader.IsDone()) {
const SExpression* s = file_reader.GetNext();
if (s) {
// All known settings are lists for now.
if (!s->IsList()) {
LOG(WARNING) << "Skipping: " << s->Repr();
delete s;
continue;
}
// All known settings start with a symbol followed by a list of values.
SExpression::const_iterator iter = s->Begin();
if (iter != s->End() && iter->IsSymbol()) {
const string& setting =
down_cast<const SExpressionSymbol*>(&(*iter))->name();
++iter;
if (setting == "gtags-corpuses") {
ReadList(iter, s->End(), &corpuses_);
} else if (setting == "gtags-languages") {
ReadList(iter, s->End(), &languages_);
} else if (setting == "gtags-language-has-callgraph") {
ReadPairList(iter, s->End(), &has_callgraph);
} else if (setting == "gtags-language-hostnames") {
ReadPairList(iter, s->End(), &language_hostnames);
} else if (setting == "gtags-callgraph-hostnames") {
ReadPairList(iter, s->End(), &callgraph_hostnames);
} else if (setting == "gtags-language-ports") {
ReadPairList(iter, s->End(), &language_ports);
} else if (setting == "gtags-callgraph-ports") {
ReadPairList(iter, s->End(), &callgraph_ports);
}
} else {
LOG(WARNING) << "Skipping: " << iter->Repr();
}
delete s;
}
}
// Add sources based on settings.
for (list<string>::const_iterator corpus = corpuses_.begin();
corpus != corpuses_.end(); ++corpus) {
for (list<string>::const_iterator lang = languages_.begin();
lang != languages_.end(); ++lang) {
// create a data source for this language.
pair<DataSource*, DataSource*> source_pair;
RemoteDataSource* definition = new RemoteDataSource();
LOG(INFO) << "Data source for (" << *lang << ", definition) created"
;
// We don't have callgraph support for all the languages.
// Create data source for only the ones with callgraph.
RemoteDataSource* callgraph;
hash_map<string, bool>::iterator cg_iter = has_callgraph.find(*lang);
if (cg_iter != has_callgraph.end() && cg_iter->second) {
callgraph = new RemoteDataSource();
LOG(INFO) << "Data source for (" << *corpus << ", " << *lang
<< ", callers) created.";
} else {
callgraph = NULL;
LOG(INFO) << "Data source for (" << *corpus << ", " << *lang
<< ", callers) skipped.";
}
hash_map<string, string>::iterator lang_hostname_iter =
language_hostnames.find(*lang);
hash_map<string, int>::iterator lang_port_iter =
language_ports.find(*lang);
if (lang_hostname_iter != language_hostnames.end()
&& lang_port_iter != language_ports.end()) {
const string LANGUAGE_ADDRESS = lang_hostname_iter->second;
const int LANGUAGE_PORT = lang_port_iter->second;
definition->AddSource(
new gtags::SocketTagsServiceUser(
LANGUAGE_ADDRESS, LANGUAGE_PORT));
LOG(INFO) << "Added source: " << LANGUAGE_ADDRESS
<< ':' << LANGUAGE_PORT;
}
if (callgraph) {
hash_map<string, string>::iterator cg_hostname_iter =
callgraph_hostnames.find(*lang);
hash_map<string, int>::iterator cg_port_iter =
callgraph_ports.find(*lang);
if (cg_hostname_iter != callgraph_hostnames.end()
&& cg_port_iter != callgraph_ports.end()) {
const string CALLGRAPH_ADDRESS = cg_hostname_iter->second;
const int CALLGRAPH_PORT = cg_port_iter->second;
callgraph->AddSource(
new gtags::SocketTagsServiceUser(
CALLGRAPH_ADDRESS, CALLGRAPH_PORT));
LOG(INFO) << "Added source: " << CALLGRAPH_ADDRESS
<< ':' << CALLGRAPH_PORT;
}
}
source_pair.first = definition;
source_pair.second = callgraph;
sources_[*corpus][*lang] = source_pair;
}
}
}