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

Last change on this file since 11683 was 11471, checked in by tbretz, 13 years ago
Replaced boost::array by std::array
File size: 3.3 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 std::deque<std::vector<char>> fOutQueue;
39
40 ConnectionStatus_t fConnectionStatus;
41
42public:
43 void SetLogStream(MessageImp *log) { fLog = log; }
44 std::ostream &Out() { return fLog ? fLog->Out() : Out(); }
45
46 // -------- Abbreviations for starting async tasks ---------
47
48 void AsyncRead(const boost::asio::mutable_buffers_1 buffers, int type=0);
49 void AsyncWrite(const boost::asio::const_buffers_1 &buffers);
50 void AsyncWait(boost::asio::deadline_timer &timer, int millisec,
51 void (ConnectionUSB::*handler)(const boost::system::error_code&));
52
53private:
54 void CloseImp(bool restart=true);
55
56 void ConnectImp(const boost::system::error_code& error,
57 boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
58
59 void HandleWriteTimeout(const boost::system::error_code &error);
60 void HandleSentData(const boost::system::error_code& error, size_t);
61
62 int Write(const Time &t, const std::string &txt, int qos=kInfo);
63
64 virtual void ConnectionEstablished() { }
65
66public:
67 ConnectionUSB(boost::asio::io_service& io_service, std::ostream &out);
68
69 // ------------------------ connect --------------------------
70
71 void SetEndpoint(const std::string &addr);
72
73 void Connect();
74
75 // ------------------------ close --------------------------
76 void PostClose(bool restart=true);
77
78 // ------------------------ write --------------------------
79 void SendMessageImp(const std::vector<char> msg);
80 void PostMessage(const void *msg, size_t s=0);
81 void PostMessage(const std::string &cmd, size_t s=-1);
82
83 template<typename T, size_t N>
84 void PostMessage(const std::array<T, N> &msg)
85 {
86 PostMessage(msg.begin(), msg.size()*sizeof(T));
87 }
88
89 template<typename T>
90 void PostMessage(const std::vector<T> &msg)
91 {
92 PostMessage(&msg[0], msg.size()*sizeof(T));
93 }
94
95 // ------------------------ others --------------------------
96
97 virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0) { }
98 virtual void HandleTransmittedData(size_t) { }
99 virtual void HandleReadTimeout(const boost::system::error_code&) { }
100
101 int IsClosed() const { return !is_open(); }
102
103 bool IsConnected() const { return fConnectionStatus==kConnected; }
104 bool IsConnecting() const { return fConnectionStatus==kConnecting; }
105
106 std::string URL() const { return fAddress; }
107};
108
109#endif
Note: See TracBrowser for help on using the repository browser.