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

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