libvrt: remove from 3.3 API.
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_udp_sink.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_sink.h>
27 #include <gr_io_signature.h>
28 #include <stdexcept>
29 #if defined(HAVE_SOCKET)
30 #include <netdb.h>
31 #include <stdio.h>
32 typedef void* optval_t;
33 #else
34 #define SHUT_RDWR 2
35 #define inet_aton(N,A) ( (A)->s_addr = inet_addr(N), ( (A)->s_addr != INADDR_NONE ) )
36 typedef char* optval_t;
37 #endif
38
39 #include <gruel/thread.h>
40
41 #define SNK_VERBOSE 0
42
43 gr_udp_sink::gr_udp_sink (size_t itemsize, 
44                           const char *src, unsigned short port_src,
45                           const char *dst, unsigned short port_dst,
46                           int payload_size)
47   : gr_sync_block ("udp_sink",
48                    gr_make_io_signature (1, 1, itemsize),
49                    gr_make_io_signature (0, 0, 0)),
50     d_itemsize (itemsize), d_updated(false), d_payload_size(payload_size)
51 {
52   int ret = 0;
53   
54   // Set up the address stucture for the source address and port numbers
55   // Get the source IP address from the host name
56   struct hostent *hsrc = gethostbyname(src);
57   if(hsrc) {   // if the source was provided as a host namex
58     d_ip_src = *(struct in_addr*)hsrc->h_addr_list[0];    
59   }
60   else { // assume it was specified as an IP address
61     if((ret=inet_aton(src, &d_ip_src)) == 0) {            // format IP address
62       perror("Not a valid source IP address or host name");
63       throw std::runtime_error("can't initialize source socket");
64     }
65   }
66
67   // Get the destination IP address from the host name
68   struct hostent *hdst = gethostbyname(dst);
69   if(hdst) {   // if the source was provided as a host namex
70     d_ip_dst = *(struct in_addr*)hdst->h_addr_list[0];    
71   }
72   else { // assume it was specified as an IP address
73     if((ret=inet_aton(dst, &d_ip_dst)) == 0) {            // format IP address
74       perror("Not a valid destination IP address or host name");
75       throw std::runtime_error("can't initialize destination socket");
76     }
77   }
78
79   d_port_src = htons(port_src);           // format port number
80   d_port_dst = htons(port_dst);           // format port number
81
82   d_sockaddr_src.sin_family = AF_INET;
83   d_sockaddr_src.sin_addr   = d_ip_src;
84   d_sockaddr_src.sin_port   = d_port_src;
85
86   d_sockaddr_dst.sin_family = AF_INET;
87   d_sockaddr_dst.sin_addr   = d_ip_dst;
88   d_sockaddr_dst.sin_port   = d_port_dst;
89   
90   open();
91 }
92
93 // public constructor that returns a shared_ptr
94
95 gr_udp_sink_sptr
96 gr_make_udp_sink (size_t itemsize, 
97                   const char *src, unsigned short port_src,
98                   const char *dst, unsigned short port_dst,
99                   int payload_size)
100 {
101   return gr_udp_sink_sptr (new gr_udp_sink (itemsize, 
102                                             src, port_src,
103                                             dst, port_dst,
104                                             payload_size));
105 }
106
107 gr_udp_sink::~gr_udp_sink ()
108 {
109   close();
110 }
111
112 bool
113 gr_udp_sink::open()
114 {
115   gruel::scoped_lock guard(d_mutex);    // hold mutex for duration of this function
116
117   // create socket
118   if((d_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
119     perror("socket open");
120     throw std::runtime_error("can't open socket");
121   }
122
123   // Turn on reuse address
124   int opt_val = true;
125   if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (optval_t)&opt_val, sizeof(int)) == -1) {
126     perror("SO_REUSEADDR");
127     throw std::runtime_error("can't set socket option SO_REUSEADDR");
128   }
129
130   // Don't wait when shutting down
131   linger lngr;
132   lngr.l_onoff  = 1;
133   lngr.l_linger = 0;
134   if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (optval_t)&lngr, sizeof(linger)) == -1) {
135     perror("SO_LINGER");
136     throw std::runtime_error("can't set socket option SO_LINGER");
137   }
138
139   // bind socket to an address and port number to listen on
140   if(bind (d_socket, (sockaddr*)&d_sockaddr_src, sizeof(struct sockaddr)) == -1) {
141     perror("socket bind");
142     throw std::runtime_error("can't bind socket");
143   }
144
145   // Not sure if we should throw here or allow retries
146   if(connect(d_socket, (sockaddr*)&d_sockaddr_dst, sizeof(struct sockaddr)) == -1) {
147     perror("socket connect");
148     throw std::runtime_error("can't connect to socket");
149   }
150
151   d_updated = true;
152   return d_socket != 0;
153 }
154
155 void
156 gr_udp_sink::close()
157 {
158   gruel::scoped_lock guard(d_mutex);    // hold mutex for duration of this function
159
160   if (d_socket){
161     shutdown(d_socket, SHUT_RDWR);
162     d_socket = 0;
163   }
164   d_updated = true;
165 }
166
167 int 
168 gr_udp_sink::work (int noutput_items,
169                    gr_vector_const_void_star &input_items,
170                    gr_vector_void_star &output_items)
171 {
172   const char *in = (const char *) input_items[0];
173   ssize_t r=0, bytes_sent=0, bytes_to_send=0;
174   ssize_t total_size = noutput_items*d_itemsize;
175
176   #if SNK_VERBOSE
177   printf("Entered upd_sink\n");
178   #endif
179
180   while(bytes_sent <  total_size) {
181     bytes_to_send = std::min((ssize_t)d_payload_size, (total_size-bytes_sent));
182   
183     r = send(d_socket, (in+bytes_sent), bytes_to_send, 0);
184     if(r == -1) {         // error on send command
185       perror("udp_sink"); // there should be no error case where this function 
186       return -1;          // should not exit immediately
187     }
188     bytes_sent += r;
189     
190     #if SNK_VERBOSE
191     printf("\tbyte sent: %d bytes\n", bytes);
192     #endif
193   }
194
195   #if SNK_VERBOSE
196   printf("Sent: %d bytes (noutput_items: %d)\n", bytes_sent, noutput_items);
197   #endif
198
199   return noutput_items;
200 }