Restructure Storage Module -> Connection module
This commit is contained in:
parent
fa4d1d72dc
commit
e9dbee6667
24 changed files with 121 additions and 354 deletions
|
|
@ -18,4 +18,4 @@ add_subdirectory(Allocators)
|
|||
add_subdirectory(Strings)
|
||||
add_subdirectory(Tokenizer)
|
||||
add_subdirectory(CommandLine)
|
||||
add_subdirectory(Storage)
|
||||
add_subdirectory(Connection)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
9
Connection/private/ConnectionCommon.cpp
Normal file
9
Connection/private/ConnectionCommon.cpp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
#include "ConnectionCommon.hpp"
|
||||
|
||||
static tp::ModuleManifest* sModuleDependencies[] = {
|
||||
&tp::gModuleStrings,
|
||||
nullptr
|
||||
};
|
||||
|
||||
tp::ModuleManifest tp::gModuleConnection = ModuleManifest("Storage", nullptr, nullptr, sModuleDependencies);
|
||||
|
|
@ -1,13 +1,12 @@
|
|||
#include "LocalStorage.hpp"
|
||||
#include "LocalConnection.hpp"
|
||||
|
||||
#include "SystemAPI.hpp"
|
||||
#include "bindings/Disk.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
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();
|
||||
2
Connection/private/RemoteConnection.cpp
Normal file
2
Connection/private/RemoteConnection.cpp
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
#include "RemoteConnection.hpp"
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
#include "SystemAPI.hpp"
|
||||
#include "Disk.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
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);
|
||||
17
Connection/private/bindings/Disk.hpp
Normal file
17
Connection/private/bindings/Disk.hpp
Normal file
|
|
@ -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();
|
||||
};
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
#include "SystemAPI.hpp"
|
||||
#include "Network.hpp"
|
||||
|
||||
#include "asio.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;
|
||||
|
||||
7
Connection/public/ConnectionCommon.hpp
Normal file
7
Connection/public/ConnectionCommon.hpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "Strings.hpp"
|
||||
|
||||
namespace tp {
|
||||
extern ModuleManifest gModuleConnection;
|
||||
};
|
||||
|
|
@ -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);
|
||||
|
|
@ -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();
|
||||
}
|
||||
19
Connection/tests/Tests.cpp
Normal file
19
Connection/tests/Tests.cpp
Normal file
|
|
@ -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();
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
#include "SystemAPI.hpp"
|
||||
|
||||
#include "Strings.hpp"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -1,92 +0,0 @@
|
|||
|
||||
#include "SystemAPI.hpp"
|
||||
#include "Strings.hpp"
|
||||
#include "List.hpp"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
using namespace tp;
|
||||
|
||||
class ChatServer {
|
||||
|
||||
struct SharedData {
|
||||
List<Server::Socket> 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;
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
|
||||
#include "Storage.hpp"
|
||||
|
||||
static tp::ModuleManifest* sModuleDependencies[] = {
|
||||
&tp::gModuleStrings,
|
||||
nullptr
|
||||
};
|
||||
|
||||
tp::ModuleManifest tp::gModuleStorage = ModuleManifest("Storage", nullptr, nullptr, sModuleDependencies);
|
||||
|
|
@ -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
|
||||
};
|
||||
*/
|
||||
}
|
||||
|
|
@ -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;
|
||||
};
|
||||
};
|
||||
|
|
@ -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();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue