diff --git a/server/application.cpp b/server/application.cpp deleted file mode 100644 index c20c99d..0000000 --- a/server/application.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "application.hpp" - -Application::Application() { - //TODO -} - -Application::~Application() { - //TODO -} - -void Application::Init() { - //TODO - - //disable this for debugging - running = false; -} - -void Application::Loop() { - while(running) { - //TODO - } -} - -void Application::Quit() { - //TODO -} - -void Application::NewRoom() { - //TODO -} diff --git a/server/application.hpp b/server/application.hpp deleted file mode 100644 index 3512973..0000000 --- a/server/application.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef APPLICATION_HPP_ -#define APPLICATION_HPP_ - -#include "threading.hpp" - -#include - -class Application { -public: - Application(); - ~Application(); - - void Init(); - void Loop(); - void Quit(); - - bool GetRunning() const { return running; } -private: - void NewRoom(/* args */); - - std::vector rooms; - bool running = true; -}; - -#endif diff --git a/server/threading.cpp b/server/base_room.cpp similarity index 52% rename from server/threading.cpp rename to server/base_room.cpp index 0b83b9a..3773d90 100644 --- a/server/threading.cpp +++ b/server/base_room.cpp @@ -1,16 +1,23 @@ -#include "threading.hpp" +#include "base_room.hpp" + +#include "SDL/SDL_thread.h" -#include #include +BaseRoom::BaseRoom(std::map args): + arguments(args) +{ + // +} + int roomThread(void* ptr) { #ifdef DEBUG std::cout << "Opening room" << std::endl; #endif try { - reinterpret_cast(ptr)->Init(); - reinterpret_cast(ptr)->Loop(); - reinterpret_cast(ptr)->Quit(); + reinterpret_cast(ptr)->Init(); + reinterpret_cast(ptr)->Loop(); + reinterpret_cast(ptr)->Quit(); } catch(std::exception& e) { std::cerr << "Fatal room error: " << e.what() << std::endl; diff --git a/server/base_room.hpp b/server/base_room.hpp new file mode 100644 index 0000000..c59a9d6 --- /dev/null +++ b/server/base_room.hpp @@ -0,0 +1,33 @@ +#ifndef BASEROOM_HPP_ +#define BASEROOM_HPP_ + +#include "mail_box.hpp" + +#include +#include + +class BaseRoom { +public: + BaseRoom(std::map args); + ~BaseRoom() = default; + + virtual void Init() = 0; + virtual void Loop() = 0; + virtual void Quit() = 0; + + bool SetRunning(bool b) { return running = b; } + bool GetRunning() const { return running; } + + MailBox* GetMailBox() { return& mailBox; } + +protected: + std::map const arguments; + MailBox mailBox; + +private: + bool running = true; +}; + +int roomThread(void*); + +#endif diff --git a/server/mail_box.cpp b/server/mail_box.cpp new file mode 100644 index 0000000..24e89b2 --- /dev/null +++ b/server/mail_box.cpp @@ -0,0 +1,93 @@ +#include "mail_box.hpp" + +#include +#include + +MailBox::MailBox() { + if (!(inLock = SDL_CreateMutex())) { + std::ostringstream os; + os << "Failed to create a MailBox mutex; "; + os << SDL_GetError(); + throw(std::runtime_error(os.str())); + } + if (!(outLock = SDL_CreateMutex())) { + SDL_DestroyMutex(inLock); + std::ostringstream os; + os << "Failed to create a MailBox mutex; "; + os << SDL_GetError(); + throw(std::runtime_error(os.str())); + } +} + +MailBox::~MailBox() { + SDL_DestroyMutex(inLock); + SDL_DestroyMutex(outLock); +} + +std::string MailBox::PushIn(std::string msg) { + SDL_LockMutex(inLock); + input.push_back(msg); + SDL_UnlockMutex(inLock); + return msg; +} + +std::string MailBox::PeekIn() { + std::string ret; + SDL_LockMutex(inLock); + if (input.size()) { + ret = input[0]; + } + else { + ret = ""; + } + SDL_UnlockMutex(inLock); + return ret; +} + +std::string MailBox::PopIn() { + std::string ret; + SDL_LockMutex(inLock); + if (input.size()) { + ret = input[0]; + input.pop_front(); + } + else { + ret = ""; + } + SDL_UnlockMutex(inLock); + return ret; +} + +std::string MailBox::PushOut(std::string msg) { + SDL_LockMutex(outLock); + output.push_back(msg); + SDL_UnlockMutex(outLock); + return msg; +} + +std::string MailBox::PeekOut() { + std::string ret; + SDL_LockMutex(outLock); + if (output.size()) { + ret = output[0]; + } + else { + ret = ""; + } + SDL_UnlockMutex(outLock); + return ret; +} + +std::string MailBox::PopOut() { + std::string ret; + SDL_LockMutex(outLock); + if (output.size()) { + ret = output[0]; + output.pop_front(); + } + else { + ret = ""; + } + SDL_UnlockMutex(outLock); + return ret; +} diff --git a/server/mail_box.hpp b/server/mail_box.hpp new file mode 100644 index 0000000..15ec1ce --- /dev/null +++ b/server/mail_box.hpp @@ -0,0 +1,28 @@ +#ifndef MAILBOX_HPP_ +#define MAILBOX_HPP_ + +#include "SDL/SDL_thread.h" + +#include +#include + +//Thread safe mailbox +class MailBox { +public: + MailBox(); + ~MailBox(); + + std::string PushIn(std::string); + std::string PeekIn(); + std::string PopIn(); + std::string PushOut(std::string); + std::string PeekOut(); + std::string PopOut(); +private: + std::deque input; + std::deque output; + SDL_mutex* inLock = nullptr; + SDL_mutex* outLock = nullptr; +}; + +#endif diff --git a/server/main.cpp b/server/main.cpp index cf64f6c..e3066da 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -1,4 +1,4 @@ -#include "application.hpp" +#include "server_application.hpp" #include "SDL/SDL.h" @@ -12,7 +12,7 @@ int main(int, char**) { cout << "Beginning server" << endl; #endif try { - Application app; + ServerApplication app; app.Init(); app.Loop(); app.Quit(); diff --git a/server/room.cpp b/server/room.cpp deleted file mode 100644 index c61fe24..0000000 --- a/server/room.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "room.hpp" - -Room::Room() { - //TODO -} - -Room::~Room() { - //TODO -} - -void Room::Init() { - //TODO - - //disable this for debugging - running = false; -} - -void Room::Loop() { - while(running) { - //TODO - } -} - -void Room::Quit() { - //TODO -} diff --git a/server/room.hpp b/server/room.hpp deleted file mode 100644 index 8538438..0000000 --- a/server/room.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef ROOM_HPP_ -#define ROOM_HPP_ - -class Room { -public: - Room(/* args */); - ~Room(); - - void Init(); - void Loop(); - void Quit(); - - bool GetRunning() const { return running; } -private: - bool running = true; -}; - -#endif diff --git a/server/server_application.cpp b/server/server_application.cpp new file mode 100644 index 0000000..94dc7cd --- /dev/null +++ b/server/server_application.cpp @@ -0,0 +1,29 @@ +#include "server_application.hpp" + +ServerApplication::ServerApplication() { + //TODO +} + +ServerApplication::~ServerApplication() { + //TODO +} + +void ServerApplication::Init() { + //TODO +} + +void ServerApplication::Loop() { + //TODO +} + +void ServerApplication::Quit() { + //TODO +} + +void ServerApplication::OpenRoom(std::map args) { + //TODO +} + +void ServerApplication::CloseRoom(roomHandle) { + //TODO +} diff --git a/server/server_application.hpp b/server/server_application.hpp new file mode 100644 index 0000000..9bd4847 --- /dev/null +++ b/server/server_application.hpp @@ -0,0 +1,37 @@ +#ifndef SERVERAPPLICATION_HPP_ +#define SERVERAPPLICATION_HPP_ + +#include "base_room.hpp" + +#include "SDL/SDL_thread.h" + +#include +#include +#include + +struct roomHandle { + SDL_Thread* thread = nullptr; + BaseRoom* room = nullptr; +}; + +class ServerApplication { +public: + ServerApplication(); + ~ServerApplication(); + + void Init(); + void Loop(); + void Quit(); + + bool SetRunning(bool b) { return running = b; } + bool GetRunning() const { return running; } + +private: + void OpenRoom(std::map); + void CloseRoom(roomHandle); + + std::list rooms; + bool running = true; +}; + +#endif diff --git a/server/threading.hpp b/server/threading.hpp deleted file mode 100644 index 630f88f..0000000 --- a/server/threading.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef THREADING_HPP_ -#define THREADING_HPP_ - -#include "room.hpp" - -#include "SDL/SDL_thread.h" - -struct roomHandle { - SDL_Thread* thread = nullptr; - Room* room = nullptr; -}; - -int roomThread(void*); - -#endif