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