0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
master.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; version 3 of the
9  * 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 
25 
26 #include <Hypertable/Lib/Client.h>
27 #include <Hypertable/Lib/Config.h>
30 
31 #include <Tools/Lib/CommandShell.h>
32 
34 
35 #include <Common/InetAddr.h>
36 #include <Common/Init.h>
37 
38 #include <chrono>
39 #include <iostream>
40 #include <thread>
41 
42 using namespace Hypertable;
43 using namespace Tools::client::master;
44 using namespace Config;
45 using namespace std;
46 
47 namespace {
48 
49  const char *usage =
50  "\n"
51  "Usage: ht_master [options] <host>[:<port>]\n\nOptions"
52  ;
53 
54  struct AppPolicy : Policy {
55  static void init_options() {
56  cmdline_desc(usage);
57  cmdline_hidden_desc().add_options()("address", str(), "");
58  cmdline_positional_desc().add("address", -1);
59  }
60  static void init() {
61  if (has("address")) {
62  Endpoint e = InetAddr::parse_endpoint(get_str("address"));
63  properties->set("master-host", e.host);
64  if (e.port) properties->set("master-port", e.port);
65  }
66  }
67  };
68 
69  typedef Meta::list<CommandShellPolicy, MasterClientPolicy,
70  DefaultCommPolicy, AppPolicy> Policies;
71 
72  class MasterDispatchHandler : public DispatchHandler {
73  public:
74  MasterDispatchHandler(bool silent) : m_silent(silent) { }
75  virtual void handle(EventPtr &event_ptr) {
76  if (event_ptr->type == Event::DISCONNECT) {
77  if (!m_connected) {
78  if (!m_silent)
79  cout << "Master CRITICAL - connect error" << endl;
80  quick_exit(2);
81  }
82  }
83  else if (event_ptr->type == Event::CONNECTION_ESTABLISHED)
84  m_connected = true;
85  }
86  private:
87  bool m_connected {};
88  bool m_silent {};
89  };
90 
91 } // local namespace
92 
93 
94 int main(int argc, char **argv) {
95  bool silent {};
96  int error = 1;
97 
98  try {
99  init_with_policies<Policies>(argc, argv);
100 
101  int timeout = get_i32("timeout");
102  InetAddr addr(get_str("master-host"), get_i16("master-port"));
103  silent = has("silent") && get_bool("silent");
104 
105  Comm *comm = Comm::instance();
106 
107  Lib::Master::ClientPtr client = make_shared<Lib::Master::Client>(comm, addr, timeout);
108 
109  DispatchHandlerPtr dispatch_handler_ptr = make_shared<MasterDispatchHandler>(silent);
110 
111  // connect to Master
112  if ((error = comm->connect(addr, dispatch_handler_ptr)) != Error::OK) {
113  if (!silent)
114  cout << "Master CRITICAL - connect error" << endl;
115  quick_exit(2);
116  }
117 
118  this_thread::sleep_for(chrono::milliseconds(100));
119 
120  CommandInterpreterPtr interp = make_shared<MasterCommandInterpreter>(client);
121 
122  CommandShellPtr shell = make_shared<CommandShell>("master", "Master", interp, properties);
123 
124  error = shell->run();
125  }
126  catch (Exception &e) {
127  if (!silent) {
128  cout << "Master CRITICAL - " << Error::get_text(e.code());
129  const char *msg = e.what();
130  if (msg && *msg)
131  cout << " - " << msg;
132  cout << endl;
133  }
134  quick_exit(2);
135  }
136  quick_exit(error);
137 }
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
int main(int argc, char **argv)
Definition: master.cc:94
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
Abstract base class for application dispatch handlers registered with AsyncComm.
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
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
STL namespace.
Connection established event.
Definition: Event.h:61
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
Declarations for DispatchHandler.
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
Encapsulate an internet address.
Definition: InetAddr.h:66
Compatibility Macros for C/C++.
Initialization helper for applications.
Connection disconnected event.
Definition: Event.h:62
std::shared_ptr< CommandInterpreter > CommandInterpreterPtr
std::shared_ptr< CommandShell > CommandShellPtr
Definition: CommandShell.h:91
Hypertable definitions
Declarations for HqlCommandInterpreter.
Entry point to AsyncComm service.
Definition: Comm.h:61
std::shared_ptr< Client > ClientPtr
Smart pointer to Client.
Definition: Client.h:201
Internet address wrapper classes and utility functions.
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.
Declarations for MasterClient This file contains declarations for MasterClient, a client interface cl...
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
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