0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fsbroker.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 <Hypertable/Lib/Config.h>
27 
28 #include <FsBroker/Lib/Config.h>
29 
30 #include <Tools/Lib/CommandShell.h>
31 
32 #include <AsyncComm/Comm.h>
33 #include <AsyncComm/Config.h>
34 
35 #include <Common/Error.h>
36 #include <Common/Init.h>
37 #include <Common/Properties.h>
38 #include <Common/Usage.h>
39 
40 #include <boost/algorithm/string.hpp>
41 #include <boost/thread/condition.hpp>
42 #include <boost/thread/mutex.hpp>
43 
44 #include <cstring>
45 #include <iostream>
46 #include <vector>
47 
48 extern "C" {
49 #include <editline/readline.h>
50 }
51 
52 using namespace Hypertable;
53 using namespace Tools::client;
54 using namespace Config;
55 using namespace std;
56 
57 namespace {
58 
59  const char *usage =
60  "\n"
61  "Usage: ht_fsbroker [options] <host>[:<port>]\n\nOptions"
62  ;
63 
64  struct AppPolicy : Policy {
65  static void init_options() {
66  cmdline_desc(usage).add_options()
67  ("nowait", "Don't wait for certain commands to complete (e.g. shutdown)")
68  ("output-only", "Display status output and exit with status 0")
69  ;
70  cmdline_hidden_desc().add_options()("address", str(), "");
71  cmdline_positional_desc().add("address", -1);
72  }
73  static void init() {
74  if (has("address")) {
75  Endpoint e = InetAddr::parse_endpoint(get_str("address"));
76  properties->set("FsBroker.Host", e.host);
77  if (e.port)
78  properties->set("FsBroker.Port", e.port);
79  }
80  }
81  };
82 
83  typedef Meta::list<CommandShellPolicy, FsClientPolicy,
84  DefaultCommPolicy, AppPolicy> Policies;
85 
86 }
87 
88 
89 int main(int argc, char **argv) {
90  int error = 1;
91  bool silent {};
92  bool output_only {};
93 
94  try {
95  init_with_policies<Policies>(argc, argv);
96  InetAddr addr;
97  String host = get_str("FsBroker.Host");
98  ::uint16_t port;
99  ::uint32_t timeout_ms;
100  bool nowait = has("nowait");
101 
102  output_only = has("output-only");
103  silent = has("silent") && get_bool("silent");
104 
105  if (has("DfsBroker.Port"))
106  port = get_i16("DfsBroker.Port");
107  else
108  port = get_i16("FsBroker.Port");
109 
110  if (has("timeout"))
111  timeout_ms = get_i32("timeout");
112  else
113  timeout_ms = get_i32("Hypertable.Request.Timeout");
114 
115  InetAddr::initialize(&addr, host.c_str(), port);
116 
118  DispatchHandlerPtr default_handler(sync_handler);
119  EventPtr event;
120  Comm *comm = Comm::instance();
121 
122  if ((error = comm->connect(addr, default_handler)) != Error::OK) {
123  if (!silent)
124  cout << "FsBroker CRITICAL - " << Error::get_text(error) << endl;
125  quick_exit(output_only ? 0 : 2);
126  }
127 
129  if (!sync_handler->wait_for_connection()) {
130  if (!silent)
131  cout << "FsBroker CRITICAL - connect error" << endl;
132  quick_exit(output_only ? 0 : 2);
133  }
134 
135  FsBroker::Lib::ClientPtr client = make_shared<FsBroker::Lib::Client>(comm, addr, timeout_ms);
136 
137  CommandInterpreterPtr interp = make_shared<fsbroker::CommandInterpreter>(client, nowait);
138 
139  CommandShellPtr shell = make_shared<CommandShell>("fsbroker", "FsBroker", interp, properties);
140 
141  error = shell->run();
142  }
143  catch (Exception &e) {
144  if (!silent) {
145  cout << "FsBroker CRITICAL - " << Error::get_text(e.code());
146  const char *msg = e.what();
147  if (msg && *msg)
148  cout << " - " << msg;
149  cout << endl;
150  }
151  quick_exit(output_only ? 0 : 2);
152  }
153  quick_exit(error);
154 }
Declarations for configuration properties.
static Comm * instance()
Creates/returns singleton instance of the Comm class.
Definition: Comm.h:72
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
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
static Endpoint parse_endpoint(const char *endpoint, int defport=0)
Parse an endpoint string in (host:port) format.
Definition: InetAddr.cc:181
int connect(const CommAddress &addr, const DispatchHandlerPtr &default_handler)
Establishes a TCP connection and attaches a default dispatch handler.
Definition: Comm.cc:134
Helper class for printing usage banners on the command line.
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.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
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
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
Encapsulate an internet address.
Definition: InetAddr.h:66
std::shared_ptr< Client > ClientPtr
Smart pointer to Client.
Definition: Client.h:233
Declarations for CommandInterpreter.
Compatibility Macros for C/C++.
Initialization helper for applications.
std::shared_ptr< CommandInterpreter > CommandInterpreterPtr
std::shared_ptr< CommandShell > CommandShellPtr
Definition: CommandShell.h:91
Hypertable definitions
DispatchHandler class used to synchronize with response messages.
static bool initialize(sockaddr_in *addr, const char *host, uint16_t port)
Initialize a sockaddr_in structure from host:port.
Definition: InetAddr.cc:68
Entry point to AsyncComm service.
Definition: Comm.h:61
Declarations for Comm.
Meta::list< MyPolicy, DefaultPolicy > Policies
This is a generic exception class for Hypertable.
Definition: Error.h:314
std::shared_ptr< DispatchHandler > DispatchHandlerPtr
Smart pointer to DispatchHandler.
Desc & cmdline_hidden_desc()
Get the command line hidden options description (for positional options)
Definition: Config.cc:81
int main(int argc, char **argv)
Definition: fsbroker.cc:89
PositionalDesc & cmdline_positional_desc()
Get the command line positional options description.
Definition: Config.cc:90
Error codes, Exception handling, error logging.
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