]> git.sesse.net Git - cubemap/blobdiff - cubemap.cpp
Split Server and MutexLock out into separate source files.
[cubemap] / cubemap.cpp
index 643f1a8b0b6c7a0e1bc274a47ae65ebf7e18df76..49889f2e28bec81da0e90a51ab43ad8978f3e0c7 100644 (file)
 #include <vector>
 #include <string>
 #include <map>
+
 #include "metacube.h"
+#include "server.h"
 
 #define NUM_SERVERS 4
 #define STREAM_ID "stream"
 #define STREAM_URL "http://gruessi.zrh.sesse.net:4013/"
-#define BACKLOG_SIZE 1048576
 #define PORT 9094
 
 using namespace std;
 
-// Locks a pthread mutex, RAII-style.
-class MutexLock {
-public:
-       MutexLock(pthread_mutex_t *mutex);
-       ~MutexLock();
-
-private:
-       pthread_mutex_t *mutex;
-};
-       
-MutexLock::MutexLock(pthread_mutex_t *mutex)
-       : mutex(mutex)
-{
-       pthread_mutex_lock(mutex);
-}
-
-MutexLock::~MutexLock()
-{
-       pthread_mutex_unlock(mutex);
-}
-
-struct Client {
-       enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA };
-       State state;
-
-       // The HTTP request, as sent by the client. If we are in READING_REQUEST,
-       // this might not be finished.
-       string client_request;
-
-#if 0
-       // What stream we're connecting to; parsed from client_request.
-       // Not relevant for READING_REQUEST.
-       string stream_id;
-#endif
-
-       // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER.
-       size_t header_bytes_sent;
-
-       // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
-       size_t bytes_sent;
-};
-
-struct Stream {
-       // The HTTP response header, plus the video stream header (if any).
-       string header;
-
-       // The stream data itself, stored in a circular buffer.
-       char data[BACKLOG_SIZE];
-
-       // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
-       // since the buffer wraps.
-       size_t data_size;
-};
-
-class Server {
-public:
-       Server();
-
-       // Start a new thread that handles clients.
-       void run();
-       void add_client(int sock);
-       void add_stream(const string &stream_id);
-       void set_header(const string &stream_id, const string &header);
-       void add_data(const string &stream_id, const char *data, size_t bytes);
-
-private:
-       pthread_mutex_t mutex;
-
-       // Map from stream ID to stream.
-       map<string, Stream> streams;
-
-       // Map from file descriptor to client.
-       map<int, Client> clients;
-
-       int epoll_fd;
-
-       // Recover the this pointer, and call do_work().
-       static void *do_work_thunk(void *arg);
-
-       // The actual worker thread.
-       void do_work();
-};
-
 Server *servers = NULL;
 
-Server::Server()
-{
-       pthread_mutex_init(&mutex, NULL);
-
-       epoll_fd = epoll_create(1024);  // Size argument is ignored.
-       if (epoll_fd == -1) {
-               perror("epoll_fd");
-               exit(1);
-       }
-}
-
-void Server::run()
-{
-       pthread_t thread;
-       pthread_create(&thread, NULL, Server::do_work_thunk, this);
-}
-
-void *Server::do_work_thunk(void *arg)
-{
-       Server *server = static_cast<Server *>(arg);
-       server->do_work();
-       return NULL;
-}
-
-void Server::do_work()
-{
-       for ( ;; ) {
-               MutexLock lock(&mutex);
-               printf("server thread running\n");
-               sleep(1);
-       }
-}
-       
-void Server::add_client(int sock)
-{
-       MutexLock lock(&mutex);
-       Client new_client;
-       new_client.state = Client::READING_REQUEST;
-       new_client.header_bytes_sent = 0;
-       new_client.bytes_sent = 0;
-
-       clients.insert(make_pair(sock, new_client));
-
-       // Start listening on data from this socket.
-       epoll_event ev;
-       ev.events = EPOLLIN;
-       ev.data.fd = sock;
-       if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
-               perror("epoll_ctl(EPOLL_CTL_ADD)");
-               exit(1);
-       }
-}
-       
-void Server::add_stream(const string &stream_id)
-{
-       // TODO
-}
-       
-void Server::set_header(const string &stream_id, const string &header)
-{
-       // TODO
-       printf("got header! %lu bytes\n", header.size());
-}
-       
-void Server::add_data(const string &stream_id, const char *data, size_t bytes)
-{
-       // TODO
-}
-
 class Input {
 public:
        Input(const string &stream_id);