Split Server and MutexLock out into separate source files.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Apr 2013 14:05:47 +0000 (16:05 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Apr 2013 14:05:47 +0000 (16:05 +0200)
Makefile
cubemap.cpp
mutexlock.cpp [new file with mode: 0644]
mutexlock.h [new file with mode: 0644]
server.cpp [new file with mode: 0644]
server.h [new file with mode: 0644]

index 8d26f49..c80ab9d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ CXX=g++
 CXXFLAGS=-Wall -O2 -g
 LDLIBS=-lcurl
 
-OBJS=cubemap.o
+OBJS=cubemap.o server.o mutexlock.o
 
 all: cubemap
 
index 643f1a8..49889f2 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);
diff --git a/mutexlock.cpp b/mutexlock.cpp
new file mode 100644 (file)
index 0000000..dbd5ae5
--- /dev/null
@@ -0,0 +1,13 @@
+#include "mutexlock.h"
+
+MutexLock::MutexLock(pthread_mutex_t *mutex)
+       : mutex(mutex)
+{
+       pthread_mutex_lock(mutex);
+}
+
+MutexLock::~MutexLock()
+{
+       pthread_mutex_unlock(mutex);
+}
+
diff --git a/mutexlock.h b/mutexlock.h
new file mode 100644 (file)
index 0000000..0d1bc00
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef _MUTEXLOCK_H
+#define _MUTEXLOCK_H 1
+
+#include <pthread.h>
+
+// Locks a pthread mutex, RAII-style.
+class MutexLock {
+public:
+       MutexLock(pthread_mutex_t *mutex);
+       ~MutexLock();
+
+private:
+       pthread_mutex_t *mutex;
+};
+
+#endif  // !defined(_MUTEXLOCK_H)
diff --git a/server.cpp b/server.cpp
new file mode 100644 (file)
index 0000000..31e5c0a
--- /dev/null
@@ -0,0 +1,90 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <assert.h>
+#include <arpa/inet.h>
+#include <curl/curl.h>
+#include <sys/socket.h>
+#include <pthread.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/epoll.h>
+#include <errno.h>
+#include <vector>
+#include <string>
+#include <map>
+
+#include "metacube.h"
+#include "server.h"
+#include "mutexlock.h"
+
+using namespace std;
+
+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
+}
diff --git a/server.h b/server.h
new file mode 100644 (file)
index 0000000..d8b9064
--- /dev/null
+++ b/server.h
@@ -0,0 +1,73 @@
+#ifndef _SERVER_H
+#define _SERVER_H 1
+
+#include <stdint.h>
+#include <pthread.h>
+#include <string>
+#include <map>
+
+#define BACKLOG_SIZE 1048576
+
+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.
+       std::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).
+       std::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 std::string &stream_id);
+       void set_header(const std::string &stream_id, const std::string &header);
+       void add_data(const std::string &stream_id, const char *data, size_t bytes);
+
+private:
+       pthread_mutex_t mutex;
+
+       // Map from stream ID to stream.
+       std::map<std::string, Stream> streams;
+
+       // Map from file descriptor to client.
+       std::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();
+};
+
+#endif  // !defined(_SERVER_H)