main.cpp 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (C) 2003 MySQL AB

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

17
#include <ndb_global.h>
18 19 20 21 22 23 24 25 26 27 28

#include <NdbMain.h>
#include <NdbHost.h>
#include <util/getarg.h>
#include <mgmapi.h>
#include <LocalConfig.hpp>

#include "CommandInterpreter.hpp"

#include <signal.h>

29
const char *progname = "ndb_mgm";
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49


static CommandInterpreter* com;

extern "C"
void 
handler(int sig){
  switch(sig){
  case SIGPIPE:
    /**
     * Will happen when connection to mgmsrv is broken
     * Reset connected flag
     */
    com->disconnect();    
    break;
  }
}

int main(int argc, const char** argv){
  int optind = 0;
50 51 52 53
  char _default_connectstring_buf[256];
  snprintf(_default_connectstring_buf, sizeof(_default_connectstring_buf),
	   "host=localhost:%u", NDB_BASE_PORT);
  const char *_default_connectstring= _default_connectstring_buf;
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
  const char *_host = 0;
  int _port = 0;
  int _help = 0;
  int _try_reconnect = 0;
  
  struct getargs args[] = {
    { "try-reconnect", 0, arg_integer, &_try_reconnect, "", "" },
    { "usage", '?', arg_flag, &_help, "Print help", "" },
  };
  int num_args = sizeof(args) / sizeof(args[0]); /* Number of arguments */
  
  
  if(getarg(args, num_args, argc, argv, &optind) || _help) {
    arg_printusage(args, num_args, progname, "[host [port]]");
    exit(1);
  }

  argv += optind;
  argc -= optind;

  LocalConfig cfg;

  if(argc >= 1) {
    _host = argv[0];
    if(argc >= 2) {
      _port = atoi(argv[1]);
    }
  } else {
    if(cfg.init(false, 0, 0, _default_connectstring) && cfg.items > 0 && cfg.ids[0]->type == MgmId_TCP){
      _host = cfg.ids[0]->data.tcp.remoteHost;
      _port = cfg.ids[0]->data.tcp.port;
    } else {
      cfg.printError();
      cfg.printUsage();
      return 1;
    }
  }
  
  char buf[MAXHOSTNAMELEN+10];
  snprintf(buf, sizeof(buf), "%s:%d", _host, _port);

  ndbout << "-- NDB Cluster -- Management Client --" << endl;
  printf("Connecting to Management Server: %s\n", buf);

  signal(SIGPIPE, handler);

  com = new CommandInterpreter(buf);
  while(com->readAndExecute(_try_reconnect));
  delete com;
  
  return 0;
}