diff options
Diffstat (limited to 'cpp/src/IceStorm/Parser.cpp')
-rw-r--r-- | cpp/src/IceStorm/Parser.cpp | 334 |
1 files changed, 151 insertions, 183 deletions
diff --git a/cpp/src/IceStorm/Parser.cpp b/cpp/src/IceStorm/Parser.cpp index 0629ca0e393..faa857157e0 100644 --- a/cpp/src/IceStorm/Parser.cpp +++ b/cpp/src/IceStorm/Parser.cpp @@ -9,8 +9,9 @@ #include <IceStorm/IceStormInternal.h> #include <algorithm> -#if defined(__APPLE__) || defined(__linux__) -# include <editline/readline.h> +#ifdef HAVE_READLINE +# include <readline/readline.h> +# include <readline/history.h> #endif extern FILE* yyin; @@ -29,7 +30,7 @@ namespace IceStorm Parser* parser; #ifdef _WIN32 -Ice::StringConverterPtr windowsConsoleConverter = 0; +shared_ptr<Ice::StringConverter> windowsConsoleConverter = nullptr; #endif } @@ -37,51 +38,37 @@ Ice::StringConverterPtr windowsConsoleConverter = 0; namespace { -class UnknownManagerException : public Exception +class UnknownManagerException : public std::exception { public: - UnknownManagerException(const string& nameP, const char* file, int line) : - Exception(file, line), - name(nameP) + explicit UnknownManagerException(const std::string& name) : _name(name) { } -#ifndef ICE_CPP11_COMPILER - virtual - ~UnknownManagerException() throw() + const char* what() const noexcept override { - } -#endif - - virtual string - ice_id() const - { - return "::UnknownManagerException"; - } - - virtual Exception* - ice_clone() const - { - return new UnknownManagerException(*this); - } - - virtual void - ice_throw() const - { - throw *this; + return _name.c_str(); } - const string name; +private: + const string _name; }; } -ParserPtr -Parser::createParser(const CommunicatorPtr& communicator, const TopicManagerPrx& admin, - const map<Ice::Identity, TopicManagerPrx>& managers) +Parser::Parser(shared_ptr<Communicator> communicator, shared_ptr<TopicManagerPrx> admin, + map<Ice::Identity, shared_ptr<TopicManagerPrx>> managers) : + _communicator(move(communicator)), + _defaultManager(move(admin)), + _managers(move(managers)) { - return new Parser(communicator, admin, managers); +#ifdef _WIN32 + if(!windowsConsoleConverter) + { + windowsConsoleConverter = Ice::createWindowsStringConverter(GetConsoleOutputCP()); + } +#endif } void @@ -114,17 +101,17 @@ Parser::create(const list<string>& args) return; } - for(list<string>::const_iterator i = args.begin(); i != args.end() ; ++i) + for(const auto& arg : args) { try { string topicName; - TopicManagerPrx manager = findManagerById(*i, topicName); + auto manager = findManagerById(arg, topicName); manager->create(topicName); } - catch(const Ice::Exception& ex) + catch(const std::exception&) { - exception(ex, args.size() > 1); // Print a warning if we're creating multiple topics, an error otherwise. + exception(current_exception(), args.size() > 1); // Print a warning if we're creating multiple topics, an error otherwise. } } } @@ -138,15 +125,15 @@ Parser::destroy(const list<string>& args) return; } - for(list<string>::const_iterator i = args.begin(); i != args.end() ; ++i) + for(const auto& arg : args) { try { - findTopic(*i)->destroy(); + findTopic(arg)->destroy(); } - catch(const Ice::Exception& ex) + catch(const std::exception&) { - exception(ex, args.size() > 1); // Print a warning if we're destroying multiple topics, an error otherwise. + exception(current_exception(), args.size() > 1); // Print a warning if we're destroying multiple topics, an error otherwise. } } } @@ -164,15 +151,15 @@ Parser::link(const list<string>& args) { list<string>::const_iterator p = args.begin(); - TopicPrx fromTopic = findTopic(*p++); - TopicPrx toTopic = findTopic(*p++); - Ice::Int cost = p != args.end() ? atoi(p->c_str()) : 0; + auto fromTopic = findTopic(*p++); + auto toTopic = findTopic(*p++); + auto cost = p != args.end() ? atoi(p->c_str()) : 0; fromTopic->link(toTopic, cost); } - catch(const Exception& ex) + catch(const std::exception&) { - exception(ex); + exception(current_exception()); } } @@ -189,14 +176,14 @@ Parser::unlink(const list<string>& args) { list<string>::const_iterator p = args.begin(); - TopicPrx fromTopic = findTopic(*p++); - TopicPrx toTopic = findTopic(*p++); + auto fromTopic = findTopic(*p++); + auto toTopic = findTopic(*p++); fromTopic->unlink(toTopic); } - catch(const Exception& ex) + catch(const std::exception&) { - exception(ex); + exception(current_exception()); } } @@ -211,7 +198,7 @@ Parser::links(const list<string>& args) try { - TopicManagerPrx manager; + shared_ptr<TopicManagerPrx> manager; if(args.size() == 0) { manager = _defaultManager; @@ -221,19 +208,17 @@ Parser::links(const list<string>& args) manager = findManagerByCategory(args.front()); } - TopicDict d = manager->retrieveAll(); - for(TopicDict::iterator i = d.begin(); i != d.end(); ++i) + for(const auto& topic : manager->retrieveAll()) { - LinkInfoSeq links = i->second->getLinkInfoSeq(); - for(LinkInfoSeq::const_iterator p = links.begin(); p != links.end(); ++p) + for(const auto& linkInfo : topic.second->getLinkInfoSeq()) { - consoleOut << i->first << " to " << p->name << " with cost " << p->cost << endl; + consoleOut << topic.first << " to " << linkInfo.name << " with cost " << linkInfo.cost << endl; } } } - catch(const Exception& ex) + catch(const std::exception&) { - exception(ex); + exception(current_exception()); } } @@ -248,7 +233,7 @@ Parser::topics(const list<string>& args) try { - TopicManagerPrx manager; + shared_ptr<TopicManagerPrx> manager; if(args.size() == 0) { manager = _defaultManager; @@ -258,15 +243,14 @@ Parser::topics(const list<string>& args) manager = findManagerByCategory(args.front()); } - TopicDict d = manager->retrieveAll(); - for(TopicDict::iterator i = d.begin(); i != d.end(); ++i) + for(const auto& topic : manager->retrieveAll()) { - consoleOut << i->first << endl; + consoleOut << topic.first << endl; } } - catch(const Exception& ex) + catch(const std::exception&) { - exception(ex); + exception(current_exception()); } } @@ -281,7 +265,7 @@ Parser::replica(const list<string>& args) try { - TopicManagerPrx m; + shared_ptr<TopicManagerPrx> m; if(args.size() == 0) { m = _defaultManager; @@ -290,63 +274,63 @@ Parser::replica(const list<string>& args) { m = findManagerByCategory(args.front()); } - TopicManagerInternalPrx manager = TopicManagerInternalPrx::uncheckedCast(m); - IceStormElection::NodePrx node = manager->getReplicaNode(); + auto manager = Ice::uncheckedCast<TopicManagerInternalPrx>(m); + auto node = manager->getReplicaNode(); if(!node) { error("This topic is not replicated"); } - IceStormElection::NodeInfoSeq nodes = node->nodes(); + auto nodes = node->nodes(); consoleOut << "replica count: " << nodes.size() << endl; - for(IceStormElection::NodeInfoSeq::const_iterator p = nodes.begin(); p != nodes.end(); ++p) + for(const auto& n : nodes) { try { - IceStormElection::QueryInfo info = p->n->query(); - consoleOut << p->id << ": id: " << info.id << endl; - consoleOut << p->id << ": coord: " << info.coord << endl; - consoleOut << p->id << ": group name: " << info.group << endl; - consoleOut << p->id << ": state: "; + auto info = n.n->query(); + consoleOut << n.id << ": id: " << info.id << endl; + consoleOut << n.id << ": coord: " << info.coord << endl; + consoleOut << n.id << ": group name: " << info.group << endl; + consoleOut << n.id << ": state: "; switch(info.state) { - case IceStormElection::NodeStateInactive: + case IceStormElection::NodeState::NodeStateInactive: consoleOut << "inactive"; break; - case IceStormElection::NodeStateElection: + case IceStormElection::NodeState::NodeStateElection: consoleOut << "election"; break; - case IceStormElection::NodeStateReorganization: + case IceStormElection::NodeState::NodeStateReorganization: consoleOut << "reorganization"; break; - case IceStormElection::NodeStateNormal: + case IceStormElection::NodeState::NodeStateNormal: consoleOut << "normal"; break; default: consoleOut << "unknown"; } consoleOut << endl; - consoleOut << p->id << ": group: "; - for(IceStormElection::GroupInfoSeq::const_iterator q = info.up.begin(); q != info.up.end(); ++q) + consoleOut << n.id << ": group: "; + for(auto q = info.up.cbegin(); q != info.up.cend(); ++q) { - if(q != info.up.begin()) + if(q != info.up.cbegin()) { consoleOut << ","; } consoleOut << q->id; } consoleOut << endl; - consoleOut << p->id << ": max: " << info.max + consoleOut << n.id << ": max: " << info.max << endl; } catch(const Exception& ex) { - consoleOut << p->id << ": " << ex.ice_id() << endl; + consoleOut << n.id << ": " << ex.ice_id() << endl; } } } - catch(const Exception& ex) + catch(const std::exception&) { - exception(ex); + exception(current_exception()); } } @@ -360,20 +344,19 @@ Parser::subscribers(const list<string>& args) } try { - for(list<string>::const_iterator i = args.begin(); i != args.end() ; ++i) + for(const auto& arg : args) { - TopicPrx topic = _defaultManager->retrieve(*i); - consoleOut << (*i) << ": subscribers:" << endl; - IdentitySeq subscribers = topic->getSubscribers(); - for(IdentitySeq::const_iterator j = subscribers.begin(); j != subscribers.end(); ++j) + auto topic = _defaultManager->retrieve(arg); + consoleOut << arg << ": subscribers:" << endl; + for(const auto& subscriber : topic->getSubscribers()) { - consoleOut << "\t" << _communicator->identityToString(*j) << endl; + consoleOut << "\t" << _communicator->identityToString(subscriber) << endl; } } } - catch(const Exception& ex) + catch(const std::exception&) { - exception(ex); + exception(current_exception()); } } @@ -393,13 +376,13 @@ Parser::current(const list<string>& args) try { - TopicManagerPrx manager = findManagerByCategory(args.front()); + auto manager = findManagerByCategory(args.front()); manager->ice_ping(); _defaultManager = manager; } - catch(const Exception& ex) + catch(const std::exception&) { - exception(ex); + exception(current_exception()); } } @@ -444,80 +427,77 @@ Parser::getInput(char* buf, size_t& result, size_t maxSize) } else { -#if defined(__APPLE__) || defined(__linux__) - if (isatty(fileno(stdin)) == 1) +#ifdef HAVE_READLINE + + const char* prompt = parser->getPrompt(); + char* line = readline(const_cast<char*>(prompt)); + if(!line) + { + result = 0; + } + else { - const char* prompt = parser->getPrompt(); - char* line = readline(const_cast<char*>(prompt)); - if(!line) + if(*line) + { + add_history(line); + } + + result = strlen(line) + 1; + if(result > maxSize) { + free(line); + error("input line too long"); result = 0; } else { - if(*line) - { - add_history(line); - } - - result = strlen(line) + 1; - if(result > maxSize) - { - free(line); - error("input line too long"); - result = 0; - } - else - { - strcpy(buf, line); - strcat(buf, "\n"); - free(line); - } + strcpy(buf, line); + strcat(buf, "\n"); + free(line); } } - else - { -#endif - consoleOut << parser->getPrompt() << flush; - string line; - while(true) - { - int c = getc(yyin); - if(c == EOF) - { - if(line.size()) - { - line += '\n'; - } - break; - } +#else + + consoleOut << parser->getPrompt() << flush; - line += static_cast<char>(c); - if(c == '\n') + string line; + while(true) + { + int c = getc(yyin); + if(c == EOF) + { + if(line.size()) { - break; + line += '\n'; } + break; } -#ifdef _WIN32 - if(windowsConsoleConverter) + + line += static_cast<char>(c); + if(c == '\n') { - line = nativeToUTF8(line, windowsConsoleConverter); + break; } + } +#ifdef _WIN32 + if(windowsConsoleConverter) + { + line = nativeToUTF8(line, windowsConsoleConverter); + } #endif - result = line.length(); - if(result > maxSize) - { - error("input line too long"); - buf[0] = EOF; - result = 1; - } - else - { - strcpy(buf, line.c_str()); - } -#if defined(__APPLE__) || defined(__linux__) + result = line.length(); + if(result > maxSize) + { + error("input line too long"); + buf[0] = EOF; + result = 1; + } + else + { + strcpy(buf, line.c_str()); } + #endif } } @@ -625,67 +605,51 @@ Parser::parse(const std::string& commands, bool debug) return status; } -TopicManagerPrx +shared_ptr<TopicManagerPrx> Parser::findManagerById(const string& full, string& arg) const { - Ice::Identity id = Ice::stringToIdentity(full); + auto id = Ice::stringToIdentity(full); arg = id.name; if(id.category.empty()) { return _defaultManager; } id.name = "TopicManager"; - map<Ice::Identity, TopicManagerPrx>::const_iterator p = _managers.find(id); + auto p = _managers.find(id); if(p == _managers.end()) { - throw UnknownManagerException(id.category, __FILE__, __LINE__); + throw UnknownManagerException(id.category); } return p->second; } -TopicManagerPrx +shared_ptr<TopicManagerPrx> Parser::findManagerByCategory(const string& full) const { - Ice::Identity id; - id.category = full; - id.name = "TopicManager"; - map<Ice::Identity, TopicManagerPrx>::const_iterator p = _managers.find(id); + Ice::Identity id = {"TopicManager", full}; + auto p = _managers.find(id); if(p == _managers.end()) { - throw UnknownManagerException(id.category, __FILE__, __LINE__); + throw UnknownManagerException(id.category); } return p->second; } -TopicPrx +shared_ptr<TopicPrx> Parser::findTopic(const string& full) const { string topicName; - TopicManagerPrx manager = findManagerById(full, topicName); + auto manager = findManagerById(full, topicName); return manager->retrieve(topicName); } -Parser::Parser(const CommunicatorPtr& communicator, const TopicManagerPrx& admin, - const map<Ice::Identity, TopicManagerPrx>& managers) : - _communicator(communicator), - _defaultManager(admin), - _managers(managers) -{ -#ifdef _WIN32 - if(!windowsConsoleConverter) - { - windowsConsoleConverter = Ice::createWindowsStringConverter(GetConsoleOutputCP()); - } -#endif -} - void -Parser::exception(const Ice::Exception& pex, bool warn) +Parser::exception(exception_ptr pex, bool warn) { ostringstream os; try { - pex.ice_throw(); + rethrow_exception(pex); } catch(const LinkExists& ex) { @@ -705,7 +669,7 @@ Parser::exception(const Ice::Exception& pex, bool warn) } catch(const UnknownManagerException& ex) { - os << "couldn't find IceStorm service `" << ex.name << "'"; + os << "couldn't find IceStorm service `" << ex.what() << "'"; } catch(const IdentityParseException& ex) { @@ -719,6 +683,10 @@ Parser::exception(const Ice::Exception& pex, bool warn) { os << ex; } + catch(const std::exception& ex) + { + os << ex.what(); + } if(warn) { |