0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
rangeserver.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/Config.h>
27 #include <Hypertable/Lib/Client.h>
30 
31 #include <Hyperspace/Session.h>
32 
33 #include <Tools/Lib/CommandShell.h>
34 
36 
37 #include <Common/Init.h>
38 #include <Common/InetAddr.h>
39 
40 #include <chrono>
41 #include <iostream>
42 #include <thread>
43 
44 using namespace Hypertable;
45 using namespace Hypertable::Lib;
46 using namespace Config;
47 using namespace Tools::client::rangeserver;
48 using namespace std;
49 
50 namespace {
51 
52  const char *usage =
53  "\n"
54  "Usage: ht_rangeserver [options] <host>[:<port>]\n\nOptions"
55  ;
56 
57  struct AppPolicy : Policy {
58  static void init_options() {
59  cmdline_desc(usage).add_options()
60  ("no-hyperspace", "Do not establish a connection to hyperspace")
61  ;
62  cmdline_hidden_desc().add_options()("address", str(), "");
63  cmdline_positional_desc().add("address", -1);
64  }
65  static void init() {
66  if (has("address")) {
67  Endpoint e = InetAddr::parse_endpoint(get_str("address"));
68  properties->set("rs-host", e.host);
69  if (e.port) properties->set("rs-port", e.port);
70  }
71  }
72  };
73 
74  typedef Meta::list<CommandShellPolicy, HyperspaceClientPolicy,
76 
77  class RangeServerDispatchHandler : public DispatchHandler {
78  public:
79  RangeServerDispatchHandler(bool silent) : m_silent(silent) { }
80  virtual void handle(EventPtr &event_ptr) {
81  if (event_ptr->type == Event::DISCONNECT) {
82  if (!m_connected) {
83  if (!m_silent)
84  cout << "RangeServer CRITICAL - connect error" << endl;
85  quick_exit(2);
86  }
87  }
88  else if (event_ptr->type == Event::CONNECTION_ESTABLISHED)
89  m_connected = true;
90  }
91  private:
92  bool m_silent {};
93  bool m_connected {};
94  };
95 
96 } // local namespace
97 
98 
99 int main(int argc, char **argv) {
100  bool silent {};
101  int error = 1;
102  bool no_hyperspace = false;
103 
104  try {
105  init_with_policies<Policies>(argc, argv);
106 
107  int timeout = get_i32("timeout");
108  InetAddr addr(get_str("rs-host"), get_i16("rs-port"));
109  silent = has("silent") && get_bool("silent");
110 
111  if (has("no-hyperspace"))
112  no_hyperspace = true;
113 
114  Comm *comm = Comm::instance();
115 
116  // Create Range Server client object
117  RangeServer::ClientPtr client = make_shared<RangeServer::Client>(comm, timeout);
118 
119  DispatchHandlerPtr dispatch_handler_ptr = make_shared<RangeServerDispatchHandler>(silent);
120  // connect to RangeServer
121  if ((error = comm->connect(addr, dispatch_handler_ptr)) != Error::OK) {
122  if (!silent)
123  cout << "RangeServer CRITICAL - connect error" << endl;
124  quick_exit(2);
125  }
126 
127  this_thread::sleep_for(chrono::milliseconds(100));
128 
129  // Maybe connect to Hyperspace
130  Hyperspace::SessionPtr hyperspace;
131  if (!no_hyperspace) {
132  hyperspace = make_shared<Hyperspace::Session>(comm, properties);
133  if (!hyperspace->wait_for_connection(timeout)) {
134  if (!silent)
135  cout << "RangeServer CRITICAL - Unable to connecto to Hyperspace" << endl;
136  quick_exit(2);
137  }
138  }
139 
140  CommandInterpreterPtr interp =
141  make_shared<RangeServerCommandInterpreter>(hyperspace, addr, client);
142 
143  CommandShellPtr shell = make_shared<CommandShell>("rangeserver", "RangeServer", interp, properties);
144 
145  error = shell->run();
146  }
147  catch (Exception &e) {
148  if (!silent) {
149  cout << "RangeServer CRITICAL - " << Error::get_text(e.code());
150  const char *msg = e.what();
151  if (msg && *msg)
152  cout << " - " << msg;
153  cout << endl;
154  }
155  quick_exit(2);
156  }
157  quick_exit(error);
158 }
static Comm * instance()
Creates/returns singleton instance of the Comm class.
Definition: Comm.h:72
int main(int argc, char **argv)
Definition: rangeserver.cc:99
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
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.
Declarations for RangeServerClient.
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.
std::shared_ptr< Session > SessionPtr
Definition: Session.h:734
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:594
Compatibility Macros for C/C++.
Initialization helper for applications.
Connection disconnected event.
Definition: Event.h:62
std::shared_ptr< CommandInterpreter > CommandInterpreterPtr
Hypertable library.
Definition: CellInterval.h:30
std::shared_ptr< CommandShell > CommandShellPtr
Definition: CommandShell.h:91
Hypertable definitions
Declarations for HqlCommandInterpreter.
Entry point to AsyncComm service.
Definition: Comm.h:61
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.
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