18 #include "LocalConfig.hpp"
20 #include <NdbConfig.h>
21 #include <NdbAutoPtr.hpp>
22 #include <util/NdbOut.hpp>
24 #define _STR_VALUE(x) #x
25 #define STR_VALUE(x) _STR_VALUE(x)
27 LocalConfig::LocalConfig(){
28 error_line = 0; error_msg[0] = 0;
50 if(connectString != 0 && connectString[0] != 0){
51 if(readConnectString(connectString,
"connect string")){
60 if (fileName && strlen(fileName) > 0) {
62 if(readFile(fileName, fopenError)){
70 if(NdbEnv_GetEnv(
"NDB_CONNECTSTRING", buf,
sizeof(buf)) &&
72 if(readConnectString(buf,
"NDB_CONNECTSTRING")){
81 char *buf2= NdbConfig_NdbCfgName(1 );
83 if(readFile(buf2, fopenError))
92 char *buf2= NdbConfig_NdbCfgName(0 );
94 if(readFile(buf2, fopenError))
102 if(readConnectString(
"host=localhost:" STR_VALUE(NDB_PORT),
103 "default connect string"))
112 LocalConfig::~LocalConfig(){
115 void LocalConfig::setError(
int lineNumber,
const char * _msg) {
116 error_line = lineNumber;
117 strncpy(error_msg, _msg,
sizeof(error_msg));
120 void LocalConfig::printError()
const {
121 ndbout <<
"Configuration error" << endl;
123 ndbout <<
"Line: "<< error_line <<
", ";
124 ndbout << error_msg << endl << endl;
127 void LocalConfig::printUsage()
const {
128 ndbout <<
"This node needs information on how to connect"<<endl
129 <<
"to the NDB Management Server."<<endl
130 <<
"The information can be supplied in one of the following ways:"
133 ndbout <<
"1. Put a Ndb.cfg file in the directory where you start"<<endl
134 <<
" the node. "<< endl
135 <<
" Ex: Ndb.cfg" << endl
136 <<
" | host=localhost:"<<NDB_PORT<<endl;
138 ndbout <<
"2. Use the environment variable NDB_CONNECTSTRING to "<<endl
139 <<
" provide this information." <<endl
141 <<
" >export NDB_CONNECTSTRING=\"host=localhost:"<<NDB_PORT<<
"\""
145 const char *nodeIdTokens[] = {
151 const char *hostNameTokens[] = {
160 const char *bindAddressTokens[] = {
161 "bind-address=%[^:]:%i",
165 const char *fileNameTokens[] = {
172 LocalConfig::parseNodeId(
const char *
buf){
173 for(
int i = 0; nodeIdTokens[
i] != 0;
i++)
174 if (sscanf(buf, nodeIdTokens[
i], &_ownNodeId) == 1)
180 LocalConfig::parseHostName(
const char * buf){
181 char tempString[1024];
182 char tempString2[1024];
185 for(
int i = 0; hostNameTokens[
i] != 0;
i++) {
186 if (sscanf(buf, hostNameTokens[
i], tempString, &port) == 2) {
188 mgmtSrvrId.type = MgmId_TCP;
189 mgmtSrvrId.name.
assign(tempString);
190 mgmtSrvrId.port = port;
192 if (bind_address.
length())
193 mgmtSrvrId.bind_address.
assign(bind_address);
194 mgmtSrvrId.bind_address_port = bind_address_port;
195 ids.push_back(mgmtSrvrId);
199 if (buf == tempString2)
203 "%s:%d", buf, NDB_PORT);
210 LocalConfig::parseBindAddress(
const char * buf)
212 char tempString[1024];
213 char tempString2[1024];
217 for(
int i = 0; bindAddressTokens[
i] != 0; i++)
219 if (sscanf(buf, bindAddressTokens[i], tempString, &port) == 2)
224 bind_address.
assign(tempString);
225 bind_address_port = port;
230 mgmtSrvrId.bind_address.
assign(tempString);
231 mgmtSrvrId.bind_address_port = port;
235 if (buf == tempString2)
245 LocalConfig::parseFileName(
const char * buf){
246 char tempString[1024];
247 for(
int i = 0; fileNameTokens[
i] != 0; i++) {
248 if (sscanf(buf, fileNameTokens[i], tempString) == 1) {
250 mgmtSrvrId.type = MgmId_File;
251 mgmtSrvrId.name.
assign(tempString);
252 ids.push_back(mgmtSrvrId);
260 LocalConfig::parseString(
const char * connectString,
BaseString &err){
262 char * copy = strdup(connectString);
265 for (
char *tok = strtok_r(copy,
";,",&for_strtok); tok != 0;
266 tok = strtok_r(NULL,
";,", &for_strtok)) {
267 if (tok[0] ==
'#')
continue;
270 if (parseNodeId(tok))
272 if (parseHostName(tok))
274 if (parseBindAddress(tok))
276 if (parseFileName(tok))
279 err.
assfmt(
"Unexpected entry: \"%s\"", tok);
282 bind_address_port= 0;
287 bool LocalConfig::readFile(
const char * filename,
bool &fopenError)
293 FILE *
file = fopen(filename,
"r");
296 "Unable to open local config file: %s", filename);
304 while(fgets(line,
sizeof(line), file)){
307 if(tmp.length() > 0 && tmp.c_str()[0] !=
'#'){
312 while (fgets(line,
sizeof(line), file)) {
315 if(tmp.length() > 0 && tmp.c_str()[0] !=
'#'){
322 bool return_value = parseString(theString.
c_str(), err);
326 tmp.
assfmt(
"Reading %s: %s", filename, err.
c_str());
327 setError(0, tmp.
c_str());
335 LocalConfig::readConnectString(
const char * connectString,
338 bool return_value = parseString(connectString, err);
341 err2.
assfmt(
"Reading %s \"%s\": %s", info, connectString, err.
c_str());
342 setError(0,err2.
c_str());
348 LocalConfig::makeConnectString(
char *buf,
int sz)
351 if (p < sz && bind_address.
length())
354 bind_address.
c_str(), bind_address_port);
361 for (
unsigned i = 0; i < ids.size(); i++)
363 if (ids[i].
type != MgmId_TCP)
366 ids[i].
name.c_str(), ids[
i].port);
374 if (!bind_address.
length() && ids[
i].bind_address.length())
377 ids[i].bind_address.
c_str(), ids[
i].bind_address_port);