Admin.cpp
Go to the documentation of this file.
1// **********************************************************************
2//
3// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
4//
5// This copy of Ice is licensed to you under the terms described in the
6// ICE_LICENSE file included in this distribution.
7//
8// **********************************************************************
9
10#include <Ice/Application.h>
11#include <Ice/ConsoleUtil.h>
12#include <Ice/SliceChecksums.h>
13#include <IceStorm/Parser.h>
14#include <IceUtil/Options.h>
15
16#ifdef _WIN32
17#include <fcntl.h>
18
19#include <io.h>
20#endif
21
22using namespace Ice;
23using namespace IceInternal;
24using namespace IceStorm;
25
26class Client : public Ice::Application
27{
28public:
29 void usage();
30 virtual int run(int, char*[]);
31};
32
33#ifdef _WIN32
34
35int
36wmain(int argc, wchar_t* argv[])
37{
38 //
39 // Enable binary input mode for stdin to avoid automatic conversions.
40 //
41 _setmode(_fileno(stdin), _O_BINARY);
42#else
43
44int
45main(int argc, char* argv[])
46{
47#endif
48 Client app;
49 Ice::InitializationData id;
50 Ice::StringSeq args = Ice::argsToStringSeq(argc, argv);
51 id.properties = Ice::createProperties(args);
52 id.properties->setProperty("Ice.Warn.Endpoints", "0");
53 int rc = app.main(argc, argv, id);
54 return rc;
55}
56
57void
59{
60 consoleErr << "Usage: " << appName() << " [options]\n";
61 consoleErr << "Options:\n"
62 "-h, --help Show this message.\n"
63 "-v, --version Display the Ice version.\n"
64 "-e COMMANDS Execute COMMANDS.\n"
65 "-d, --debug Print debug messages.\n";
66}
67
68int
69Client::run(int argc, char* argv[])
70{
71 std::string commands;
72 bool debug;
73
74 IceUtilInternal::Options opts;
75 opts.addOpt("h", "help");
76 opts.addOpt("v", "version");
77 opts.addOpt("e", "", IceUtilInternal::Options::NeedArg, "", IceUtilInternal::Options::Repeat);
78 opts.addOpt("d", "debug");
79
80 std::vector<std::string> args;
81 try
82 {
83 args = opts.parse(argc, const_cast<const char**>(argv));
84 }
85 catch (const IceUtilInternal::BadOptException& e)
86 {
87 consoleErr << e.reason << std::endl;
88 usage();
89 return EXIT_FAILURE;
90 }
91 if (!args.empty())
92 {
93 consoleErr << argv[0] << ": too many arguments" << std::endl;
94 usage();
95 return EXIT_FAILURE;
96 }
97
98 if (opts.isSet("help"))
99 {
100 usage();
101 return EXIT_SUCCESS;
102 }
103 if (opts.isSet("version"))
104 {
105 consoleOut << ICE_STRING_VERSION << std::endl;
106 return EXIT_SUCCESS;
107 }
108 if (opts.isSet("e"))
109 {
110 std::vector<std::string> optargs = opts.argVec("e");
111 for (std::vector<std::string>::const_iterator i = optargs.begin(); i != optargs.end(); ++i)
112 {
113 commands += *i + ";";
114 }
115 }
116 debug = opts.isSet("debug");
117
118 // The complete set of Ice::Identity -> manager proxies.
119 std::map<Ice::Identity, IceStorm::TopicManagerPrx> managers;
120 PropertiesPtr properties = communicator()->getProperties();
121 IceStorm::TopicManagerPrx defaultManager;
122
123 Ice::PropertyDict props =
124 communicator()->getProperties()->getPropertiesForPrefix("IceStormAdmin.TopicManager.");
125 {
126 for (Ice::PropertyDict::const_iterator p = props.begin(); p != props.end(); ++p)
127 {
128 //
129 // Ignore proxy property settings. eg IceStormAdmin.TopicManager.*.LocatorCacheTimeout
130 //
131 if (p->first.find('.', strlen("IceStormAdmin.TopicManager.")) == std::string::npos)
132 {
133 try
134 {
135 IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::uncheckedCast(
136 communicator()->propertyToProxy(p->first));
137 managers.insert(std::map<Ice::Identity, IceStorm::TopicManagerPrx>::value_type(
138 manager->ice_getIdentity(), manager));
139 }
140 catch (const Ice::ProxyParseException&)
141 {
142 consoleErr << appName() << ": malformed proxy: " << p->second << std::endl;
143 return EXIT_FAILURE;
144 }
145 }
146 }
147
148 std::string managerProxy = properties->getProperty("IceStormAdmin.TopicManager.Default");
149 if (!managerProxy.empty())
150 {
151 defaultManager = IceStorm::TopicManagerPrx::uncheckedCast(
152 communicator()->stringToProxy(managerProxy));
153 }
154 else if (!managers.empty())
155 {
156 defaultManager = managers.begin()->second;
157 }
158 }
159
160 if (!defaultManager)
161 {
162 std::string host = properties->getProperty("IceStormAdmin.Host");
163 std::string port = properties->getProperty("IceStormAdmin.Port");
164
165 const int timeout = 3000; // 3s connection timeout.
166 std::ostringstream os;
167 os << "IceStorm/Finder";
168 os << ":tcp" << (host.empty() ? "" : (" -h \"" + host + "\"")) << " -p " << port << " -t "
169 << timeout;
170 os << ":ssl" << (host.empty() ? "" : (" -h \"" + host + "\"")) << " -p " << port << " -t "
171 << timeout;
172 IceStorm::FinderPrx finder =
173 IceStorm::FinderPrx::uncheckedCast(communicator()->stringToProxy(os.str()));
174 try
175 {
176 defaultManager = finder->getTopicManager();
177 }
178 catch (const Ice::LocalException&)
179 {
180 // Ignore.
181 }
182 }
183
184 if (!defaultManager)
185 {
186 consoleErr << appName() << ": no manager proxies configured" << std::endl;
187 return EXIT_FAILURE;
188 }
189
190 ParserPtr p = Parser::createParser(communicator(), defaultManager, managers);
191 int status = EXIT_SUCCESS;
192
193 if (!commands.empty()) // Commands were given
194 {
195 int parseStatus = p->parse(commands, debug);
196 if (parseStatus == EXIT_FAILURE)
197 {
198 status = EXIT_FAILURE;
199 }
200 }
201 else // No commands, let's use standard input
202 {
203 p->showBanner();
204
205 int parseStatus = p->parse(stdin, debug);
206 if (parseStatus == EXIT_FAILURE)
207 {
208 status = EXIT_FAILURE;
209 }
210 }
211
212 return status;
213}
void usage()
Definition Admin.cpp:58
virtual int run(int, char *[])
Definition Admin.cpp:69
static ParserPtr createParser(const Ice::CommunicatorPtr &, const TopicManagerPrx &, const std::map< Ice::Identity, TopicManagerPrx > &)
Definition Parser.cpp:83
This file is part of ArmarX.
::IceUtil::Handle< Parser > ParserPtr
Definition Parser.h:53
::IceInternal::ProxyHandle<::IceProxy::IceStorm::TopicManager > TopicManagerPrx
Definition IceManager.h:69
Ice::PropertiesPtr createProperties()
::IceInternal::Handle<::Ice::Properties > PropertiesPtr