Split Input into its own file.
[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 "input.h"
19 #include "server.h"
20
21 using namespace std;
22
23 extern Server *servers;
24
25 Input::Input(const string &stream_id)
26         : stream_id(stream_id),
27           has_metacube_header(false)
28 {
29 }
30
31 void Input::run(const string &url)
32 {
33         CURL *curl = curl_easy_init();
34         curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
35         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Input::curl_callback_thunk);
36         curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
37         curl_easy_perform(curl);
38 }
39
40 size_t Input::curl_callback_thunk(char *ptr, size_t size, size_t nmemb, void *userdata)
41 {
42         Input *input = static_cast<Input *>(userdata);
43         size_t bytes = size * nmemb;
44         input->curl_callback(ptr, bytes);       
45         return bytes;
46 }
47         
48 void Input::curl_callback(char *ptr, size_t bytes)
49 {
50         pending_data.insert(pending_data.end(), ptr, ptr + bytes);
51
52         for ( ;; ) {
53                 // If we don't have enough data (yet) for even the Metacube header, just return.
54                 if (pending_data.size() < sizeof(metacube_block_header)) {
55                         return;
56                 }
57
58                 // Make sure we have the Metacube sync header at the start.
59                 // We may need to skip over junk data (it _should_ not happen, though).
60                 if (!has_metacube_header) {
61                         char *ptr = static_cast<char *>(
62                                 memmem(pending_data.data(), pending_data.size(),
63                                        METACUBE_SYNC, strlen(METACUBE_SYNC)));
64                         if (ptr == NULL) {
65                                 // OK, so we didn't find the sync marker. We know then that
66                                 // we do not have the _full_ marker in the buffer, but we
67                                 // could have N-1 bytes. Drop everything before that,
68                                 // and then give up.
69                                 drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
70                                 return;
71                         } else {
72                                 // Yay, we found the header. Drop everything (if anything) before it.
73                                 drop_pending_data(ptr - pending_data.data());
74                                 has_metacube_header = true;
75
76                                 // Re-check that we have the entire header; we could have dropped data.
77                                 if (pending_data.size() < sizeof(metacube_block_header)) {
78                                         return;
79                                 }
80                         }
81                 }
82
83                 // Now it's safe to read the header.
84                 metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());    
85                 assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
86                 uint32_t size = ntohl(hdr->size);
87                 uint32_t flags = ntohl(hdr->flags);
88
89                 // See if we have the entire block. If not, wait for more data.
90                 if (pending_data.size() < sizeof(metacube_block_header) + size) {
91                         return;
92                 }
93
94                 process_block(pending_data.data(), size, flags);
95
96                 // Consume this block. This isn't the most efficient way of dealing with things
97                 // should we have many blocks, but these routines don't need to be too efficient
98                 // anyway.
99                 pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
100         }
101 }
102                 
103 void Input::process_block(const char *data, uint32_t size, uint32_t flags)
104 {       
105         if (flags & METACUBE_FLAGS_HEADER) {
106                 string header(data, data + size);
107                 for (int i = 0; i < NUM_SERVERS; ++i) {
108                         servers[i].set_header(stream_id, header);
109                 }
110         } else { 
111                 for (int i = 0; i < NUM_SERVERS; ++i) {
112                         servers[i].add_data(stream_id, data, size);
113                 }
114         }
115 }
116
117 void Input::drop_pending_data(size_t num_bytes)
118 {
119         if (num_bytes == 0) {
120                 return;
121         }
122         fprintf(stderr, "Warning: Dropping %lld junk bytes from stream, maybe it is not a Metacube stream?\n",
123                 (long long)num_bytes);
124         pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);
125 }
126