From 8c600a1cdb31e4b28eb2bf6271c203166702774f Mon Sep 17 00:00:00 2001 From: IlushaShurupov Date: Fri, 21 Jul 2023 19:55:31 +0300 Subject: [PATCH] Restructure Storage Module -> Connection module --- CMakeLists.txt | 2 +- CommandLine/CMakeLists.txt | 2 +- CommandLine/private/CommandLine.cpp | 4 +- CommandLine/public/CommandLine.hpp | 8 +- {Storage => Connection}/CMakeLists.txt | 10 +- Connection/private/ConnectionCommon.cpp | 9 ++ .../private/LocalConnection.cpp | 31 +++---- Connection/private/RemoteConnection.cpp | 2 + .../private/bindings/Disk.cpp | 20 ++-- Connection/private/bindings/Disk.hpp | 17 ++++ .../private/bindings/Network.cpp | 2 +- .../private/bindings/Network.hpp | 15 --- Connection/public/ConnectionCommon.hpp | 7 ++ .../public/LocalConnection.hpp | 48 +++++----- .../public/RemoteConnection.hpp | 0 .../tests/TestLocal.cpp | 12 +-- Connection/tests/Tests.cpp | 19 ++++ Storage/applications/CMakeLists.txt | 13 --- Storage/applications/Client.cpp | 64 ------------- Storage/applications/Server.cpp | 92 ------------------- Storage/private/Storage.cpp | 9 -- Storage/public/RemoteStorage.hpp | 17 ---- Storage/public/Storage.hpp | 53 ----------- Storage/tests/Tests.cpp | 19 ---- 24 files changed, 121 insertions(+), 354 deletions(-) rename {Storage => Connection}/CMakeLists.txt (76%) create mode 100644 Connection/private/ConnectionCommon.cpp rename Storage/private/LocalStorage.cpp => Connection/private/LocalConnection.cpp (64%) create mode 100644 Connection/private/RemoteConnection.cpp rename Storage/private/SystemAPIFile.cpp => Connection/private/bindings/Disk.cpp (63%) create mode 100644 Connection/private/bindings/Disk.hpp rename Storage/private/SystemAPINet.cpp => Connection/private/bindings/Network.cpp (99%) rename Storage/public/SystemAPI.hpp => Connection/private/bindings/Network.hpp (66%) create mode 100644 Connection/public/ConnectionCommon.hpp rename Storage/public/LocalStorage.hpp => Connection/public/LocalConnection.hpp (51%) rename Storage/private/RemoteStorage.cpp => Connection/public/RemoteConnection.hpp (100%) rename Storage/tests/TestLocalStorage.cpp => Connection/tests/TestLocal.cpp (53%) create mode 100644 Connection/tests/Tests.cpp delete mode 100644 Storage/applications/CMakeLists.txt delete mode 100644 Storage/applications/Client.cpp delete mode 100644 Storage/applications/Server.cpp delete mode 100644 Storage/private/Storage.cpp delete mode 100644 Storage/public/RemoteStorage.hpp delete mode 100644 Storage/public/Storage.hpp delete mode 100644 Storage/tests/Tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d731dda..db1eb4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,4 +18,4 @@ add_subdirectory(Allocators) add_subdirectory(Strings) add_subdirectory(Tokenizer) add_subdirectory(CommandLine) -add_subdirectory(Storage) +add_subdirectory(Connection) diff --git a/CommandLine/CMakeLists.txt b/CommandLine/CMakeLists.txt index 33cea0b..eaf3fc7 100644 --- a/CommandLine/CMakeLists.txt +++ b/CommandLine/CMakeLists.txt @@ -10,7 +10,7 @@ file(GLOB SOURCES "./private/*.cpp") file(GLOB HEADERS "./public/*.hpp") add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) target_include_directories(${PROJECT_NAME} PUBLIC ./public/) -target_link_libraries(${PROJECT_NAME} PUBLIC Tokenizer Storage) +target_link_libraries(${PROJECT_NAME} PUBLIC Tokenizer Connection) ### -------------------------- Tests -------------------------- ### enable_testing() diff --git a/CommandLine/private/CommandLine.cpp b/CommandLine/private/CommandLine.cpp index 24d56cb..e5743ad 100644 --- a/CommandLine/private/CommandLine.cpp +++ b/CommandLine/private/CommandLine.cpp @@ -3,7 +3,7 @@ using namespace tp; -static ModuleManifest* sModuleDependencies[] = { &gModuleTokenizer, &gModuleStorage, nullptr }; +static ModuleManifest* sModuleDependencies[] = { &gModuleTokenizer, &gModuleConnection, nullptr }; ModuleManifest tp::gModuleCommandLine = ModuleManifest("CommandLine", nullptr, nullptr, sModuleDependencies); const char* regexSpace = "\n|\t| |\r"; @@ -219,7 +219,7 @@ alni CommandLine::getInt(const String& id) const { auto& arg = getArg(id, Arg::I alnf CommandLine::getFloat(const String& id) const { auto& arg = getArg(id, Arg::FLOAT); return arg.mFloat.mVal; } bool CommandLine::getBool(const String& id) const { auto& arg = getArg(id, Arg::BOOL); return arg.mBool.mFlag; } const String& CommandLine::getString(const String& id) const { auto& arg = getArg(id, Arg::STR); return arg.mStr.mStr; } -const FileLocation& CommandLine::getFile(const String& id) const { auto& arg = getArg(id, Arg::FILE_IN); return arg.mFile.mFileLocation; } +const LocalConnectionLocation& CommandLine::getFile(const String& id) const { auto& arg = getArg(id, Arg::FILE_IN); return arg.mFile.mFileLocation; } CommandLine::Arg& CommandLine::getArg(const String& id, Arg::Type type) { diff --git a/CommandLine/public/CommandLine.hpp b/CommandLine/public/CommandLine.hpp index 51c98b6..edf049c 100644 --- a/CommandLine/public/CommandLine.hpp +++ b/CommandLine/public/CommandLine.hpp @@ -1,8 +1,8 @@ #pragma once -#include "Tokenizer.hpp" #include "Map.hpp" -#include "LocalStorage.hpp" +#include "LocalConnection.hpp" +#include "Tokenizer.hpp" namespace tp { @@ -33,7 +33,7 @@ namespace tp { }; struct FileInputArg { - FileLocation mFileLocation; + LocalConnectionLocation mFileLocation; }; struct Arg { @@ -81,7 +81,7 @@ namespace tp { [[nodiscard]] alnf getFloat(const String& id) const; [[nodiscard]] bool getBool(const String& id) const; [[nodiscard]] const String& getString(const String& id) const; - [[nodiscard]] const FileLocation& getFile(const String& id) const; + [[nodiscard]] const LocalConnectionLocation& getFile(const String& id) const; const CommandLine& operator=(const CommandLine&) = delete; diff --git a/Storage/CMakeLists.txt b/Connection/CMakeLists.txt similarity index 76% rename from Storage/CMakeLists.txt rename to Connection/CMakeLists.txt index 0d982b1..cd1102d 100644 --- a/Storage/CMakeLists.txt +++ b/Connection/CMakeLists.txt @@ -3,22 +3,18 @@ cmake_minimum_required(VERSION 3.2) set(CMAKE_CXX_STANDARD 23) -project(Storage) +project(Connection) -set(ASIO_INCLUDE ./../Externals/asio/asio/include) +set(BINDINGS_INCLUDE ./../Externals/asio/asio/include) ### ---------------------- Static Library --------------------- ### file(GLOB SOURCES "./private/*.cpp" "./private/*/*.cpp") file(GLOB HEADERS "./public/*.hpp") add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS}) target_include_directories(${PROJECT_NAME} PUBLIC ./public/) -target_include_directories(${PROJECT_NAME} PRIVATE ${ASIO_INCLUDE}) +target_include_directories(${PROJECT_NAME} PRIVATE ${BINDINGS_INCLUDE}) target_link_libraries(${PROJECT_NAME} PUBLIC Strings) - -### -------------------------- Applications -------------------------- ### -add_subdirectory(applications) - ### -------------------------- Tests -------------------------- ### enable_testing() file(GLOB TEST_SOURCES "./tests/*.cpp") diff --git a/Connection/private/ConnectionCommon.cpp b/Connection/private/ConnectionCommon.cpp new file mode 100644 index 0000000..0592308 --- /dev/null +++ b/Connection/private/ConnectionCommon.cpp @@ -0,0 +1,9 @@ + +#include "ConnectionCommon.hpp" + +static tp::ModuleManifest* sModuleDependencies[] = { + &tp::gModuleStrings, + nullptr +}; + +tp::ModuleManifest tp::gModuleConnection = ModuleManifest("Storage", nullptr, nullptr, sModuleDependencies); \ No newline at end of file diff --git a/Storage/private/LocalStorage.cpp b/Connection/private/LocalConnection.cpp similarity index 64% rename from Storage/private/LocalStorage.cpp rename to Connection/private/LocalConnection.cpp index b3cfb8b..f12995b 100644 --- a/Storage/private/LocalStorage.cpp +++ b/Connection/private/LocalConnection.cpp @@ -1,13 +1,12 @@ -#include "LocalStorage.hpp" +#include "LocalConnection.hpp" -#include "SystemAPI.hpp" +#include "bindings/Disk.hpp" #include using namespace tp; - -bool FileLocation::exists() const { +bool LocalConnectionLocation::exists() const { FILE* file = fopen(mLocation.read(), "r"); if (file) { // File exists, close it and return 1 @@ -17,18 +16,18 @@ bool FileLocation::exists() const { return false; } -bool File::connect(const FileLocation& location, const FileConnectionType& connectionInfo) { +bool LocalConnection::connect(const LocalConnectionLocation& location, const LocalConnectionType& connectionInfo) { DEBUG_ASSERT(!mStatus.isOpened()); if (mStatus.isOpened()) return false; - auto handle = new FileSystemHandle(); + auto handle = new LocalConnectionContext(); switch (connectionInfo.getType()) { - case FileConnectionType::READ: + case LocalConnectionType::READ: handle->open(location.getLocation().read(), false); break; - case FileConnectionType::WRITE: + case LocalConnectionType::WRITE: handle->open(location.getLocation().read(), true); break; @@ -37,33 +36,33 @@ bool File::connect(const FileLocation& location, const FileConnectionType& conne }; if (!handle->isOpen()) { - mStatus.setStatus(FileConnectionStatus::DENIED); + mStatus.setStatus(LocalConnectionStatus::DENIED); delete handle; return false; } - mStatus.setStatus(FileConnectionStatus::OPENED); + mStatus.setStatus(LocalConnectionStatus::OPENED); mHandle = handle; mConnectionType = connectionInfo; return true; } -bool File::disconnect() { +bool LocalConnection::disconnect() { DEBUG_ASSERT(mStatus.isOpened()); if (!mStatus.isOpened() || !mHandle) return false; mHandle->close(); delete mHandle; - mStatus.setStatus(FileConnectionStatus::CLOSED); + mStatus.setStatus(LocalConnectionStatus::CLOSED); return true; } -bool File::setPointer(BytePointer pointer) { +bool LocalConnection::setPointer(BytePointer pointer) { DEBUG_ASSERT(mStatus.isOpened()); if (!mStatus.isOpened()) return false; return true; } -bool File::readBytes(Byte* bytes, SizeBytes size) { +bool LocalConnection::readBytes(Byte* bytes, SizeBytes size) { DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isWrite()); if (!mStatus.isOpened() || !mConnectionType.isWrite()) return false; @@ -73,7 +72,7 @@ bool File::readBytes(Byte* bytes, SizeBytes size) { return true; } -bool File::writeBytes(const Byte* bytes, SizeBytes size) { +bool LocalConnection::writeBytes(const Byte* bytes, SizeBytes size) { DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isRead()); if (!mStatus.isOpened() || !mConnectionType.isRead()) return false; @@ -83,7 +82,7 @@ bool File::writeBytes(const Byte* bytes, SizeBytes size) { return true; } -File::SizeBytes File::size() { +LocalConnection::SizeBytes LocalConnection::size() { DEBUG_ASSERT(mStatus.isOpened()); if (!mStatus.isOpened() || !mHandle) return 0; return mHandle->size(); diff --git a/Connection/private/RemoteConnection.cpp b/Connection/private/RemoteConnection.cpp new file mode 100644 index 0000000..9495b4f --- /dev/null +++ b/Connection/private/RemoteConnection.cpp @@ -0,0 +1,2 @@ + +#include "RemoteConnection.hpp" diff --git a/Storage/private/SystemAPIFile.cpp b/Connection/private/bindings/Disk.cpp similarity index 63% rename from Storage/private/SystemAPIFile.cpp rename to Connection/private/bindings/Disk.cpp index 5278d2f..caf6bdc 100644 --- a/Storage/private/SystemAPIFile.cpp +++ b/Connection/private/bindings/Disk.cpp @@ -1,10 +1,10 @@ -#include "SystemAPI.hpp" +#include "Disk.hpp" #include using namespace tp; -ualni tp::FileSystemHandle::size() { +ualni tp::LocalConnectionContext::size() { auto strm = (std::fstream*) stream; ualni out = 0; strm->seekg(0, std::ios::beg); @@ -14,41 +14,41 @@ ualni tp::FileSystemHandle::size() { return out; } -FileSystemHandle::FileSystemHandle() { +LocalConnectionContext::LocalConnectionContext() { stream = new std::fstream(); } -FileSystemHandle::~FileSystemHandle() { +LocalConnectionContext::~LocalConnectionContext() { auto strm = (std::fstream*) stream; delete strm; } -bool FileSystemHandle::isOpen() { +bool LocalConnectionContext::isOpen() { auto strm = (std::fstream*) stream; return strm->is_open(); } -void FileSystemHandle::close() { +void LocalConnectionContext::close() { auto strm = (std::fstream*) stream; strm->close(); } -void FileSystemHandle::seekp(ualni in) { +void LocalConnectionContext::seekp(ualni in) { auto strm = (std::fstream*) stream; strm->seekp(in); } -void FileSystemHandle::read(int1* in, ualni size) { +void LocalConnectionContext::read(int1* in, ualni size) { auto strm = (std::fstream*) stream; strm->read(in, size); } -void FileSystemHandle::write(const int1* in, ualni size) { +void LocalConnectionContext::write(const int1* in, ualni size) { auto strm = (std::fstream*) stream; strm->write(in, size); } -void FileSystemHandle::open(const char* path, bool read) { +void LocalConnectionContext::open(const char* path, bool read) { auto strm = (std::fstream*) stream; if (read) strm->open(path, std::ios::in | std::ios::binary | std::ios::app); diff --git a/Connection/private/bindings/Disk.hpp b/Connection/private/bindings/Disk.hpp new file mode 100644 index 0000000..02d247b --- /dev/null +++ b/Connection/private/bindings/Disk.hpp @@ -0,0 +1,17 @@ +#include "Common.hpp" + +namespace tp { + class LocalConnectionContext { + void* stream; + public: + LocalConnectionContext(); + ~LocalConnectionContext(); + void open(const char*, bool); + bool isOpen(); + void close(); + void seekp(ualni); + void read(int1*, ualni); + void write(const int1*, ualni); + ualni size(); + }; +} \ No newline at end of file diff --git a/Storage/private/SystemAPINet.cpp b/Connection/private/bindings/Network.cpp similarity index 99% rename from Storage/private/SystemAPINet.cpp rename to Connection/private/bindings/Network.cpp index 3106571..779de03 100644 --- a/Storage/private/SystemAPINet.cpp +++ b/Connection/private/bindings/Network.cpp @@ -1,4 +1,4 @@ -#include "SystemAPI.hpp" +#include "Network.hpp" #include "asio.hpp" diff --git a/Storage/public/SystemAPI.hpp b/Connection/private/bindings/Network.hpp similarity index 66% rename from Storage/public/SystemAPI.hpp rename to Connection/private/bindings/Network.hpp index ff1e681..9258c49 100644 --- a/Storage/public/SystemAPI.hpp +++ b/Connection/private/bindings/Network.hpp @@ -3,21 +3,6 @@ #include "Common.hpp" namespace tp { - class FileSystemHandle { - void* stream; - public: - FileSystemHandle(); - ~FileSystemHandle(); - void open(const char*, bool); - bool isOpen(); - void close(); - void seekp(ualni); - void read(int1*, ualni); - void write(const int1*, ualni); - ualni size(); - }; - - class ServerContext; class ClientContext; diff --git a/Connection/public/ConnectionCommon.hpp b/Connection/public/ConnectionCommon.hpp new file mode 100644 index 0000000..2e964bb --- /dev/null +++ b/Connection/public/ConnectionCommon.hpp @@ -0,0 +1,7 @@ +#pragma once + +#include "Strings.hpp" + +namespace tp { + extern ModuleManifest gModuleConnection; +}; \ No newline at end of file diff --git a/Storage/public/LocalStorage.hpp b/Connection/public/LocalConnection.hpp similarity index 51% rename from Storage/public/LocalStorage.hpp rename to Connection/public/LocalConnection.hpp index f1cf805..552b707 100644 --- a/Storage/public/LocalStorage.hpp +++ b/Connection/public/LocalConnection.hpp @@ -1,22 +1,22 @@ #pragma once -#include "Storage.hpp" +#include "ConnectionCommon.hpp" namespace tp { - class FileSystemHandle; + class LocalConnectionContext; - class FileLocation { + class LocalConnectionLocation { String mLocation; public: - FileLocation() : mLocation("tmp") {}; - explicit FileLocation(const String& location) : mLocation(location) {} + LocalConnectionLocation() : mLocation("tmp") {}; + explicit LocalConnectionLocation(const String& location) : mLocation(location) {} void setLocation(const String& location) { mLocation = location; } [[nodiscard]] const String& getLocation() const { return mLocation; } [[nodiscard]] bool exists() const; }; - class FileConnectionType { + class LocalConnectionType { public: enum HandleType { READ, @@ -28,15 +28,15 @@ namespace tp { HandleType mHandleType; public: - FileConnectionType() : mHandleType(NONE) {} - explicit FileConnectionType(bool read) : mHandleType((HandleType) read) {} - explicit FileConnectionType(HandleType handleType) : mHandleType(handleType) {} + LocalConnectionType() : mHandleType(NONE) {} + explicit LocalConnectionType(bool read) : mHandleType((HandleType) read) {} + explicit LocalConnectionType(HandleType handleType) : mHandleType(handleType) {} [[nodiscard]] HandleType getType() const { return mHandleType; } [[nodiscard]] bool isRead() const { return mHandleType == READ; } [[nodiscard]] bool isWrite() const { return mHandleType == WRITE; } }; - class FileConnectionStatus { + class LocalConnectionStatus { public: enum Status { NONE, @@ -50,43 +50,43 @@ namespace tp { Status mStatus = NONE; public: - FileConnectionStatus() = default; + LocalConnectionStatus() = default; [[nodiscard]] Status getStatus() const { return mStatus; } void setStatus(Status status) { mStatus = status; } [[nodiscard]] bool isOpened() const { return mStatus == OPENED; } }; - class File { + class LocalConnection { typedef ualni SizeBytes; typedef ualni BytePointer; typedef int1 Byte; private: - FileSystemHandle* mHandle = nullptr; + LocalConnectionContext* mHandle = nullptr; - FileLocation mLocation; - FileConnectionType mConnectionType; - FileConnectionStatus mStatus; + LocalConnectionLocation mLocation; + LocalConnectionType mConnectionType; + LocalConnectionStatus mStatus; BytePointer mPointer = 0; public: - File() { - MODULE_SANITY_CHECK(gModuleStorage) + LocalConnection() { + MODULE_SANITY_CHECK(gModuleConnection) }; - virtual ~File() { - if (mStatus.isOpened()) File::disconnect(); + virtual ~LocalConnection() { + if (mStatus.isOpened()) LocalConnection::disconnect(); } public: - virtual bool connect(const FileLocation& location, const FileConnectionType& connectionInfo); + virtual bool connect(const LocalConnectionLocation& location, const LocalConnectionType& connectionInfo); virtual bool disconnect(); public: - virtual const FileConnectionStatus& getConnectionStatus() { return mStatus; } - virtual const FileConnectionType& getConnectionType() { return mConnectionType; } - virtual const FileLocation& getLocation() { return mLocation; } + virtual const LocalConnectionStatus& getConnectionStatus() { return mStatus; } + virtual const LocalConnectionType& getConnectionType() { return mConnectionType; } + virtual const LocalConnectionLocation& getLocation() { return mLocation; } public: virtual bool setPointer(BytePointer pointer); diff --git a/Storage/private/RemoteStorage.cpp b/Connection/public/RemoteConnection.hpp similarity index 100% rename from Storage/private/RemoteStorage.cpp rename to Connection/public/RemoteConnection.hpp diff --git a/Storage/tests/TestLocalStorage.cpp b/Connection/tests/TestLocal.cpp similarity index 53% rename from Storage/tests/TestLocalStorage.cpp rename to Connection/tests/TestLocal.cpp index b4c765d..028e9b9 100644 --- a/Storage/tests/TestLocalStorage.cpp +++ b/Connection/tests/TestLocal.cpp @@ -1,6 +1,6 @@ +#include "LocalConnection.hpp" #include "Testing.hpp" -#include "LocalStorage.hpp" using namespace tp; @@ -10,15 +10,15 @@ TEST_DEF_STATIC(Simple) { int1 dataRead[6]{}; { - File file; - file.connect(FileLocation(String("tmp2.txt")), FileConnectionType(false)); + LocalConnection file; + file.connect(LocalConnectionLocation(String("tmp2.txt")), LocalConnectionType(false)); file.writeBytes(data, 6); file.disconnect(); } { - File file; - file.connect(FileLocation(String("tmp2.txt")), FileConnectionType(true)); + LocalConnection file; + file.connect(LocalConnectionLocation(String("tmp2.txt")), LocalConnectionType(true)); file.readBytes(dataRead, 6); file.disconnect(); } @@ -28,6 +28,6 @@ TEST_DEF_STATIC(Simple) { } } -TEST_DEF(LocalStorage) { +TEST_DEF(LocalConnection) { testSimple(); } \ No newline at end of file diff --git a/Connection/tests/Tests.cpp b/Connection/tests/Tests.cpp new file mode 100644 index 0000000..6662ee9 --- /dev/null +++ b/Connection/tests/Tests.cpp @@ -0,0 +1,19 @@ + +#include "ConnectionCommon.hpp" +#include "Testing.hpp" + +void testLocalConnection(); + +int main() { + + tp::ModuleManifest* deps[] = { &tp::gModuleConnection, nullptr }; + tp::ModuleManifest testModule("ConnectionTest", nullptr, nullptr, deps); + + if (!testModule.initialize()) { + return 1; + } + + testLocalConnection(); + + testModule.deinitialize(); +} \ No newline at end of file diff --git a/Storage/applications/CMakeLists.txt b/Storage/applications/CMakeLists.txt deleted file mode 100644 index dd0142d..0000000 --- a/Storage/applications/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ - -cmake_minimum_required(VERSION 3.2) - -project(Applications) - -file(GLOB SOURCES_CLIENT ./Client.cpp ./SystemAPI.cpp) -file(GLOB SOURCES_SERVER ./Server.cpp ./SystemAPI.cpp) - -add_executable(Client ${SOURCES_CLIENT}) -target_link_libraries(Client Storage CommandLine) - -add_executable(Server ${SOURCES_SERVER}) -target_link_libraries(Server Storage CommandLine) diff --git a/Storage/applications/Client.cpp b/Storage/applications/Client.cpp deleted file mode 100644 index 35f1c26..0000000 --- a/Storage/applications/Client.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "SystemAPI.hpp" - -#include "Strings.hpp" - -#include - -using namespace tp; - -class CharClient { - - typedef void* (*ThreadFunction)(void *); - - Client client; - pthread_mutex_t mutex; - -public: - CharClient(int port, const char* ip) { - pthread_mutex_unlock(&mutex); - client.connect(ip, port); - } - - void start() { - // Create a new thread to handle the client - pthread_t threadId; - if (pthread_create(&threadId, nullptr, (ThreadFunction) readServerBroadcast, this)) { - printf("Failed to create thread for client\n"); - return; - } - - // Detach the thread so it can run independently - pthread_detach(threadId); - - while (true) { - const auto length = 1024; - static char message[length]; - - printf(" >> "); - fgets(message, length, stdin); - - if (String::Logic::isEqualLogic(message, "exit")) return; - - pthread_mutex_lock(&mutex); - client.write(message); - pthread_mutex_unlock(&mutex); - } - } - - static void* readServerBroadcast(CharClient* self) { - while (true) { - auto message = self->client.read(); - pthread_mutex_lock(&self->mutex); - printf("Broadcast : %s \n", message); - delete[] message; - pthread_mutex_unlock(&self->mutex); - } - return nullptr; - } -}; - -int main() { - CharClient client(5555, "127.0.0.1"); - client.start(); - return 0; -} diff --git a/Storage/applications/Server.cpp b/Storage/applications/Server.cpp deleted file mode 100644 index eea79f1..0000000 --- a/Storage/applications/Server.cpp +++ /dev/null @@ -1,92 +0,0 @@ - -#include "SystemAPI.hpp" -#include "Strings.hpp" -#include "List.hpp" - -#include - -using namespace tp; - -class ChatServer { - - struct SharedData { - List clients; - pthread_mutex_t mutex; - }; - - SharedData mSharedData; - Server server; - -public: - explicit ChatServer(int port) { - server.start(port); - } - - ~ChatServer() { - DEBUG_ASSERT(false) - } - - bool start() { - while (true) { - auto clientSocket = server.accept(); - - // add client - pthread_mutex_lock(&mSharedData.mutex); - mSharedData.clients.pushBack(clientSocket); - pthread_mutex_unlock(&mSharedData.mutex); - - // Create a new thread to handle the client - pthread_t threadId; - if (pthread_create(&threadId, nullptr, (void* (*)(void*)) handleClient, this)) { - printf("Failed to create thread for client\n"); - return false; - } - - // Detach the thread so it can run independently - pthread_detach(threadId); - } - return true; - } - - - static void* handleClient(ChatServer* self) { - // read shared data - current client id - pthread_mutex_lock(&self->mSharedData.mutex); - auto clientSocket = self->mSharedData.clients.last(); - pthread_mutex_unlock(&self->mSharedData.mutex); - - MESSAGE: - // wait for a message request - auto message = Server::read(clientSocket); - - // Broadcast to all clients - for (auto client : self->mSharedData.clients) { - Server::write(client.data(), message); - } - - auto exit = memCompare(message, "exit", 5) == 0; - - delete[] message; - - if (exit) { - // TODO : disconnect - for (auto iter : self->mSharedData.clients) { - if (clientSocket == iter.data()) { - self->mSharedData.clients.removeNode(iter.node()); - break; - } - } - } - - pthread_mutex_unlock(&self->mSharedData.mutex); - - if (exit) return nullptr; - goto MESSAGE; - } -}; - -int main() { - ChatServer server(5555); - server.start(); - return 0; -} diff --git a/Storage/private/Storage.cpp b/Storage/private/Storage.cpp deleted file mode 100644 index 197a710..0000000 --- a/Storage/private/Storage.cpp +++ /dev/null @@ -1,9 +0,0 @@ - -#include "Storage.hpp" - -static tp::ModuleManifest* sModuleDependencies[] = { - &tp::gModuleStrings, - nullptr -}; - -tp::ModuleManifest tp::gModuleStorage = ModuleManifest("Storage", nullptr, nullptr, sModuleDependencies); \ No newline at end of file diff --git a/Storage/public/RemoteStorage.hpp b/Storage/public/RemoteStorage.hpp deleted file mode 100644 index 4a1d13c..0000000 --- a/Storage/public/RemoteStorage.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "SystemAPI.hpp" - -namespace tp { - - /* - class Client : public Storage { - // same as local storage file but transfers all commands to server - }; - - class Server { - // opens local storage and transfers all client command to it - // manages multiple clients - }; - */ -} \ No newline at end of file diff --git a/Storage/public/Storage.hpp b/Storage/public/Storage.hpp deleted file mode 100644 index b2f2b87..0000000 --- a/Storage/public/Storage.hpp +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -#include "Strings.hpp" - -namespace tp { - - extern ModuleManifest gModuleStorage; - - class StorageLocation { - public: - StorageLocation() = default; - virtual ~StorageLocation() = default; - }; - - class ConnectionType { - public: - ConnectionType() = default; - virtual ~ConnectionType() = default; - }; - - class ConnectionStatus { - public: - ConnectionStatus() = default; - virtual ~ConnectionStatus() = default; - }; - - class Storage { - typedef ualni SizeBytes; - typedef ualni BytePointer; - typedef int1 Byte; - - private: - StorageLocation mLocation; - ConnectionType mConnectionType; - ConnectionStatus mStatus; - - public: - Storage() = default; - virtual ~Storage() = default; - - public: - virtual bool connect(const StorageLocation& location, const ConnectionType& connectionInfo) = 0; - virtual bool disconnect() = 0; - virtual const ConnectionStatus& getConnectionStatus() = 0; - - public: - virtual bool readBytes(BytePointer pointer, Byte* bytes, SizeBytes size) = 0; - virtual bool writeBytes(BytePointer pointer, const Byte* bytes, SizeBytes size) = 0; - - public: - virtual SizeBytes size() = 0; - }; -}; \ No newline at end of file diff --git a/Storage/tests/Tests.cpp b/Storage/tests/Tests.cpp deleted file mode 100644 index 36f7fd5..0000000 --- a/Storage/tests/Tests.cpp +++ /dev/null @@ -1,19 +0,0 @@ - -#include "Testing.hpp" -#include "Storage.hpp" - -void testLocalStorage(); - -int main() { - - tp::ModuleManifest* deps[] = { &tp::gModuleStorage, nullptr }; - tp::ModuleManifest testModule("StorageTest", nullptr, nullptr, deps); - - if (!testModule.initialize()) { - return 1; - } - - testLocalStorage(); - - testModule.deinitialize(); -} \ No newline at end of file