FACT++  1.0
Connection.h
Go to the documentation of this file.
1 #ifndef FACT_Connection
2 #define FACT_Connection
3 
4 #include <list>
5 #include <array>
6 #include <string>
7 
8 #include <boost/bind.hpp>
9 #include <boost/asio.hpp>
10 #include <boost/function.hpp>
11 #include <boost/asio/deadline_timer.hpp>
12 
13 #include "MessageImp.h"
14 
15 class Connection : public MessageImp, public boost::asio::ip::tcp::socket
16 {
17 private:
19 
20  std::string fAddress;
21  std::string fPort;
22 
23  boost::asio::ip::tcp::endpoint fEndpoint;
24 
25  bool fVerbose;
26  bool fDebugTx;
27 
29  {
33  };
34 
35 protected:
36  boost::asio::deadline_timer fInTimeout;
37 
38 private:
39  boost::asio::deadline_timer fOutTimeout;
40  boost::asio::deadline_timer fConnectionTimer;
41 
42  size_t fQueueSize;
43 
45 
46  std::string fErrConnect;
47  std::string fMsgConnect;
48 
49 public:
50  void SetLogStream(MessageImp *log) { fLog = log; }
51  std::ostream &Out() { return fLog ? fLog->Out() : Out(); }
52 
53  // -------- Abbreviations for starting async tasks ---------
54 
55  void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0);
56  void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
57 
58  template<class T>
59  void AsyncWaitImp(boost::asio::deadline_timer &timer, int millisec,
60  void (T::*handler)(const boost::system::error_code&))
61  {
62  // - The boost::asio::basic_deadline_timer::expires_from_now()
63  // function cancels any pending asynchronous waits, and returns
64  // the number of asynchronous waits that were cancelled. If it
65  // returns 0 then you were too late and the wait handler has
66  // already been executed, or will soon be executed. If it
67  // returns 1 then the wait handler was successfully cancelled.
68  // - If a wait handler is cancelled, the bs::error_code passed to
69  // it contains the value bs::error::operation_aborted.
70  timer.expires_from_now(boost::posix_time::milliseconds(millisec));
71 
72  timer.async_wait(boost::bind(handler, this, boost::asio::placeholders::error));
73  }
74 
75  void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
76  void (Connection::*handler)(const boost::system::error_code&))
77  {
78  AsyncWaitImp(timer, millisec, handler);
79  }
80 
81 
82 private:
83  void AsyncConnect(boost::asio::ip::tcp::resolver::iterator iterator);
84  void AsyncConnect();
85 
86  void CloseImp(bool restart=true);
87 
88  bool ConnectImp(const boost::asio::ip::tcp::endpoint &endpoint,
89  const boost::system::error_code& error);
90  void ConnectIter(boost::asio::ip::tcp::resolver::iterator endpoint_iterator,
91  const boost::system::error_code& error);
92  void ConnectAddr(const boost::asio::ip::tcp::endpoint &endpoint,
93  const boost::system::error_code& error);
94 
95  void HandleConnectionTimer(const boost::system::error_code &error);
96  void HandleWriteTimeout(const boost::system::error_code &error);
97  void HandleSentData(const boost::system::error_code& error, size_t);
98 
99  int Write(const Time &t, const std::string &txt, int qos=kInfo);
100 
101  virtual void ConnectionEstablished() { }
102  virtual void ConnectionFailed() { }
103 
104 public:
105  Connection(boost::asio::io_service& io_service, std::ostream &out);
106 
107  // ------------------------ connect --------------------------
108 
109  void SetEndpoint(const std::string &addr, int port);
110  void SetEndpoint(const std::string &addr, const std::string &port);
111  void SetEndpoint(const std::string &addr);
112  void SetEndpoint(const boost::asio::ip::tcp::endpoint &ep);
113 
114  virtual void StartConnect();
115 
116  // ------------------------ close --------------------------
117  void PostClose(bool restart=true);
118 
119  // ------------------------ write --------------------------
120  void PostMessage(const void *msg, size_t s=0);
121  void PostMessage(const std::string &cmd, size_t s=-1);
122 
123  template<typename T, size_t N>
124  void PostMessage(const std::array<T, N> &msg)
125  {
126  PostMessage(msg.begin(), msg.size()*sizeof(T));
127  }
128 
129  template<typename T>
130  void PostMessage(const std::vector<T> &msg)
131  {
132  PostMessage(&msg[0], msg.size()*sizeof(T));
133  }
134 
135  // ------------------------ others --------------------------
136 
137  virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0) { }
138  virtual void HandleReadTimeout(const boost::system::error_code&) { }
139 
140  bool IsTxQueueEmpty() const { return fQueueSize==0; /*fOutQueue.empty();*/ }
141 
142  int IsClosed() const { return !is_open(); }
143 
144  bool IsDisconnected() const { return fConnectionStatus==kDisconnected; }
145  bool IsConnected() const { return fConnectionStatus==kConnected; }
146  bool IsConnecting() const { return fConnectionStatus==kConnecting; }
147 
148  void SetVerbose(bool b=true) { fVerbose=b; }
149  void SetDebugTx(bool b=true) { fDebugTx=b; }
150 
151  std::string URL() const { return fAddress + ":" + fPort; }
152 
153  const boost::asio::ip::tcp::endpoint &GetEndpoint() const { return fEndpoint; }
154 };
155 
156 #endif
void PostMessage(const void *msg, size_t s=0)
void SetDebugTx(bool b=true)
Definition: Connection.h:149
boost::asio::deadline_timer fConnectionTimer
Definition: Connection.h:40
bool ConnectImp(const boost::asio::ip::tcp::endpoint &endpoint, const boost::system::error_code &error)
Definition: Connection.cc:257
void AsyncWait(boost::asio::deadline_timer &timer, int millisec, void(Connection::*handler)(const boost::system::error_code &))
Definition: Connection.h:75
boost::asio::deadline_timer fInTimeout
Definition: Connection.h:36
void PostClose(bool restart=true)
Definition: Connection.cc:125
virtual void HandleReceivedData(const boost::system::error_code &, size_t, int=0)
Definition: Connection.h:137
bool IsDisconnected() const
Definition: Connection.h:144
void HandleConnectionTimer(const boost::system::error_code &error)
Definition: Connection.cc:230
The base implementation of a distributed messaging system.
Definition: MessageImp.h:10
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
void CloseImp(bool restart=true)
Definition: Connection.cc:89
void AsyncConnect()
Definition: Connection.cc:76
bool IsConnected() const
Definition: Connection.h:145
int Write(const Time &t, const std::string &txt, int qos=kInfo)
Definition: Connection.cc:23
void HandleWriteTimeout(const boost::system::error_code &error)
Definition: Connection.cc:131
bool fDebugTx
Definition: Connection.h:26
void HandleSentData(const boost::system::error_code &error, size_t)
Definition: Connection.cc:166
bool IsConnecting() const
Definition: Connection.h:146
void SetEndpoint(const std::string &addr, int port)
std::ostream & Out() const
Definition: MessageImp.h:73
ConnectionStatus_t fConnectionStatus
Definition: Connection.h:44
virtual void ConnectionFailed()
Definition: Connection.h:102
void AsyncWaitImp(boost::asio::deadline_timer &timer, int millisec, void(T::*handler)(const boost::system::error_code &))
Definition: Connection.h:59
An info telling something which can be interesting to know.
Definition: MessageImp.h:17
std::ostream & Out()
Definition: Connection.h:51
virtual void StartConnect()
Definition: Connection.cc:352
void AsyncWrite(const boost::asio::const_buffers_1 &buffers)
Definition: Connection.cc:38
MessageImp * fLog
Definition: Connection.h:18
boost::asio::ip::tcp::endpoint fEndpoint
Definition: Connection.h:23
int type
uint16_t qos
Definition: HeadersGPS.h:29
std::string URL() const
Definition: Connection.h:151
void ConnectAddr(const boost::asio::ip::tcp::endpoint &endpoint, const boost::system::error_code &error)
Definition: Connection.cc:343
std::string fPort
Definition: Connection.h:21
std::string fErrConnect
Definition: Connection.h:46
void SetLogStream(MessageImp *log)
Definition: Connection.h:50
void SetVerbose(bool b=true)
Definition: Connection.h:148
std::string fAddress
Definition: Connection.h:20
void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0)
Definition: Connection.cc:31
std::string fMsgConnect
Definition: Connection.h:47
void ConnectIter(boost::asio::ip::tcp::resolver::iterator endpoint_iterator, const boost::system::error_code &error)
Definition: Connection.cc:326
virtual void HandleReadTimeout(const boost::system::error_code &)
Definition: Connection.h:138
const boost::asio::ip::tcp::endpoint & GetEndpoint() const
Definition: Connection.h:153
bool IsTxQueueEmpty() const
Definition: Connection.h:140
TT t
Definition: test_client.c:26
static void handler(int conn_id, char *packet, int size, int status)
Definition: webServer.c:635
bool fVerbose
Definition: Connection.h:25
void PostMessage(const std::array< T, N > &msg)
Definition: Connection.h:124
Connection(boost::asio::io_service &io_service, std::ostream &out)
Definition: Connection.cc:454
void PostMessage(const std::vector< T > &msg)
Definition: Connection.h:130
virtual void ConnectionEstablished()
Definition: Connection.h:101
boost::asio::deadline_timer fOutTimeout
Definition: Connection.h:39
Maintains an ansynchronous TCP/IP client connection.
Definition: Connection.h:15
ConnectionStatus_t
Definition: Connection.h:28
int IsClosed() const
Definition: Connection.h:142
size_t fQueueSize
Definition: Connection.h:42