6 #include <sys/socket.h>
12 #include "mutexlock.h"
13 #include "serverpool.h"
21 extern ServerPool *servers;
23 UDPInput::UDPInput(const string &url)
27 // Should be verified by the caller.
29 bool ok = parse_url(url, &protocol, &host, &port, &path);
34 pthread_mutex_init(&stats_mutex, NULL);
36 stats.bytes_received = 0;
37 stats.data_bytes_received = 0;
38 stats.connect_time = time(NULL);
41 UDPInput::UDPInput(const InputProto &serialized)
42 : url(serialized.url()),
43 sock(serialized.sock())
45 // Should be verified by the caller.
47 bool ok = parse_url(url, &protocol, &host, &port, &path);
52 pthread_mutex_init(&stats_mutex, NULL);
54 stats.bytes_received = serialized.bytes_received();
55 stats.data_bytes_received = serialized.data_bytes_received();
56 if (serialized.has_connect_time()) {
57 stats.connect_time = serialized.connect_time();
59 stats.connect_time = time(NULL);
63 InputProto UDPInput::serialize() const
65 InputProto serialized;
66 serialized.set_url(url);
67 serialized.set_sock(sock);
68 serialized.set_bytes_received(stats.bytes_received);
69 serialized.set_data_bytes_received(stats.data_bytes_received);
70 serialized.set_connect_time(stats.connect_time);
74 void UDPInput::close_socket()
80 void UDPInput::construct_header()
84 "Content-type: application/octet-stream\r\n"
85 "Cache-control: no-cache\r\n"
86 "Server: " SERVER_IDENTIFICATION "\r\n"
87 "Connection: close\r\n";
90 void UDPInput::add_destination(int stream_index)
92 stream_indices.push_back(stream_index);
93 servers->set_header(stream_index, http_header, "");
96 void UDPInput::do_work()
98 while (!should_stop()) {
100 int port_num = atoi(port.c_str());
101 sock = create_server_socket(port_num, UDP_SOCKET);
103 log(WARNING, "[%s] UDP socket creation failed. Waiting 0.2 seconds and trying again...",
110 // Wait for a packet, or a wakeup.
111 bool activity = wait_for_activity(sock, POLLIN, NULL);
113 // Most likely, should_stop was set.
119 ret = recv(sock, packet_buf, sizeof(packet_buf), 0);
120 } while (ret == -1 && errno == EINTR);
129 MutexLock lock(&stats_mutex);
130 stats.bytes_received += ret;
131 stats.data_bytes_received += ret;
134 for (size_t i = 0; i < stream_indices.size(); ++i) {
135 servers->add_data(stream_indices[i], packet_buf, ret, SUITABLE_FOR_STREAM_START);
140 InputStats UDPInput::get_stats() const
142 MutexLock lock(&stats_mutex);