Serialize and die on SIGHUP (no deserialization yet). Required a whole bunch of Valgr...
[cubemap] / input.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdint.h>
4 #include <assert.h>
5 #include <arpa/inet.h>
6 #include <curl/curl.h>
7 #include <sys/socket.h>
8 #include <pthread.h>
9 #include <sys/types.h>
10 #include <sys/ioctl.h>
11 #include <sys/epoll.h>
12 #include <errno.h>
13 #include <vector>
14 #include <string>
15 #include <map>
16
17 #include "metacube.h"
18 #include "mutexlock.h"
19 #include "input.h"
20 #include "server.h"
21
22 using namespace std;
23
24 extern Server *servers;
25
26 Input::Input(const string &stream_id, const string &url)
27         : stream_id(stream_id),
28           url(url),
29           has_metacube_header(false)
30 {
31 }
32
33 void Input::run()
34 {
35         should_stop = false;
36         
37         // Joinable is already the default, but it's good to be certain.
38         pthread_attr_t attr;
39         pthread_attr_init(&attr);
40         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
41         pthread_create(&worker_thread, &attr, Input::do_work_thunk, this);
42 }
43
44 void Input::stop()
45 {
46         should_stop = true;
47
48         if (pthread_join(worker_thread, NULL) == -1) {
49                 perror("pthread_join");
50                 exit(1);
51         }
52 }
53
54 void *Input::do_work_thunk(void *arg)
55 {
56         Input *input = static_cast<Input *>(arg);
57         input->do_work();
58         return NULL;
59 }
60
61 void Input::do_work()
62 {
63         CURL *curl = curl_easy_init();
64         curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
65         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Input::curl_callback_thunk);
66         curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
67         curl_easy_perform(curl);
68 }
69
70 size_t Input::curl_callback_thunk(char *ptr, size_t size, size_t nmemb, void *userdata)
71 {
72         Input *input = static_cast<Input *>(userdata);
73         if (input->should_stop) {
74                 return 0;
75         }
76
77         size_t bytes = size * nmemb;
78         input->curl_callback(ptr, bytes);       
79         return bytes;
80 }
81         
82 void Input::curl_callback(char *ptr, size_t bytes)
83 {
84         pending_data.insert(pending_data.end(), ptr, ptr + bytes);
85
86         for ( ;; ) {
87                 // If we don't have enough data (yet) for even the Metacube header, just return.
88                 if (pending_data.size() < sizeof(metacube_block_header)) {
89                         return;
90                 }
91
92                 // Make sure we have the Metacube sync header at the start.
93                 // We may need to skip over junk data (it _should_ not happen, though).
94                 if (!has_metacube_header) {
95                         char *ptr = static_cast<char *>(
96                                 memmem(pending_data.data(), pending_data.size(),
97                                        METACUBE_SYNC, strlen(METACUBE_SYNC)));
98                         if (ptr == NULL) {
99                                 // OK, so we didn't find the sync marker. We know then that
100                                 // we do not have the _full_ marker in the buffer, but we
101                                 // could have N-1 bytes. Drop everything before that,
102                                 // and then give up.
103                                 drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
104                                 return;
105                         } else {
106                                 // Yay, we found the header. Drop everything (if anything) before it.
107                                 drop_pending_data(ptr - pending_data.data());
108                                 has_metacube_header = true;
109
110                                 // Re-check that we have the entire header; we could have dropped data.
111                                 if (pending_data.size() < sizeof(metacube_block_header)) {
112                                         return;
113                                 }
114                         }
115                 }
116
117                 // Now it's safe to read the header.
118                 metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());    
119                 assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
120                 uint32_t size = ntohl(hdr->size);
121                 uint32_t flags = ntohl(hdr->flags);
122
123                 // See if we have the entire block. If not, wait for more data.
124                 if (pending_data.size() < sizeof(metacube_block_header) + size) {
125                         return;
126                 }
127
128                 process_block(pending_data.data() + sizeof(metacube_block_header), size, flags);
129
130                 // Consume this block. This isn't the most efficient way of dealing with things
131                 // should we have many blocks, but these routines don't need to be too efficient
132                 // anyway.
133                 pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
134         }
135 }
136                 
137 void Input::process_block(const char *data, uint32_t size, uint32_t flags)
138 {       
139         if (flags & METACUBE_FLAGS_HEADER) {
140                 string header(data, data + size);
141                 for (int i = 0; i < NUM_SERVERS; ++i) {
142                         servers[i].set_header(stream_id, header);
143                 }
144         } else { 
145                 for (int i = 0; i < NUM_SERVERS; ++i) {
146                         servers[i].add_data(stream_id, data, size);
147                 }
148         }
149 }
150
151 void Input::drop_pending_data(size_t num_bytes)
152 {
153         if (num_bytes == 0) {
154                 return;
155         }
156         fprintf(stderr, "Warning: Dropping %lld junk bytes from stream, maybe it is not a Metacube stream?\n",
157                 (long long)num_bytes);
158         pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);
159 }
160