diff options
Diffstat (limited to 'cpp/src/IceStorm/Admin.cpp')
-rw-r--r-- | cpp/src/IceStorm/Admin.cpp | 112 |
1 files changed, 62 insertions, 50 deletions
diff --git a/cpp/src/IceStorm/Admin.cpp b/cpp/src/IceStorm/Admin.cpp index 4a074d50f31..79f85986e0c 100644 --- a/cpp/src/IceStorm/Admin.cpp +++ b/cpp/src/IceStorm/Admin.cpp @@ -8,7 +8,7 @@ // ********************************************************************** #include <IceUtil/Options.h> -#include <Ice/Application.h> +#include <Ice/Ice.h> #include <Ice/ConsoleUtil.h> #include <Ice/SliceChecksums.h> #include <IceStorm/Parser.h> @@ -19,21 +19,20 @@ #endif using namespace std; -using namespace Ice; using namespace IceInternal; -using namespace IceStorm; -class Client : public Ice::Application -{ -public: +int run(const Ice::StringSeq&); - void usage(); - virtual int run(int, char*[]); -}; +Ice::CommunicatorPtr communicator; -#ifdef _WIN32 +void +destroyCommunicator(int) +{ + communicator->destroy(); +} int +#ifdef _WIN32 wmain(int argc, wchar_t* argv[]) { // @@ -41,24 +40,39 @@ wmain(int argc, wchar_t* argv[]) // _setmode(_fileno(stdin), _O_BINARY); #else - -int main(int argc, char* argv[]) { #endif - Client app; - Ice::InitializationData id; - Ice::StringSeq args = Ice::argsToStringSeq(argc, argv); - id.properties = Ice::createProperties(args); - id.properties->setProperty("Ice.Warn.Endpoints", "0"); - int rc = app.main(argc, argv, id); - return rc; + int status = 0; + + try + { + Ice::CtrlCHandler ctrlCHandler; + + Ice::InitializationData id; + Ice::StringSeq args = Ice::argsToStringSeq(argc, argv); + id.properties = Ice::createProperties(args); + id.properties->setProperty("Ice.Warn.Endpoints", "0"); + Ice::CommunicatorHolder ich(id); + communicator = ich.communicator(); + + ctrlCHandler.setCallback(&destroyCommunicator); + + status = run(args); + } + catch(const std::exception& ex) + { + consoleErr << ex.what() << endl; + status = 1; + } + + return status; } void -Client::usage() +usage(const string& name) { - consoleErr << "Usage: " << appName() << " [options]\n"; + consoleErr << "Usage: " << name << " [options]\n"; consoleErr << "Options:\n" "-h, --help Show this message.\n" @@ -69,7 +83,7 @@ Client::usage() } int -Client::run(int argc, char* argv[]) +run(const Ice::StringSeq& args) { string commands; bool debug; @@ -80,33 +94,31 @@ Client::run(int argc, char* argv[]) opts.addOpt("e", "", IceUtilInternal::Options::NeedArg, "", IceUtilInternal::Options::Repeat); opts.addOpt("d", "debug"); - vector<string> args; try { - args = opts.parse(argc, const_cast<const char**>(argv)); + if(!opts.parse(args).empty()) + { + consoleErr << args[0] << ": too many arguments" << endl; + usage(args[0]); + return 1; + } } catch(const IceUtilInternal::BadOptException& e) { consoleErr << e.reason << endl; - usage(); - return EXIT_FAILURE; - } - if(!args.empty()) - { - consoleErr << argv[0] << ": too many arguments" << endl; - usage(); - return EXIT_FAILURE; + usage(args[0]); + return 1; } if(opts.isSet("help")) { - usage(); - return EXIT_SUCCESS; + usage(args[0]); + return 0; } if(opts.isSet("version")) { consoleOut << ICE_STRING_VERSION << endl; - return EXIT_SUCCESS; + return 0; } if(opts.isSet("e")) { @@ -120,10 +132,10 @@ Client::run(int argc, char* argv[]) // The complete set of Ice::Identity -> manager proxies. map<Ice::Identity, IceStorm::TopicManagerPrx> managers; - PropertiesPtr properties = communicator()->getProperties(); + Ice::PropertiesPtr properties = communicator->getProperties(); IceStorm::TopicManagerPrx defaultManager; - Ice::PropertyDict props = communicator()->getProperties()->getPropertiesForPrefix("IceStormAdmin.TopicManager."); + Ice::PropertyDict props = communicator->getProperties()->getPropertiesForPrefix("IceStormAdmin.TopicManager."); { for(Ice::PropertyDict::const_iterator p = props.begin(); p != props.end(); ++p) { @@ -135,14 +147,14 @@ Client::run(int argc, char* argv[]) try { IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::uncheckedCast( - communicator()->propertyToProxy(p->first)); + communicator->propertyToProxy(p->first)); managers.insert(map<Ice::Identity, IceStorm::TopicManagerPrx>::value_type( manager->ice_getIdentity(), manager)); } catch(const Ice::ProxyParseException&) { - consoleErr << appName() << ": malformed proxy: " << p->second << endl; - return EXIT_FAILURE; + consoleErr << args[0] << ": malformed proxy: " << p->second << endl; + return 1; } } } @@ -151,7 +163,7 @@ Client::run(int argc, char* argv[]) if(!managerProxy.empty()) { defaultManager = IceStorm::TopicManagerPrx::uncheckedCast( - communicator()->stringToProxy(managerProxy)); + communicator->stringToProxy(managerProxy)); } else if(!managers.empty()) { @@ -169,7 +181,7 @@ Client::run(int argc, char* argv[]) os << "IceStorm/Finder"; os << ":tcp" << (host.empty() ? "" : (" -h \"" + host + "\"")) << " -p " << port << " -t " << timeout; os << ":ssl" << (host.empty() ? "" : (" -h \"" + host + "\"")) << " -p " << port << " -t " << timeout; - IceStorm::FinderPrx finder = IceStorm::FinderPrx::uncheckedCast(communicator()->stringToProxy(os.str())); + IceStorm::FinderPrx finder = IceStorm::FinderPrx::uncheckedCast(communicator->stringToProxy(os.str())); try { defaultManager = finder->getTopicManager(); @@ -182,19 +194,19 @@ Client::run(int argc, char* argv[]) if(!defaultManager) { - consoleErr << appName() << ": no manager proxies configured" << endl; - return EXIT_FAILURE; + consoleErr << args[0] << ": no manager proxies configured" << endl; + return 1; } - ParserPtr p = Parser::createParser(communicator(), defaultManager, managers); - int status = EXIT_SUCCESS; + IceStorm::ParserPtr p = IceStorm::Parser::createParser(communicator, defaultManager, managers); + int status = 0; if(!commands.empty()) // Commands were given { int parseStatus = p->parse(commands, debug); - if(parseStatus == EXIT_FAILURE) + if(parseStatus == 1) { - status = EXIT_FAILURE; + status = 1; } } else // No commands, let's use standard input @@ -202,9 +214,9 @@ Client::run(int argc, char* argv[]) p->showBanner(); int parseStatus = p->parse(stdin, debug); - if(parseStatus == EXIT_FAILURE) + if(parseStatus == 1) { - status = EXIT_FAILURE; + status = 1; } } |