libvrt: remove from 3.3 API.
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_udp_source.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <gr_udp_source.h>
27 #include <gr_io_signature.h>
28 #include <stdexcept>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #if defined(HAVE_SOCKET)
33 #include <netdb.h>
34 typedef void* optval_t;
35 #else
36 #define SHUT_RDWR 2
37 #define inet_aton(N,A) ( (A)->s_addr = inet_addr(N), ( (A)->s_addr != INADDR_NONE ) )
38 typedef char* optval_t;
39 #endif
40
41 #define SRC_VERBOSE 0
42
43 gr_udp_source::gr_udp_source(size_t itemsize, const char *src, 
44                              unsigned short port_src, int payload_size)
45   : gr_sync_block ("udp_source",
46                    gr_make_io_signature(0, 0, 0),
47                    gr_make_io_signature(1, 1, itemsize)),
48     d_itemsize(itemsize), d_updated(false), d_payload_size(payload_size), d_residual(0), d_temp_offset(0)
49 {
50   int ret = 0;
51   
52   // Set up the address stucture for the source address and port numbers
53   // Get the source IP address from the host name
54   struct hostent *hsrc = gethostbyname(src);
55   if(hsrc) {   // if the source was provided as a host namex
56     d_ip_src = *(struct in_addr*)hsrc->h_addr_list[0];    
57   }
58   else { // assume it was specified as an IP address
59     if((ret=inet_aton(src, &d_ip_src)) == 0) {            // format IP address
60       perror("Not a valid source IP address or host name");
61       throw std::runtime_error("can't initialize source socket");
62     }
63   }
64
65   d_port_src = htons(port_src);     // format port number
66   
67   d_sockaddr_src.sin_family = AF_INET;
68   d_sockaddr_src.sin_addr   = d_ip_src;
69   d_sockaddr_src.sin_port   = d_port_src;
70
71   d_temp_buff = new char[d_payload_size];   // allow it to hold up to payload_size bytes
72   
73   open();
74 }
75
76 gr_udp_source_sptr
77 gr_make_udp_source (size_t itemsize, const char *ipaddr, 
78                     unsigned short port, int payload_size)
79 {
80   return gr_udp_source_sptr (new gr_udp_source (itemsize, ipaddr, 
81                                                 port, payload_size));
82 }
83
84 gr_udp_source::~gr_udp_source ()
85 {
86   delete [] d_temp_buff;
87   close();
88 }
89
90 bool
91 gr_udp_source::open()
92 {
93   gruel::scoped_lock guard(d_mutex);    // hold mutex for duration of this function
94   // create socket
95   d_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
96   if(d_socket == -1) {
97     perror("socket open");
98     throw std::runtime_error("can't open socket");
99   }
100
101   // Turn on reuse address
102   int opt_val = 1;
103   if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (optval_t)&opt_val, sizeof(int)) == -1) {
104     perror("SO_REUSEADDR");
105     throw std::runtime_error("can't set socket option SO_REUSEADDR");
106   }
107
108   // Don't wait when shutting down
109   linger lngr;
110   lngr.l_onoff  = 1;
111   lngr.l_linger = 0;
112   if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (optval_t)&lngr, sizeof(linger)) == -1) {
113     perror("SO_LINGER");
114     throw std::runtime_error("can't set socket option SO_LINGER");
115   }
116
117   // Set a timeout on the receive function to not block indefinitely
118   // This value can (and probably should) be changed
119   timeval timeout;
120   timeout.tv_sec = 1;
121   timeout.tv_usec = 0;
122   if(setsockopt(d_socket, SOL_SOCKET, SO_RCVTIMEO, (optval_t)&timeout, sizeof(timeout)) == -1) {
123     perror("SO_RCVTIMEO");
124     throw std::runtime_error("can't set socket option SO_RCVTIMEO");
125   }
126
127   // bind socket to an address and port number to listen on
128   if(bind (d_socket, (sockaddr*)&d_sockaddr_src, sizeof(struct sockaddr)) == -1) {
129     perror("socket bind");
130     throw std::runtime_error("can't bind socket");
131   }
132   
133   d_updated = true;
134   return d_socket != 0;
135 }
136
137 void
138 gr_udp_source::close()
139 {
140   gruel::scoped_lock guard(d_mutex);    // hold mutex for duration of this function
141
142   if (d_socket){
143     shutdown(d_socket, SHUT_RDWR);
144     d_socket = 0;
145   }
146   d_updated = true;
147 }
148
149 int 
150 gr_udp_source::work (int noutput_items,
151                      gr_vector_const_void_star &input_items,
152                      gr_vector_void_star &output_items)
153 {
154   char *out = (char *) output_items[0];
155   ssize_t r=0, nbytes=0, bytes_received=0;
156   ssize_t total_bytes = (ssize_t)(d_itemsize*noutput_items);
157
158   #if SRC_VERBOSE
159   printf("\nEntered udp_source\n");
160   #endif
161
162   // Remove items from temp buffer if they are in there
163   if(d_residual) {
164     nbytes = std::min(d_residual, total_bytes);
165     memcpy(out, d_temp_buff+d_temp_offset, nbytes);
166     bytes_received = nbytes;
167
168     #if SRC_VERBOSE
169     printf("\tTemp buff size: %d  offset: %d (bytes_received: %d) (noutput_items: %d)\n", 
170            d_residual, d_temp_offset, bytes_received, noutput_items);
171     #endif
172
173     // Increment pointer
174     out += bytes_received;
175     
176     // Update indexing of amount of bytes left in the buffer
177     d_residual -= nbytes;
178     d_temp_offset = d_temp_offset+d_residual;
179   }
180
181   while(1) {
182     // get the data into our output buffer and record the number of bytes
183     // This is a non-blocking call with a timeout set in the constructor
184     r = recv(d_socket, d_temp_buff, d_payload_size, 0);  // get the entire payload or the what's available
185
186     // Check if there was a problem; forget it if the operation just timed out
187     if(r == -1) {
188       if(errno == EAGAIN) {  // handle non-blocking call timeout
189         #if SRC_VERBOSE
190         printf("UDP receive timed out\n"); 
191         #endif
192
193         // Break here to allow the rest of the flow graph time to run and so ctrl-C breaks
194         break;
195       }
196       else {
197         perror("udp_source");
198         return -1;
199       }
200     }
201     else {
202       // Calculate the number of bytes we can take from the buffer in this call
203       nbytes = std::min(r, total_bytes-bytes_received);
204       
205       // adjust the total number of bytes we have to round down to nearest integer of an itemsize
206       nbytes -= ((bytes_received+nbytes) % d_itemsize);   
207
208       // copy the number of bytes we want to look at here
209       memcpy(out, d_temp_buff, nbytes);    
210
211       d_residual = r - nbytes;                      // save the number of bytes stored
212       d_temp_offset=nbytes;                         // reset buffer index
213
214       // keep track of the total number of bytes received
215       bytes_received += nbytes;
216
217       // increment the pointer
218       out += nbytes;
219
220       // Immediately return when data comes in
221       break;
222     }
223
224     #if SNK_VERBOSE
225     printf("\tbytes received: %d bytes (nbytes: %d)\n", bytes, nbytes);
226     #endif
227   }
228
229   #if SRC_VERBOSE
230   printf("Total Bytes Received: %d (bytes_received / noutput_items = %d / %d)\n", 
231          bytes_received, bytes_received, noutput_items);
232   #endif
233
234   // bytes_received is already set to some integer multiple of itemsize
235   return bytes_received/d_itemsize;
236 }
237