0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
thriftbroker.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include <Common/Compat.h>
23 
24 #include "CommandInterpreter.h"
25 
26 #include <ThriftBroker/Config.h>
27 
28 #include <Tools/Lib/CommandShell.h>
29 
30 #include <AsyncComm/Config.h>
31 
32 #include <Common/Error.h>
33 #include <Common/Init.h>
34 #include <Common/Properties.h>
36 
37 using namespace Hypertable;
38 using namespace Tools::client;
39 using namespace Config;
40 using namespace std;
41 
42 namespace {
43 
44  const char *usage =
45  "\n"
46  "Usage: ht_thriftbroker [options] <host>[:<port>]\n\nOptions"
47  ;
48 
49  struct AppPolicy : Policy {
50  static void init_options() {
51  cmdline_desc(usage).add_options()
52  ("nowait", "Don't wait for certain commands to complete (e.g. shutdown)")
53  ("output-only", "Display status output and exit with status 0")
54  ;
55  cmdline_hidden_desc().add_options()("address", str(), "");
56  cmdline_positional_desc().add("address", -1);
57  }
58  static void init() {
59  if (has("address")) {
60  Endpoint e = InetAddr::parse_endpoint(get_str("address"));
61  properties->set("ThriftBroker.Host", e.host);
62  if (e.port)
63  properties->set("ThriftBroker.Port", e.port);
64  }
65  }
66  };
67 
68  typedef Meta::list<CommandShellPolicy, ThriftClientPolicy,
69  DefaultCommPolicy, AppPolicy> Policies;
70 
71 
72 }
73 
74 
75 int main(int argc, char **argv) {
76  int error = 1;
77  bool silent {};
78  bool output_only {};
79 
80  try {
81  init_with_policies<Policies>(argc, argv);
82  ::uint32_t timeout_ms;
83  bool nowait = has("nowait");
84 
85  output_only = has("output-only");
86  silent = has("silent") && get_bool("silent");
87 
88  if (has("timeout"))
89  timeout_ms = get_i32("timeout");
90  else
91  timeout_ms = get_i32("Hypertable.Request.Timeout");
92 
93  if (properties->has("host"))
94  properties->set("thrift-host", properties->get_str("host"));
95 
96  string host { get_str("thrift-host") };
97  int port { get_i16("thrift-port") };
98 
99  Thrift::ClientPtr client;
100  {
102  client = make_shared<Thrift::Client>(host, port, timeout_ms);
103  }
104 
105  CommandInterpreterPtr interp = make_shared<thriftbroker::CommandInterpreter>(client, nowait);
106 
107  CommandShellPtr shell = make_shared<CommandShell>("thriftbroker", "ThriftBroker", interp, properties);
108 
109  error = shell->run();
110  }
111  catch (Exception &e) {
112  if (!silent) {
113  cout << "ThriftBroker CRITICAL - " << Error::get_text(e.code());
114  const char *msg = e.what();
115  if (msg && *msg)
116  cout << " - " << msg;
117  cout << endl;
118  }
119  quick_exit(output_only ? 0 : 2);
120  }
121  catch (ThriftGen::ClientException &e) {
122  if (!silent) {
123  cout << "ThriftBroker CRITICAL - ";
124  if (e.__isset.code && e.code != 0)
125  cout << Error::get_text(e.code);
126  if (e.__isset.message && !e.message.empty())
127  cout << " - " << e.message;
128  cout << endl;
129  }
130  quick_exit(output_only ? 0 : 2);
131  }
132  catch (std::exception &e) {
133  if (!silent)
134  cout << "ThriftBroker CRITICAL - " << e.what() << endl;
135  quick_exit(output_only ? 0 : 2);
136  }
137 
138  quick_exit(error);
139 }
Declarations for configuration properties.
Interface and base of config policy.
Definition: Config.h:149
Cons< DefaultPolicy, CommPolicy > DefaultCommPolicy
Default comm layer config policy.
Definition: Config.h:84
PropertiesPtr properties
This singleton map stores all options.
Definition: Config.cc:47
static Endpoint parse_endpoint(const char *endpoint, int defport=0)
Parse an endpoint string in (host:port) format.
Definition: InetAddr.cc:181
void init(int argc, char *argv[], const Desc *desc=NULL)
Initialize with default policy.
Definition: Init.h:95
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
Program options handling.
STL namespace.
Desc & cmdline_desc(const char *usage)
A macro which definds global functions like get_bool(), get_str(), get_i16() etc. ...
Definition: Config.cc:72
bool has(const String &name)
Check existence of a configuration value.
Definition: Config.h:57
Declaration for ConsoleOutputSquelcher.
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
Compatibility Macros for C/C++.
int main(int argc, char **argv)
Definition: thriftbroker.cc:75
Initialization helper for applications.
std::shared_ptr< CommandInterpreter > CommandInterpreterPtr
std::shared_ptr< CommandShell > CommandShellPtr
Definition: CommandShell.h:91
Hypertable definitions
Temporarily redirects stdout and stderr to /dev/null.
Meta::list< MyPolicy, DefaultPolicy > Policies
This is a generic exception class for Hypertable.
Definition: Error.h:314
std::shared_ptr< Client > ClientPtr
Smart pointer to client.
Definition: Client.h:84
Desc & cmdline_hidden_desc()
Get the command line hidden options description (for positional options)
Definition: Config.cc:81
PositionalDesc & cmdline_positional_desc()
Get the command line positional options description.
Definition: Config.cc:90
Error codes, Exception handling, error logging.
Declarations for CommandInterpreter.
int code() const
Returns the error code.
Definition: Error.h:391
High-level entry point to a service; wraps a host:port pair.
Definition: InetAddr.h:44