source: trunk/FACT++/src/Connection.h@ 11185

Last change on this file since 11185 was 11118, checked in by tbretz, 13 years ago
Added possibility to debug data transmission to the boards.
File size: 3.5 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 Connection : public MessageImp, public boost::asio::ip::tcp::socket
13{
14private:
15 MessageImp *fLog;
16
17 std::string fAddress;
18 std::string fPort;
19
20 bool fDebugTx;
21
22 enum ConnectionStatus_t
23 {
24 kDisconnected = 0,
25 kConnecting = 1,
26 kConnected = 2,
27 };
28
29protected:
30 boost::asio::deadline_timer fInTimeout;
31
32private:
33 boost::asio::deadline_timer fOutTimeout;
34 boost::asio::deadline_timer fConnectionTimer;
35 std::deque<std::vector<char>> fOutQueue;
36
37 ConnectionStatus_t fConnectionStatus;
38
39 std::string fErrConnect;
40 std::string fMsgConnect;
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 (Connection::*handler)(const boost::system::error_code&));
52
53private:
54 void AsyncConnect(boost::asio::ip::tcp::resolver::iterator iterator);
55
56 void CloseImp(bool restart=true);
57
58 void ConnectImp(const boost::system::error_code& error,
59 boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
60
61 void HandleConnectionTimer(const boost::system::error_code &error);
62 void HandleWriteTimeout(const boost::system::error_code &error);
63 void HandleSentData(const boost::system::error_code& error, size_t);
64
65 int Write(const Time &t, const std::string &txt, int qos=kInfo);
66
67 virtual void ConnectionEstablished() { }
68
69public:
70 Connection(boost::asio::io_service& io_service, std::ostream &out);
71
72 // ------------------------ connect --------------------------
73
74 void SetEndpoint(const std::string &addr, int port);
75 void SetEndpoint(const std::string &addr, const std::string &port);
76 void SetEndpoint(const std::string &addr);
77
78 void StartConnect();
79
80 // ------------------------ close --------------------------
81 void PostClose(bool restart=true);
82
83 // ------------------------ write --------------------------
84 void SendMessageImp(const std::vector<char> msg);
85 void PostMessage(const void *msg, size_t s=0);
86 void PostMessage(const std::string &cmd, size_t s=-1);
87
88 template<typename T, size_t N>
89 void PostMessage(const boost::array<T, N> &msg)
90 {
91 PostMessage(msg.begin(), msg.size()*sizeof(T));
92 }
93
94 template<typename T>
95 void PostMessage(const std::vector<T> &msg)
96 {
97 PostMessage(&msg[0], msg.size()*sizeof(T));
98 }
99
100 // ------------------------ others --------------------------
101
102 virtual void HandleReceivedData(const boost::system::error_code&, size_t, int = 0) { }
103 virtual void HandleReadTimeout(const boost::system::error_code&) { }
104
105 int IsClosed() const { return !is_open(); }
106
107 bool IsDisconnected() const { return fConnectionStatus==kDisconnected; }
108 bool IsConnected() const { return fConnectionStatus==kConnected; }
109 bool IsConnecting() const { return fConnectionStatus==kConnecting; }
110
111 void SetDebugTx(bool b=true) { fDebugTx=b; }
112
113 std::string URL() const { return fAddress + ":" + fPort; }
114};
115
116#endif
Note: See TracBrowser for help on using the repository browser.