]> git.sesse.net Git - cubemap/blob - input.h
28bc14b09b309f4dc034ff665136513c75864d8d
[cubemap] / input.h
1 #ifndef _INPUT_H
2 #define _INPUT_H 1
3
4 #include <vector>
5 #include <string>
6
7 class InputProto;
8
9 class Input {
10 public:
11         Input(const std::string &stream_id, const std::string &url);
12
13         // Serialization/deserialization.
14         Input(const InputProto &serialized);
15         InputProto serialize() const;
16
17         // Connect to the given URL and start streaming.
18         void run();
19
20         // Stops the streaming, but lets the file descriptor stay open.
21         void stop();
22
23         std::string get_url() const { return url; }
24
25 private:
26         // Recovers the this pointer and calls do_work().
27         static void *do_work_thunk(void *arg);
28
29         // Actually does the download.
30         void do_work();
31         
32         // Open a socket that connects to the given host and port. Does DNS resolving.
33         int lookup_and_connect(const std::string &host, const std::string &port);
34
35         // Parses a HTTP response. Returns false if it not a 200.
36         bool parse_response(const std::string &response);
37
38         // Stores the given data, looks for Metacube blocks (skipping data if needed),
39         // and calls process_block() for each one.
40         void process_data(char *ptr, size_t bytes);
41
42         // Drops <num_bytes> bytes from the head of <pending_data>,
43         // and outputs a warning.
44         void drop_pending_data(size_t num_bytes);
45
46         enum State {
47                 NOT_CONNECTED,
48                 SENDING_REQUEST,
49                 RECEIVING_HEADER,
50                 RECEIVING_DATA,
51                 CLOSING_SOCKET,  // Due to error.
52         };
53         State state;
54
55         std::string stream_id;
56
57         // The URL and its parsed components.
58         std::string url;
59         std::string host, port, path;
60
61         // The HTTP request, with headers and all.
62         // Only relevant for SENDING_REQUEST.
63         std::string request;
64
65         // How many bytes we've sent of the request so far.
66         // Only relevant for SENDING_REQUEST.
67         size_t request_bytes_sent;
68
69         // The HTTP response we've received so far. Only relevant for RECEIVING_HEADER.
70         std::string response;
71
72         // The HTTP respones headers we want to give clients for this input.
73         std::string http_header;
74
75         // Data we have received but not fully processed yet.
76         std::vector<char> pending_data;
77
78         // If <pending_data> starts with a Metacube header,
79         // this is true.
80         bool has_metacube_header;
81
82         // The socket we are downloading on (or -1).
83         int sock;       
84
85         // Handle to the thread that actually does the download.
86         pthread_t worker_thread;
87
88         // Whether we should stop or not.
89         volatile bool should_stop;
90 };
91
92 #endif  // !defined(_INPUT_H)