source: trunk/FACT++/src/ConnectionUSB.h@ 15470

Last change on this file since 15470 was 13294, checked in by tbretz, 13 years ago
Added an automatic reconnect under certain conditions.
File size: 3.5 KB
Line 
1#ifndef FACT_Connection
2#define FACT_Connection
3
4#include <array>
5#include <deque>
6#include <string>
7#include <boost/asio.hpp>
8#include <boost/function.hpp>
9#include <boost/asio/deadline_timer.hpp>
10
11#include "MessageImp.h"
12
13class ConnectionUSB : public MessageImp, public boost::asio::serial_port
14{
15private:
16 MessageImp *fLog;
17
18 std::string fAddress;
19
20 boost::asio::serial_port_base::baud_rate fBaudRate; // unisgned int
21 boost::asio::serial_port_base::character_size fCharacterSize; // unisgned int
22 boost::asio::serial_port_base::parity fParity; // unisgned int
23 boost::asio::serial_port_base::stop_bits fStopBits; // unisgned int
24 boost::asio::serial_port_base::flow_control fFlowControl; // unisgned int
25
26 enum ConnectionStatus_t
27 {
28 kDisconnected = 0,
29 kConnecting = 1,
30 kConnected = 2,
31 };
32
33protected:
34 boost::asio::deadline_timer fInTimeout;
35
36private:
37 boost::asio::deadline_timer fOutTimeout;
38 boost::asio::deadline_timer fConnectTimeout;
39 std::deque<std::vector<uint8_t>> fOutQueue;
40
41 ConnectionStatus_t fConnectionStatus;
42
43public:
44 void SetLogStream(MessageImp *log) { fLog = log; }
45 std::ostream &Out() { return fLog ? fLog->Out() : Out(); }
46
47 // -------- Abbreviations for starting async tasks ---------
48
49 void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0, int counter=0);
50 void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
51 void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
52 void (ConnectionUSB::*handler)(const boost::system::error_code&));
53
54protected:
55 void CloseImp(int64_t delay=0);
56
57private:
58 void ConnectImp(const boost::system::error_code& error,
59 boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
60
61 void HandleWriteTimeout(const boost::system::error_code &error);
62 void HandleSentData(const boost::system::error_code& error, size_t);
63 void HandleReconnectTimeout(const boost::system::error_code &error);
64
65 int Write(const Time &t, const std::string &txt, int qos=kInfo);
66
67 virtual void ConnectionEstablished() { }
68
69public:
70 ConnectionUSB(boost::asio::io_service& io_service, std::ostream &out);
71
72 // ------------------------ connect --------------------------
73
74 void SetEndpoint(const std::string &addr);
75
76 void Connect();
77
78 // ------------------------ close --------------------------
79 void PostClose(int64_t delay=0);
80
81 // ------------------------ write --------------------------
82 void SendMessageImp(const std::vector<uint8_t> msg);
83 void PostMessage(const void *msg, size_t s=0);
84 void PostMessage(const std::string &cmd, size_t s=-1);
85
86 template<typename T, size_t N>
87 void PostMessage(const std::array<T, N> &msg)
88 {
89 PostMessage(msg.begin(), msg.size()*sizeof(T));
90 }
91
92 template<typename T>
93 void PostMessage(const std::vector<T> &msg)
94 {
95 PostMessage(&msg[0], msg.size()*sizeof(T));
96 }
97
98 // ------------------------ others --------------------------
99
100 virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0, int = 0) { }
101 virtual void HandleTransmittedData(size_t) { }
102 virtual void HandleReadTimeout(const boost::system::error_code&) { }
103
104 int IsClosed() const { return !is_open(); }
105
106 bool IsConnected() const { return fConnectionStatus==kConnected; }
107 bool IsConnecting() const { return fConnectionStatus==kConnecting; }
108
109 std::string URL() const { return fAddress; }
110};
111
112#endif
Note: See TracBrowser for help on using the repository browser.