μHAL (v2.6.5)
Part of the IPbus software repository
ProtocolUDP.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_ProtocolUDP_hpp_
40 #define _uhal_ProtocolUDP_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/udp.hpp>
52 #include <boost/asio/deadline_timer.hpp>
53 
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"
60 
61 
62 namespace boost {
63  namespace system { class error_code; }
64 }
65 
66 namespace uhal
67 {
68  // Forward declarations
69  class Buffers;
70  struct URI;
71 
72  namespace exception
73  {
75  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( UdpTimeout , ClientTimeout , "Exception class to handle the case where the UDP connection timed out." )
77  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( ErrorAtUdpSocketCreation , TransportLayerError , "Exception class to handle a failure to create a UDP socket." )
79  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( ASIOUdpError , TransportLayerError , "Exception class to handle the case where ASIO returned an error." )
80  }
81 
83  template < typename InnerProtocol >
84  class UDP : public InnerProtocol
85  {
86 
87  private:
93  UDP ( const UDP& aUDP ); // non-copyable
94 
101  UDP& operator= ( const UDP& aUDP ); // non-assignable
102 
103  public:
105 
111  UDP ( const std::string& aId, const URI& aUri );
112 
116  virtual ~UDP();
117 
118  private:
124  void implementDispatch ( boost::shared_ptr< Buffers > aBuffers );
125 
129  virtual void Flush( );
130 
134  virtual void dispatchExceptionHandler();
135 
140  uint32_t getMaxSendSize();
141 
146  uint32_t getMaxReplySize();
147 
151  void connect();
157  void write ( );
163  void write_callback ( const boost::system::error_code& aErrorCode , std::size_t aBytesTransferred );
164 
170  void read ( );
171 
177  void read_callback ( const boost::system::error_code& aErrorCode , std::size_t aBytesTransferred );
178 
182  void CheckDeadline();
183 
188  void NotifyConditionalVariable ( const bool& aValue );
189 
193  void WaitOnConditionalVariable();
194 
195 
196  private:
198  boost::asio::io_service mIOservice;
199 
201  boost::asio::ip::udp::socket mSocket;
202 
204  boost::asio::ip::udp::endpoint mEndpoint;
205 
207  boost::asio::deadline_timer mDeadlineTimer;
208 
214  std::vector<uint8_t> mReplyMemory;
215 
217  boost::asio::io_service::work mIOserviceWork;
218 
220  boost::thread mDispatchThread;
221 
223  boost::mutex mTransportLayerMutex;
224 
226  std::deque < boost::shared_ptr< Buffers > > mDispatchQueue;
228  std::deque < boost::shared_ptr< Buffers > > mReplyQueue;
229 
232 
236  boost::condition_variable mConditionalVariable;
239 
244 
250 
251  };
252 
253 
254 }
255 
256 
257 #endif
std::deque< boost::shared_ptr< Buffers > > mDispatchQueue
The list of buffers still waiting to be sent.
Transport protocol to transfer an IPbus buffer via UDP.
Definition: ProtocolUDP.hpp:84
boost::asio::io_service mIOservice
The boost::asio::io_service used to create the connections.
uint32_t mPacketsInFlight
Counter of how many writes have been sent, for which no reply has yet been received.
boost::asio::deadline_timer mDeadlineTimer
The mechanism for providing the time-out.
#define UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(ClassName, BaseClassName, ClassDescription)
Macro for simplifying the declaration and definition of derived exception types.
Definition: exception.hpp:54
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 ...
An abstract base exception class providing an interface to a throw/ThrowAsDerivedType mechanism which...
Definition: exception.hpp:89
boost::shared_ptr< Buffers > mReplyBuffers
The receive operation currently in progress or the next to be done.
boost::shared_ptr< Buffers > mDispatchBuffers
The send operation currently in progress.
boost::mutex mTransportLayerMutex
A MutEx lock used to make sure the access functions are thread safe.
boost::condition_variable mConditionalVariable
A conditional variable for blocking the main thread until the variable with which it is associated is...
boost::asio::ip::udp::socket mSocket
A shared pointer to a boost::asio udp socket through which the operation will be performed.
boost::thread mDispatchThread
The Worker thread in Multi-threaded mode.
std::vector< uint8_t > mReplyMemory
A block of memory into which we write replies, before copying them to their final destination...
std::deque< boost::shared_ptr< Buffers > > mReplyQueue
The list of buffers still awaiting a reply.
bool mFlushDone
A variable associated with the conditional variable which specifies whether all packets have been sen...
boost::asio::ip::udp::endpoint mEndpoint
A shared pointer to a boost::asio udp endpoint - used in the ASIO send and receive functions (UDP has...
uhal::exception::exception * mAsynchronousException
A pointer to an exception object for passing exceptions from the worker thread to the main thread...
boost::mutex mConditionalVariableMutex
A mutex for use by the conditional variable.
c write(addr, xx[0])
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:49