FACT++  1.0
ConnectionUSB.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/asio.hpp>
9 #include <boost/function.hpp>
10 #include <boost/asio/deadline_timer.hpp>
11 
12 #include "MessageImp.h"
13 
14 class ConnectionUSB : public MessageImp, public boost::asio::serial_port
15 {
16 private:
18 
19  std::string fAddress;
20 
21  boost::asio::serial_port_base::baud_rate fBaudRate; // unisgned int
22  boost::asio::serial_port_base::character_size fCharacterSize; // unisgned int
23  boost::asio::serial_port_base::parity fParity; // unisgned int
24  boost::asio::serial_port_base::stop_bits fStopBits; // unisgned int
25  boost::asio::serial_port_base::flow_control fFlowControl; // unisgned int
26 
28  {
32  };
33 
34 protected:
35  boost::asio::deadline_timer fInTimeout;
36 
37 private:
38  boost::asio::deadline_timer fOutTimeout;
39  boost::asio::deadline_timer fConnectTimeout;
40 
41  size_t fQueueSize;
42 
44 
45 public:
46  void SetLogStream(MessageImp *log) { fLog = log; }
47  std::ostream &Out() { return fLog ? fLog->Out() : Out(); }
48 
49  // -------- Abbreviations for starting async tasks ---------
50 
51  void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0, int counter=0);
52  void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
53  void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
54  void (ConnectionUSB::*handler)(const boost::system::error_code&));
55 
56 protected:
57  void CloseImp(int64_t delay=0);
58 
59 private:
60  void ConnectImp(const boost::system::error_code& error,
61  boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
62 
63  void HandleWriteTimeout(const boost::system::error_code &error);
64  void HandleSentData(const boost::system::error_code& error, size_t);
65  void HandleReconnectTimeout(const boost::system::error_code &error);
66 
67  int Write(const Time &t, const std::string &txt, int qos=kInfo);
68 
69  virtual void ConnectionEstablished() { }
70 
71 public:
72  ConnectionUSB(boost::asio::io_service& io_service, std::ostream &out);
73 
74  // ------------------------ connect --------------------------
75 
76  void SetEndpoint(const std::string &addr);
77 
78  void Connect();
79 
80  // ------------------------ close --------------------------
81  void PostClose(int64_t delay=0);
82 
83  // ------------------------ write --------------------------
84  void PostMessage(const void *msg, size_t s=0);
85  void PostMessage(const std::string &cmd, size_t s=-1);
86 
87  template<typename T, size_t N>
88  void PostMessage(const std::array<T, N> &msg)
89  {
90  PostMessage(msg.begin(), msg.size()*sizeof(T));
91  }
92 
93  template<typename T>
94  void PostMessage(const std::vector<T> &msg)
95  {
96  PostMessage(&msg[0], msg.size()*sizeof(T));
97  }
98 
99  // ------------------------ others --------------------------
100 
101  virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0, int = 0) { }
102  virtual void HandleTransmittedData(size_t) { }
103  virtual void HandleReadTimeout(const boost::system::error_code&) { }
104 
105  int IsClosed() const { return !is_open(); }
106 
107  bool IsConnected() const { return fConnectionStatus==kConnected; }
108  bool IsConnecting() const { return fConnectionStatus==kConnecting; }
109 
110  std::string URL() const { return fAddress; }
111 };
112 
113 #endif
boost::asio::deadline_timer fOutTimeout
Definition: ConnectionUSB.h:38
std::string URL() const
boost::asio::serial_port_base::character_size fCharacterSize
Definition: ConnectionUSB.h:22
boost::asio::serial_port_base::flow_control fFlowControl
Definition: ConnectionUSB.h:25
int IsClosed() const
void SetLogStream(MessageImp *log)
Definition: ConnectionUSB.h:46
void SetEndpoint(const std::string &addr)
void PostMessage(const void *msg, size_t s=0)
void CloseImp(int64_t delay=0)
The base implementation of a distributed messaging system.
Definition: MessageImp.h:10
void AsyncWrite(const boost::asio::const_buffers_1 &buffers)
void ConnectImp(const boost::system::error_code &error, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
void AsyncWait(boost::asio::deadline_timer &timer, int millisec, void(ConnectionUSB::*handler)(const boost::system::error_code &))
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0, int counter=0)
virtual void HandleReceivedData(const boost::system::error_code &, size_t, int=0, int=0)
boost::asio::serial_port_base::baud_rate fBaudRate
Definition: ConnectionUSB.h:21
void PostMessage(const std::array< T, N > &msg)
Definition: ConnectionUSB.h:88
size_t fQueueSize
Definition: ConnectionUSB.h:41
std::ostream & Out() const
Definition: MessageImp.h:73
bool IsConnecting() const
An info telling something which can be interesting to know.
Definition: MessageImp.h:17
virtual void HandleReadTimeout(const boost::system::error_code &)
int type
uint16_t qos
Definition: HeadersGPS.h:29
void HandleSentData(const boost::system::error_code &error, size_t)
ConnectionUSB(boost::asio::io_service &io_service, std::ostream &out)
virtual void ConnectionEstablished()
Definition: ConnectionUSB.h:69
void HandleWriteTimeout(const boost::system::error_code &error)
ConnectionStatus_t fConnectionStatus
Definition: ConnectionUSB.h:43
std::string fAddress
Definition: ConnectionUSB.h:19
MessageImp * fLog
Definition: ConnectionUSB.h:17
boost::asio::serial_port_base::stop_bits fStopBits
Definition: ConnectionUSB.h:24
boost::asio::serial_port_base::parity fParity
Definition: ConnectionUSB.h:23
bool IsConnected() const
int counter
Definition: db_dim_client.c:19
void HandleReconnectTimeout(const boost::system::error_code &error)
void PostMessage(const std::vector< T > &msg)
Definition: ConnectionUSB.h:94
void PostClose(int64_t delay=0)
TT t
Definition: test_client.c:26
static void handler(int conn_id, char *packet, int size, int status)
Definition: webServer.c:635
boost::asio::deadline_timer fInTimeout
Definition: ConnectionUSB.h:35
std::ostream & Out()
Definition: ConnectionUSB.h:47
int Write(const Time &t, const std::string &txt, int qos=kInfo)
boost::asio::deadline_timer fConnectTimeout
Definition: ConnectionUSB.h:39
virtual void HandleTransmittedData(size_t)