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

Last change on this file since 16103 was 16090, checked in by tbretz, 12 years ago
Replaced queue by list; added Connection::IsTxQueueEmpty
File size: 3.5 KB
Line 
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
14class ConnectionUSB : public MessageImp, public boost::asio::serial_port
15{
16private:
17 MessageImp *fLog;
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
27 enum ConnectionStatus_t
28 {
29 kDisconnected = 0,
30 kConnecting = 1,
31 kConnected = 2,
32 };
33
34protected:
35 boost::asio::deadline_timer fInTimeout;
36
37private:
38 boost::asio::deadline_timer fOutTimeout;
39 boost::asio::deadline_timer fConnectTimeout;
40 std::list<std::vector<uint8_t>> fOutQueue;
41
42 ConnectionStatus_t fConnectionStatus;
43
44public:
45 void SetLogStream(MessageImp *log) { fLog = log; }
46 std::ostream &Out() { return fLog ? fLog->Out() : Out(); }
47
48 // -------- Abbreviations for starting async tasks ---------
49
50 void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0, int counter=0);
51 void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
52 void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
53 void (ConnectionUSB::*handler)(const boost::system::error_code&));
54
55protected:
56 void CloseImp(int64_t delay=0);
57
58private:
59 void ConnectImp(const boost::system::error_code& error,
60 boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
61
62 void HandleWriteTimeout(const boost::system::error_code &error);
63 void HandleSentData(const boost::system::error_code& error, size_t);
64 void HandleReconnectTimeout(const boost::system::error_code &error);
65
66 int Write(const Time &t, const std::string &txt, int qos=kInfo);
67
68 virtual void ConnectionEstablished() { }
69
70public:
71 ConnectionUSB(boost::asio::io_service& io_service, std::ostream &out);
72
73 // ------------------------ connect --------------------------
74
75 void SetEndpoint(const std::string &addr);
76
77 void Connect();
78
79 // ------------------------ close --------------------------
80 void PostClose(int64_t delay=0);
81
82 // ------------------------ write --------------------------
83 void SendMessageImp(const std::vector<uint8_t> msg);
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
Note: See TracBrowser for help on using the repository browser.