gr-baz Package
baz_udp_sink.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2007,2008,2009,2010,2013 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #if (!defined(INCLUDED_GR_UDP_SINK_H) && !defined(IN_GR_BAZ)) || (!defined(INCLUDED_BAZ_UDP_SINK_H) && defined(IN_GR_BAZ))
00024 #ifdef IN_GR_BAZ
00025 #define INCLUDED_BAZ_UDP_SINK_H
00026 #else
00027 #define INCLUDED_GR_UDP_SINK_H
00028 #endif // IN_GR_BAZ
00029 
00030 #include <gr_sync_block.h>
00031 #include <gruel/thread.h>
00032 
00033 #ifdef IN_GR_BAZ
00034 #define UDP_SINK_NAME   baz_udp_sink
00035 #define UDP_SINK_MAKER  baz_make_udp_sink
00036 #define UDP_SINK_SPTR   baz_udp_sink_sptr
00037 #else
00038 #define UDP_SINK_NAME   gr_udp_sink
00039 #define UDP_SINK_MAKER  gr_make_udp_sink
00040 #define UDP_SINK_SPTR   gr_udp_sink_sptr
00041 #endif // IN_GR_BAZ
00042 
00043 #ifndef _TO_STR
00044 #define _TO_STR(x)        #x
00045 #endif // _TO_STR
00046 #define UDP_SINK_STRING _TO_STR(UDP_SINK_NAME)
00047 
00048 #include <gr_sync_block.h>
00049 #include <gr_msg_queue.h>
00050 #include <gruel/thread.h>
00051 
00052 class BAZ_API UDP_SINK_NAME;
00053 typedef boost::shared_ptr<UDP_SINK_NAME> UDP_SINK_SPTR;
00054 
00055 BAZ_API UDP_SINK_SPTR
00056 UDP_SINK_MAKER (size_t itemsize, 
00057                   const char *host, unsigned short port,
00058                   int payload_size=1472, bool eof=true, bool bor=false);
00059 
00060 /*!
00061  * \brief Write stream to an UDP socket.
00062  * \ingroup sink_blk
00063  * 
00064  * \param itemsize     The size (in bytes) of the item datatype
00065  * \param host         The name or IP address of the receiving host; use
00066  *                     NULL or None for no connection
00067  * \param port         Destination port to connect to on receiving host
00068  * \param payload_size UDP payload size by default set to 1472 =
00069  *                     (1500 MTU - (8 byte UDP header) - (20 byte IP header))
00070  * \param eof          Send zero-length packet on disconnect
00071  */
00072 
00073 class BAZ_API UDP_SINK_NAME : public gr_sync_block
00074 {
00075   friend BAZ_API UDP_SINK_SPTR UDP_SINK_MAKER (size_t itemsize, 
00076                                             const char *host,
00077                                             unsigned short port,
00078                                             int payload_size, bool eof, bool bor);
00079  private:
00080   size_t        d_itemsize;
00081 
00082   int           d_payload_size;    // maximum transmission unit (packet length)
00083   bool          d_eof;             // send zero-length packet on disconnect
00084   int           d_socket;          // handle to socket
00085   bool          d_connected;       // are we connected?
00086   gruel::mutex  d_mutex;           // protects d_socket and d_connected
00087   bool          d_bor;
00088   unsigned short d_bor_counter;
00089   bool          d_bor_first;
00090   unsigned char* d_bor_packet;
00091   int           d_residual;
00092   int           d_offset;
00093   int           d_data_length;
00094   gr_msg_queue_sptr d_status_queue;
00095 
00096  protected:
00097   /*!
00098    * \brief UDP Sink Constructor
00099    * 
00100    * \param itemsize     The size (in bytes) of the item datatype
00101    * \param host         The name or IP address of the receiving host; use
00102    *                     NULL or None for no connection
00103    * \param port         Destination port to connect to on receiving host
00104    * \param payload_size UDP payload size by default set to 
00105    *                     1472 = (1500 MTU - (8 byte UDP header) - (20 byte IP header))
00106    * \param eof          Send zero-length packet on disconnect
00107    * \param bor          Enable BorIP encapsulation
00108    */
00109   UDP_SINK_NAME (size_t itemsize, 
00110                const char *host, unsigned short port,
00111                int payload_size, bool eof, bool bor);
00112   
00113   bool create();
00114   void allocate();
00115   void destroy();
00116 
00117  public:
00118   ~UDP_SINK_NAME ();
00119 
00120   /*! \brief return the PAYLOAD_SIZE of the socket */
00121   int payload_size() { return d_payload_size; }
00122 
00123   /*! \brief Change the connection to a new destination
00124    *
00125    * \param host         The name or IP address of the receiving host; use
00126    *                     NULL or None to break the connection without closing
00127    * \param port         Destination port to connect to on receiving host
00128    *
00129    * Calls disconnect() to terminate any current connection first.
00130    */
00131   void connect( const char *host, unsigned short port );
00132 
00133   /*! \brief Send zero-length packet (if eof is requested) then stop sending
00134    *
00135    * Zero-byte packets can be interpreted as EOF by gr_udp_source.  Note that
00136    * disconnect occurs automatically when the sink is destroyed, but not when
00137    * its top_block stops.*/
00138   void disconnect();
00139   
00140   void set_borip(bool enable);
00141   void set_payload_size(int payload_size);
00142   void set_status_msgq(gr_msg_queue_sptr queue);
00143 
00144   int work (int noutput_items,
00145             gr_vector_const_void_star &input_items,
00146             gr_vector_void_star &output_items);
00147 };
00148 
00149 #endif /* INCLUDED_GR_UDP_SINK_H */