]> git.sesse.net Git - cubemap/blob - serverpool.h
Merge branch 'master' of /srv/git.sesse.net/www/cubemap
[cubemap] / serverpool.h
1 #ifndef _SERVERPOOL_H
2 #define _SERVERPOOL_H 1
3
4 #include <stddef.h>
5 #include <string>
6 #include <vector>
7
8 #include "server.h"
9 #include "state.pb.h"
10 #include "stream.h"
11 #include "udpstream.h"
12
13 class MarkPool;
14 class Server;
15 struct ClientStats;
16
17 // Provides services such as load-balancing between a number of Server instances.
18 class ServerPool {
19 public:
20         ServerPool(int num_servers);
21         ~ServerPool();
22
23         // Fills streams() and clients().
24         CubemapStateProto serialize();
25
26         // Picks a server (round-robin) and allocates the given client to it.
27         void add_client(int sock);
28         void add_client_from_serialized(const ClientProto &client);
29
30         // Adds the given stream to all the servers. Returns the stream index.
31         int add_stream(const std::string &url, size_t backlog_size, Stream::Encoding encoding);
32         int add_stream_from_serialized(const StreamProto &stream, const std::vector<int> &data_fds);
33         int add_udpstream(const sockaddr_in6 &dst, MarkPool *mark_pool);
34
35         // Returns the stream index for the given URL (e.g. /foo.ts). Returns -1 on failure.
36         int lookup_stream_by_url(const std::string &url) const;
37
38         // Adds the given data to all the servers.
39         void set_header(int stream_index,
40                         const std::string &http_header,
41                         const std::string &stream_header);
42         void add_data(int stream_index, const char *data, size_t bytes);
43
44         // Connects the given stream to the given mark pool for all the servers.
45         void set_mark_pool(int stream_index, MarkPool *mark_pool);
46
47         // Changes the given stream's backlog size on all the servers.
48         void set_backlog_size(int stream_index, size_t new_size);
49
50         // Changes the given stream's encoding type on all the servers.
51         void set_encoding(int stream_index, Stream::Encoding encoding);
52
53         // Starts all the servers.
54         void run();
55
56         // Stops all the servers.
57         void stop();
58
59         std::vector<ClientStats> get_client_stats() const;
60
61 private:
62         Server *servers;
63         int num_servers, clients_added;
64
65         // Our indexing is currently rather primitive; every stream_index in
66         // [0, num_http_streams) maps to a HTTP stream (of which every Server
67         // has exactly one copy), and after that, it's mapping directly into
68         // <udp_streams>.
69         int num_http_streams;
70         std::vector<UDPStream *> udp_streams;
71
72         ServerPool(const ServerPool &);
73 };
74
75 #endif  // !defined(_SERVERPOOL_H)