μHAL (v2.7.9)
Part of the IPbus software repository
ProtocolTCP.hpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Andrew Rose, Imperial College, London
25  email: awr01 <AT> imperial.ac.uk
26 
27  Marc Magrans de Abril, CERN
28  email: marc.magrans.de.abril <AT> cern.ch
29 
30 ---------------------------------------------------------------------------
31 */
32 
39 #ifndef _uhal_ProtocolTCP_hpp_
40 #define _uhal_ProtocolTCP_hpp_
41 
42 
43 #include <deque>
44 #include <iostream>
45 #include <stdint.h>
46 #include <string>
47 #include <vector>
48 
49 #include <boost/shared_ptr.hpp>
50 #include <boost/asio/io_service.hpp>
51 #include <boost/asio/ip/tcp.hpp>
52 #include <boost/asio/deadline_timer.hpp>
53 #include <boost/chrono/system_clocks.hpp>
54 #include <boost/thread/thread.hpp>
55 #include <boost/thread/mutex.hpp>
56 #include <boost/thread/condition_variable.hpp>
57 
58 #include "uhal/ClientInterface.hpp"
59 #include "uhal/log/exception.hpp"
61 
62 namespace uhal
63 {
64  // Forward declarations
65  class Buffers;
66  struct URI;
67 
68 
69  namespace exception
70  {
72  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( TcpTimeout , ClientTimeout , "Exception class to handle the case where the TCP connection timed out." )
74  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( ErrorAtTcpSocketCreation , TransportLayerError , "Exception class to handle a failure to create a TCP socket." )
76  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( TcpConnectionFailure , TransportLayerError , "Exception class to handle the case where the TCP connection was refused or aborted." )
78  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( ASIOTcpError , TransportLayerError , "Exception class to handle the case where ASIO returned an error." )
79 
80  }
81 
83  template < typename InnerProtocol , std::size_t nr_buffers_per_send >
84  class TCP : public InnerProtocol
85  {
86 
87  private:
93  TCP ( const TCP& aTCP ); // non-copyable
94 
101  TCP& operator= ( const TCP& aTCP ); // non-assignable
102 
103  public:
109  TCP ( const std::string& aId, const URI& aUri );
110 
112  virtual ~TCP();
113 
114  private:
120  void implementDispatch ( boost::shared_ptr< Buffers > aBuffers );
121 
123  virtual void Flush( );
124 
126  virtual void dispatchExceptionHandler();
127 
132  uint32_t getMaxSendSize();
133 
138  uint32_t getMaxReplySize();
139 
141  void connect();
142 
148  void write ( );
149 
155  void write_callback ( const boost::system::error_code& aErrorCode , std::size_t aBytesTransferred );
156 
162  void read ( );
163 
169  void read_callback ( const boost::system::error_code& aErrorCode , std::size_t aBytesTransferred );
170 
172  void CheckDeadline();
173 
178  void NotifyConditionalVariable ( const bool& aValue );
179 
181  void WaitOnConditionalVariable();
182 
183 
184  private:
185  typedef boost::chrono::steady_clock SteadyClock_t;
186 
188  boost::asio::io_service mIOservice;
189 
191  boost::asio::ip::tcp::socket mSocket;
192 
194  boost::asio::ip::tcp::resolver::iterator mEndpoint;
195 
197  boost::asio::deadline_timer mDeadlineTimer;
198 
200  boost::asio::io_service::work mIOserviceWork;
201 
203  boost::thread mDispatchThread;
204 
206  boost::mutex mTransportLayerMutex;
207 
209  std::deque < boost::shared_ptr< Buffers > > mDispatchQueue;
211  std::deque < std::pair<std::vector< boost::shared_ptr< Buffers > >, SteadyClock_t::time_point> > mReplyQueue;
212 
215 
218 
222  boost::condition_variable mConditionalVariable;
225 
233 
240 
245  std::vector< boost::shared_ptr< Buffers > > mDispatchBuffers;
246 
251  std::pair< std::vector< boost::shared_ptr< Buffers > >, SteadyClock_t::time_point > mReplyBuffers;
252 
258 
259 
260  SteadyClock_t::time_point mLastSendQueued;
261  SteadyClock_t::time_point mLastRecvQueued;
262 
267  };
268 
269 }
270 
271 
272 #endif
uhal::TCP::mLastSendQueued
SteadyClock_t::time_point mLastSendQueued
Definition: ProtocolTCP.hpp:260
uhal::TCP::mDispatchThread
boost::thread mDispatchThread
The Worker thread in Multi-threaded mode.
Definition: ProtocolTCP.hpp:203
uhal::TCP::mReplyBuffers
std::pair< std::vector< boost::shared_ptr< Buffers > >, SteadyClock_t::time_point > mReplyBuffers
The buffers containing the payloads for the receive operation that's currently in progress.
Definition: ProtocolTCP.hpp:251
boost::shared_ptr
Definition: DerivedNodeFactory.hpp:52
uhal::TCP::mConditionalVariableMutex
boost::mutex mConditionalVariableMutex
A mutex for use by the conditional variable.
Definition: ProtocolTCP.hpp:220
uhal::TCP::mFlushDone
bool mFlushDone
A variable associated with the conditional variable which specifies whether all packets have been sen...
Definition: ProtocolTCP.hpp:224
TimeIntervalStats.hpp
uhal::TCP::mDeadlineTimer
boost::asio::deadline_timer mDeadlineTimer
The mechanism for providing the time-out.
Definition: ProtocolTCP.hpp:197
uhal::TCP::mLSTStats
TimeIntervalStats mLSTStats
Definition: ProtocolTCP.hpp:264
uhal::TCP::mAsynchronousException
uhal::exception::exception * mAsynchronousException
A pointer to an exception object for passing exceptions from the worker thread to the main thread.
Definition: ProtocolTCP.hpp:257
UHAL_DEFINE_DERIVED_EXCEPTION_CLASS
#define UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(ClassName, BaseClassName, ClassDescription)
Macro for simplifying the declaration and definition of derived exception types.
Definition: exception.hpp:49
uhal::TCP::mDispatchBuffers
std::vector< boost::shared_ptr< Buffers > > mDispatchBuffers
The buffers containing the payload for the send operation that's currently in progress.
Definition: ProtocolTCP.hpp:245
uhal::TCP::mEndpoint
boost::asio::ip::tcp::resolver::iterator mEndpoint
A shared pointer to a boost::asio tcp endpoint - used by the delayed (open-on-first-use) connect.
Definition: ProtocolTCP.hpp:194
uhal::TCP
Transport protocol to transfer an IPbus buffer via TCP.
Definition: ProtocolTCP.hpp:85
uhal::exception::exception
An abstract base exception class, including an interface to throw as the derived type (for passing ex...
Definition: exception.hpp:71
uhal::TCP::mLastRecvQueued
SteadyClock_t::time_point mLastRecvQueued
Definition: ProtocolTCP.hpp:261
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::TCP::mFlushStarted
bool mFlushStarted
Boolean specifying whether or not the main thread is within TCP::Flush method. Its value checked by t...
Definition: ProtocolTCP.hpp:217
uhal::TCP::mIOserviceWork
boost::asio::io_service::work mIOserviceWork
Needed when multi-threading to stop the boost::asio::io_service thinking it has nothing to do and so ...
Definition: ProtocolTCP.hpp:200
uhal::TCP::SteadyClock_t
boost::chrono::steady_clock SteadyClock_t
Definition: ProtocolTCP.hpp:185
uhal::TCP::mRTTStats
TimeIntervalStats mRTTStats
Definition: ProtocolTCP.hpp:263
uhal::TCP::mInterRecvTimeStats
TimeIntervalStats mInterRecvTimeStats
Definition: ProtocolTCP.hpp:266
uhal::TCP::mSendByteCounter
uint32_t mSendByteCounter
Variable storing "number of bytes to follow" field for the TCP chunk currently being sent.
Definition: ProtocolTCP.hpp:232
uhal::TCP::mConditionalVariable
boost::condition_variable mConditionalVariable
A conditional variable for blocking the main thread until the variable with which it is associated is...
Definition: ProtocolTCP.hpp:222
uhal::TCP::mReplyQueue
std::deque< std::pair< std::vector< boost::shared_ptr< Buffers > >, SteadyClock_t::time_point > > mReplyQueue
The list of buffers still awaiting a reply.
Definition: ProtocolTCP.hpp:211
uhal::TCP::mSocket
boost::asio::ip::tcp::socket mSocket
A shared pointer to a boost::asio tcp socket through which the operation will be performed.
Definition: ProtocolTCP.hpp:191
uhal::TCP::TCP
TCP(const TCP &aTCP)
Copy Constructor This creates a new socket, dispatch queue, dispatch thread, etc.
uhal::TCP::mInterSendTimeStats
TimeIntervalStats mInterSendTimeStats
Definition: ProtocolTCP.hpp:265
uhal::TCP::mPacketsInFlight
uint32_t mPacketsInFlight
Counter of how many writes have been sent, for which no reply has yet been received.
Definition: ProtocolTCP.hpp:214
uhal::TCP::mIOservice
boost::asio::io_service mIOservice
The boost::asio::io_service used to create the connections.
Definition: ProtocolTCP.hpp:188
uhal::TCP::mTransportLayerMutex
boost::mutex mTransportLayerMutex
A MutEx lock used to make sure the access functions are thread safe.
Definition: ProtocolTCP.hpp:206
uhal::TCP::mDispatchQueue
std::deque< boost::shared_ptr< Buffers > > mDispatchQueue
The list of buffers still waiting to be sent.
Definition: ProtocolTCP.hpp:209
uhal::tests::write
c write(addr, xx[0])
uhal::URI
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:50
exception.hpp
uhal::TimeIntervalStats
Definition: TimeIntervalStats.hpp:15
ClientInterface.hpp
uhal::TCP::mReplyByteCounter
uint32_t mReplyByteCounter
Variable used to store "number of bytes to follow" field for the next/current TCP chunk being receive...
Definition: ProtocolTCP.hpp:239