0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Notifier.h
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 #ifndef Tools_Lib_Notifier_h
23 #define Tools_Lib_Notifier_h
24 
25 #include <AsyncComm/Comm.h>
26 #include <AsyncComm/CommAddress.h>
27 #include <AsyncComm/CommBuf.h>
28 
29 #include <Common/Error.h>
30 #include <Common/InetAddr.h>
31 
32 #include <cstdio>
33 #include <memory>
34 #include <string>
35 
36 namespace Hypertable {
37 
38  /*
39  * Helper class which sends notification to specified address.
40  */
41  class Notifier {
42  public:
43  Notifier(const char *addr_str) {
44  DispatchHandlerPtr null_handler(0);
45  InetAddr inet_addr;
47  if (!InetAddr::initialize(&inet_addr, addr_str)) {
48  exit(EXIT_FAILURE);
49  }
50  m_addr = inet_addr;
51  InetAddr::initialize(&inet_addr, INADDR_ANY, 0);
52  m_send_addr = inet_addr;
54  }
55 
56  Notifier() { }
57 
58  void notify() {
59  if (m_comm) {
60  int error;
61  CommHeader header(0);
62  CommBufPtr cbp(new CommBuf(header, 0));
63  if ((error = m_comm->send_datagram(m_addr, m_send_addr, cbp))
64  != Error::OK) {
65  HT_ERRORF("Problem sending datagram - %s", Error::get_text(error));
66  exit(EXIT_FAILURE);
67  }
68  }
69  }
70 
71  private:
72  Comm *m_comm {};
75  };
76 
77  typedef std::shared_ptr<Notifier> NotifierPtr;
78 
79 }
80 
81 #endif // Tools_Lib_Notifier_h
static Comm * instance()
Creates/returns singleton instance of the Comm class.
Definition: Comm.h:72
Declarations for CommAddress.
int send_datagram(const CommAddress &addr, const CommAddress &send_addr, CommBufPtr &cbuf)
Sends a datagram to a remote address.
Definition: Comm.cc:437
CommAddress m_addr
Definition: Notifier.h:73
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< Notifier > NotifierPtr
Definition: Notifier.h:77
std::shared_ptr< CommBuf > CommBufPtr
Smart pointer to CommBuf.
Definition: CommBuf.h:305
Hypertable definitions
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
Header for messages transmitted via AsyncComm.
Definition: CommHeader.h:40
Declarations for CommBuf.
Declarations for Comm.
Internet address wrapper classes and utility functions.
void create_datagram_receive_socket(CommAddress &addr, int tos, const DispatchHandlerPtr &handler)
Creates a socket for receiving datagrams and attaches handler as the default dispatch handler...
Definition: Comm.cc:367
std::shared_ptr< DispatchHandler > DispatchHandlerPtr
Smart pointer to DispatchHandler.
Message buffer for holding data to be transmitted over a network.
Definition: CommBuf.h:79
#define HT_ERRORF(msg,...)
Definition: Logger.h:300
Notifier(const char *addr_str)
Definition: Notifier.h:43
Error codes, Exception handling, error logging.
CommAddress m_send_addr
Definition: Notifier.h:74
Address abstraction to hold either proxy name or IPv4:port address.
Definition: CommAddress.h:52