1
/* Copyright (C) 2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "LocalConfig.hpp"
18
#include <NdbConfig.h>
19
#include <NdbAutoPtr.hpp>
22
LocalConfig::LocalConfig(){
23
error_line = 0; error_msg[0] = 0;
28
LocalConfig::init(const char *connectString,
31
DBUG_ENTER("LocalConfig::init");
34
* 1. Check connectString
35
* 2. Check given filename
36
* 3. Check environment variable NDB_CONNECTSTRING
37
* 4. Check Ndb.cfg in NDB_HOME
38
* 5. Check Ndb.cfg in cwd
39
* 6. Check defaultConnectString
44
//1. Check connectString
45
if(connectString != 0 && connectString[0] != 0){
46
if(readConnectString(connectString, "connect string")){
49
// only nodeid given, continue to find hosts
54
//2. Check given filename
55
if (fileName && strlen(fileName) > 0) {
57
if(readFile(fileName, fopenError)){
63
//3. Check environment variable
65
if(NdbEnv_GetEnv("NDB_CONNECTSTRING", buf, sizeof(buf)) &&
67
if(readConnectString(buf, "NDB_CONNECTSTRING")){
73
//4. Check Ndb.cfg in NDB_HOME
76
char *buf2= NdbConfig_NdbCfgName(1 /*true*/);
77
NdbAutoPtr<char> tmp_aptr(buf2);
78
if(readFile(buf2, fopenError))
84
//5. Check Ndb.cfg in cwd
87
char *buf2= NdbConfig_NdbCfgName(0 /*false*/);
88
NdbAutoPtr<char> tmp_aptr(buf2);
89
if(readFile(buf2, fopenError))
98
BaseString::snprintf(buf2, sizeof(buf2), "host=localhost:%s", NDB_PORT);
99
if(readConnectString(buf2, "default connect string"))
108
LocalConfig::~LocalConfig(){
111
void LocalConfig::setError(int lineNumber, const char * _msg) {
112
error_line = lineNumber;
113
strncpy(error_msg, _msg, sizeof(error_msg));
116
void LocalConfig::printError() const {
117
ndbout << "Configuration error" << endl;
119
ndbout << "Line: "<< error_line << ", ";
120
ndbout << error_msg << endl << endl;
123
void LocalConfig::printUsage() const {
124
ndbout << "This node needs information on how to connect"<<endl
125
<< "to the NDB Management Server."<<endl
126
<< "The information can be supplied in one of the following ways:"
129
ndbout << "1. Put a Ndb.cfg file in the directory where you start"<<endl
130
<< " the node. "<< endl
131
<< " Ex: Ndb.cfg" << endl
132
<< " | host=localhost:"<<NDB_PORT<<endl;
134
ndbout << "2. Use the environment variable NDB_CONNECTSTRING to "<<endl
135
<< " provide this information." <<endl
137
<< " >export NDB_CONNECTSTRING=\"host=localhost:"<<NDB_PORT<<"\""
141
const char *nodeIdTokens[] = {
147
const char *hostNameTokens[] = {
156
const char *fileNameTokens[] = {
163
LocalConfig::parseNodeId(const char * buf){
164
for(int i = 0; nodeIdTokens[i] != 0; i++)
165
if (sscanf(buf, nodeIdTokens[i], &_ownNodeId) == 1)
171
LocalConfig::parseHostName(const char * buf){
172
char tempString[1024];
173
char tempString2[1024];
176
for(int i = 0; hostNameTokens[i] != 0; i++) {
177
if (sscanf(buf, hostNameTokens[i], tempString, &port) == 2) {
178
MgmtSrvrId mgmtSrvrId;
179
mgmtSrvrId.type = MgmId_TCP;
180
mgmtSrvrId.name.assign(tempString);
181
mgmtSrvrId.port = port;
182
ids.push_back(mgmtSrvrId);
186
if (buf == tempString2)
188
// try to add default port to see if it works
189
snprintf(tempString2, sizeof(tempString2),"%s:%s", buf, NDB_PORT);
196
LocalConfig::parseFileName(const char * buf){
197
char tempString[1024];
198
for(int i = 0; fileNameTokens[i] != 0; i++) {
199
if (sscanf(buf, fileNameTokens[i], tempString) == 1) {
200
MgmtSrvrId mgmtSrvrId;
201
mgmtSrvrId.type = MgmId_File;
202
mgmtSrvrId.name.assign(tempString);
203
ids.push_back(mgmtSrvrId);
211
LocalConfig::parseString(const char * connectString, BaseString &err){
213
char * copy = strdup(connectString);
214
NdbAutoPtr<char> tmp_aptr(copy);
216
for (char *tok = strtok_r(copy,";,",&for_strtok); tok != 0;
217
tok = strtok_r(NULL, ";,", &for_strtok)) {
218
if (tok[0] == '#') continue;
220
if (!_ownNodeId) // only one nodeid definition allowed
221
if (parseNodeId(tok))
223
if (parseHostName(tok))
225
if (parseFileName(tok))
228
err.assfmt("Unexpected entry: \"%s\"", tok);
235
bool LocalConfig::readFile(const char * filename, bool &fopenError)
241
FILE * file = fopen(filename, "r");
243
BaseString::snprintf(line, sizeof(line),
244
"Unable to open local config file: %s", filename);
250
BaseString theString;
252
while(fgets(line, sizeof(line), file)){
253
BaseString tmp(line);
255
if(tmp.length() > 0 && tmp.c_str()[0] != '#'){
256
theString.append(tmp);
260
while (fgets(line, sizeof(line), file)) {
261
BaseString tmp(line);
263
if(tmp.length() > 0 && tmp.c_str()[0] != '#'){
264
theString.append(";");
265
theString.append(tmp);
270
bool return_value = parseString(theString.c_str(), err);
274
tmp.assfmt("Reading %s: %s", filename, err.c_str());
275
setError(0, tmp.c_str());
283
LocalConfig::readConnectString(const char * connectString,
286
bool return_value = parseString(connectString, err);
289
err2.assfmt("Reading %d \"%s\": %s", info, connectString, err.c_str());
290
setError(0,err2.c_str());
296
LocalConfig::makeConnectString(char *buf, int sz)
298
int p= BaseString::snprintf(buf,sz,"nodeid=%d", _ownNodeId);
300
for (unsigned i = 0; i < ids.size(); i++)
302
if (ids[i].type != MgmId_TCP)
304
int new_p= p+BaseString::snprintf(buf+p,sz-p,",%s:%d",
305
ids[i].name.c_str(), ids[i].port);
318
template class Vector<MgmtSrvrId>;