Skip to content
Snippets Groups Projects
Commit f75083bd authored by Carsten Patzke's avatar Carsten Patzke
Browse files

Added HasIO

parent 34ca4714
No related branches found
No related tags found
No related merge requests found
Showing
with 278 additions and 128 deletions
#Astyle
.orig
# Created by https://www.gitignore.io/api/c++,cmake,clion+all
### C++ ###
......@@ -112,6 +108,9 @@ build
# End of https://www.gitignore.io/api/c++,cmake,clion+all
#Astyle
*.orig
### Doxygen ###
doxygen
......
......@@ -3,6 +3,7 @@ set(SOURCE_FILES
include/common/os.h
include/common/networking.h
include/system_wrappers/io.h
include/system_wrappers/has_io.h src/has_io.cpp
include/system_wrappers/system_io.h src/system_io.cpp
)
......
......@@ -4,40 +4,104 @@
#include <cstdint>
#include "os.h"
namespace hidra2 {
enum OP_CODE : uint8_t {
OP_CODE__HELLO,
};
enum ERROR_CODE : uint16_t {
ERR__NO_ERROR,
ERR__UNSUPPORTED_VERSION,
ERR__INTERNAL_SERVER_ERROR = 65535,
};
struct NetworkRequest {
OP_CODE op_code;
uint64_t request_id;
char data[];
};
struct NetworkResponse {
OP_CODE op_code;
uint64_t request_id;
ERROR_CODE error_code;
char data[];
};
struct OP_HelloRequest {
uint32_t client_version;
OS_TYPE os : 4;
bool is_x64 : 1;
};
struct OP_HelloResponse {
uint32_t server_version;
};
namespace hidra2
{
typedef uint64_t FileReferenceId;
enum OpCode : uint8_t {
OP_CODE__HELLO,
OP_CODE__PREPARE_SEND_DATA,
OP_CODE__SEND_DATA_CHUNK,
};
enum NetworkErrorCode : uint16_t {
NET_ERR__NO_ERROR,
NET_ERR__UNSUPPORTED_VERSION,
NET_ERR__FILENAME_ALREADY_IN_USE,
NET_ERR__UNKNOWN_REFERENCE_ID,
NET_ERR__INTERNAL_SERVER_ERROR = 65535,
};
/**
* @defgroup RPC
* RPC always return a response to a corresponding request
* @{
*/
struct GenericNetworkRequest {
OpCode op_code;
uint64_t request_id;
char data[];
};
struct GenericNetworkResponse {
OpCode op_code;
uint64_t request_id;
NetworkErrorCode error_code;
char data[];
};
struct HelloRequest {
uint32_t client_version;
OS_TYPE os;
//Flags
bool is_x64 : 1;
};
/**
* Possible error codes:
* - ::NET_ERR__UNSUPPORTED_VERSION
*/
struct HelloResponse {
uint32_t server_version;
};
struct PrepareSendDataRequest {
char filename[255];
uint64_t file_size;
};
/**
* Possible error codes:
* - ::NET_ERR__FILENAME_ALREADY_IN_USE
*/
struct PrepareSendDataResponse {
FileReferenceId file_reference_id;
};
struct SendDataChunkRequest {
FileReferenceId file_reference_id;
uint64_t start_byte;
uint64_t chunk_size;
};
/**
* Possible error codes:
* - ::NET_ERR__UNKNOWN_REFERENCE_ID
*/
struct SendDataChunkResponse {
};
/** @} */
/**
* @defgroup EVENT
* Events cannot be requests, they will be send by the server spontaneously
* @{
*/
struct GenericNetworkEvent {
OpCode op_code;
NetworkErrorCode error_code;
char data[];
};
/**
* Possible error codes:
* - TODO
*/
struct FileStatusEvent {
uint64_t file_reference_id;
};
/** @} */
}
#endif //HIDRA2__COMMON_NETWORKING_H
#ifndef HIDRA2_SYSTEM_WRAPPERS__HAS_IO_H
#define HIDRA2_SYSTEM_WRAPPERS__HAS_IO_H
#include "io.h"
namespace hidra2 {
class HasIO {
protected:
static IO* const kDefaultIO;
IO* io;
HasIO();
public:
void __set_io(IO* io);
};
}
#endif //HIDRA2_SYSTEM_WRAPPERS__HAS_IO_H
#ifndef HIDRA2_SYSTEM_WRAPPERS__IO_H
#define HIDRA2_SYSTEM_WRAPPERS__IO_H
#include <unistd.h>
namespace hidra2 {
class IO {
public:
virtual int open(const char *__file, int __oflag) = 0;
virtual int close(int __fd) = 0;
virtual ssize_t read(int __fd, void *buf, size_t count) = 0;
virtual ssize_t write(int __fd, const void *__buf, size_t __n) = 0;
public:
virtual int open(const char *__file, int __oflag) = 0;
virtual int close(int __fd) = 0;
virtual ssize_t read(int __fd, void *buf, size_t count) = 0;
virtual ssize_t recv(int __fd, void *__buf, size_t __n, int __flags) = 0;
virtual ssize_t write(int __fd, const void *__buf, size_t __n) = 0;
virtual ssize_t send(int __fd, const void *__buf, size_t __n, int __flags) = 0;
};
}
......
......@@ -4,12 +4,15 @@
#include "io.h"
namespace hidra2 {
class SystemIO : public IO {
class SystemIO final : public IO {
public:
int open(const char *__file, int __oflag) final;
int close(int __fd) final;
ssize_t read(int __fd, void *buf, size_t count) final;
ssize_t write(int __fd, const void *__buf, size_t __n) final;
int open(const char *__file, int __oflag);
int close(int __fd);
ssize_t read(int __fd, void *buf, size_t count);
ssize_t recv(int __fd, void *__buf, size_t __n, int __flags);
ssize_t write(int __fd, const void *__buf, size_t __n);
ssize_t send(int __fd, const void *__buf, size_t __n, int __flags);
};
}
......
#include "system_wrappers/system_io.h"
#include "system_wrappers/has_io.h"
hidra2::IO* const hidra2::HasIO::kDefaultIO = new hidra2::SystemIO();
hidra2::HasIO::HasIO() {
io = kDefaultIO;
}
void hidra2::HasIO::__set_io(hidra2::IO* io) {
this->io = io;
}
#include <fcntl.h>
#include <unistd.h>
#include <system_wrappers/system_io.h>
#include <sys/socket.h>
#include "system_wrappers/system_io.h"
int hidra2::SystemIO::open(const char *__file, int __oflag) {
return ::open(__file, __oflag);
return ::open(__file, __oflag);
}
int hidra2::SystemIO::close(int __fd) {
return ::close(__fd);
return ::close(__fd);
}
ssize_t hidra2::SystemIO::read(int __fd, void *buf, size_t count) {
return ::read(__fd, buf, count);
return ::read(__fd, buf, count);
}
ssize_t hidra2::SystemIO::recv(int __fd, void *__buf, size_t __n, int __flags) {
return ::recv(__fd, __buf, __n, __flags);
}
ssize_t hidra2::SystemIO::write(int __fd, const void *__buf, size_t __n) {
return ::write(__fd, __buf, __n);
return ::write(__fd, __buf, __n);
}
ssize_t hidra2::SystemIO::send(int __fd, const void* __buf, size_t __n, int __flags) {
return ::send(__fd, __buf, __n, __flags);
}
set(TARGET_NAME producer-api)
set(SOURCE_FILES src/producer/producer.cpp include/producer/producer.h)
set(SOURCE_FILES src/producer.cpp include/producer/producer.h src/producer_impl.cpp src/producer_impl.h)
################################
......
......@@ -2,19 +2,43 @@
#define HIDRA2__PRODUCER_PRODUCER_H
#include <string>
#include <memory>
#include <common/networking.h>
#include <system_wrappers/io.h>
#include <system_wrappers/has_io.h>
namespace hidra2 {
class Producer {
private:
static unsigned long kinit_count_;
Producer();
public:
static const uint32_t VERSION;
enum ProducerError {
PRODUCER_ERROR__OK,
PRODUCER_ERROR__CONNECTION_NOT_READY,
PRODUCER_ERROR__CHUNK_PROVIDER_NOT_READY_AT_START,
};
enum ProducerStatus {
PRODUCER_STATUS__DISCONNECTED,
PRODUCER_STATUS__CONNECTING,
PRODUCER_STATUS__CONNECTED,
PRODUCER_STATUS__SENDING,
PRODUCER_STATUS__ERROR,
};
struct FileChunk {
void *ptr;
uint64_t start_byte;
uint64_t chunk_size;
};
class Producer : public HasIO {
public:
static std::unique_ptr<Producer> create();
//virtual ~Producer() = 0;
Producer(const Producer&) = delete;
Producer& operator=(const Producer&) = delete;
virtual uint64_t get_version() const = 0;
virtual ProducerStatus get_status() const = 0;
static Producer* CreateProducer(std::string receiver_address);
virtual ProducerError connect_to_receiver(std::string receiver_address) = 0;
virtual ProducerError send(std::string filename, uint64_t file_size, void *data) = 0;
};
}
......
......@@ -4,3 +4,4 @@
std::unique_ptr<hidra2::Producer> hidra2::Producer::create() {
return std::unique_ptr<hidra2::Producer>(new ProducerImpl());
}
#include <system_wrappers/system_io.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include "producer_impl.h"
const uint32_t hidra2::ProducerImpl::kVersion = 1;
const hidra2::IO *hidra2::ProducerImpl::kDefaultIO = new hidra2::SystemIO();
hidra2::FileReferenceId hidra2::ProducerImpl::kGlobalReferenceId = 0;
......@@ -10,10 +14,6 @@ hidra2::ProducerImpl::ProducerImpl() {
__set_io(ProducerImpl::kDefaultIO);
}
void hidra2::ProducerImpl::__set_io(hidra2::IO *io) {
this->io = io;
}
uint64_t hidra2::ProducerImpl::get_version() const {
return kVersion;
}
......@@ -23,6 +23,21 @@ hidra2::ProducerStatus hidra2::ProducerImpl::get_status() const {
}
hidra2::ProducerError hidra2::ProducerImpl::connect_to_receiver(std::string receiver_address) {
int client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
sockaddr_in socket_address {};
socket_address.sin_addr.s_addr = inet_addr(receiver_address.c_str());
socket_address.sin_port = htons(8099);
socket_address.sin_family = AF_INET;
connect(client_fd, (struct sockaddr *)&socket_address, sizeof(socket_address));
void* a = malloc(1024*1024);
while(true) {
recv(client_fd, a, 1024*1024, 0);
}
return PRODUCER_ERROR__OK;
}
......
......@@ -2,31 +2,25 @@
#define HIDRA2_PRODUCER__PRODUCERIMPL_H
#include <string>
#include <system_wrappers/has_io.h>
#include "producer/producer.h"
namespace hidra2 {
class ProducerImpl : public Producer {
friend Producer;
private:
static const uint32_t kVersion;
static FileReferenceId kGlobalReferenceId;
static IO* const kDefaultIO;
IO* io;
ProducerImpl() = default;
public:
ProducerImpl(const ProducerImpl &) = delete;
ProducerImpl &operator=(const ProducerImpl &) = delete;
//~ProducerImpl() override;
void __set_io(IO* io) override;
uint64_t get_version() const override;
ProducerStatus get_status() const override;
ProducerError connect_to_receiver(std::string receiver_address) override;
ProducerError send(std::string filename, uint64_t file_size, void* data) override;
friend Producer;
private:
static const uint32_t kVersion;
static FileReferenceId kGlobalReferenceId;
public:
ProducerImpl();
ProducerImpl(const ProducerImpl &) = delete;
ProducerImpl &operator=(const ProducerImpl &) = delete;
//~ProducerImpl() override;
uint64_t get_version() const override;
ProducerStatus get_status() const override;
ProducerError connect_to_receiver(std::string receiver_address) override;
ProducerError send(std::string filename, uint64_t file_size, void* data) override;
};
}
......
......@@ -13,9 +13,6 @@ target_include_directories(${TARGET_NAME} PRIVATE include)
target_link_libraries(${TARGET_NAME} pthread common producer-api)
set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
astyle(${TARGET_NAME} "${SOURCE_FILES}")
################################
# Testing
################################
......
......@@ -3,23 +3,22 @@
int DummyDetector::main(int argc, char **argv) {
std::unique_ptr<hidra2::Producer> producer = hidra2::Producer::create();
producer->connect_to_receiver("127.0.0.1");
std::unique_ptr<hidra2::Producer> producer = hidra2::Producer::create();
producer->connect_to_receiver("127.0.0.1");
/*
const size_t size = 1024 * 20;
void *buffer = malloc(size);
const size_t size = 1024 * 20;
void *buffer = malloc(size);
hidra2::ProducerError error;
error = producer->send("testfile", size, buffer);
hidra2::ProducerError error;
error = producer->send("testfile", size, buffer);
if(error) {
std::cerr << "File was not successfully send, ErrorCode: " << error << std::endl;
} else {
std::cout << "File was successfully send." << std::endl;
}
if(error) {
std::cerr << "File was not successfully send, ErrorCode: " << error << std::endl;
}
else {
std::cout << "File was successfully send." << std::endl;
}
free(buffer);
return 0;
free(buffer);
*/
return 0;
}
......@@ -4,10 +4,9 @@
#include <common/networking.h>
#include <producer/producer.h>
class DummyDetector
{
class DummyDetector {
public:
public:
int main(int argc, char* argv[]);
};
......
#include "dummy_detector.h"
int main (int argc, char* argv[])
{
int main (int argc, char* argv[]) {
return DummyDetector().main(argc, argv);
}
#include <gtest/gtest.h>
namespace {
TEST(EMPTY, REMOVEME) {
EXPECT_EQ(1, 1);
}
TEST(EMPTY, REMOVEME) {
EXPECT_EQ(1, 1);
}
}
......@@ -7,21 +7,15 @@ set(SOURCE_FILES
################################
# Library
################################
add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
target_include_directories(${TARGET_NAME} PUBLIC include)
target_link_libraries(${TARGET_NAME} common)
add_executable(${TARGET_NAME} ${SOURCE_FILES})
#target_include_directories(${TARGET_NAME} PUBLIC include)
target_link_libraries(${TARGET_NAME} common pthread)
set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
astyle(${TARGET_NAME} "${SOURCE_FILES}")
################################
# Testing
################################
set(TEST_SOURCE_FILES unittests/test_receiver.cpp)
set(TEST_LIBRARIES producer-api)
gtest(${TARGET_NAME} ${TEST_SOURCE_FILES} ${TEST_LIBRARIES})
#set(TEST_SOURCE_FILES unittests/test_receiver.cpp)
#set(TEST_LIBRARIES producer-api)
#gtest(${TARGET_NAME} ${TEST_SOURCE_FILES} ${TEST_LIBRARIES})
#include "receiver.h"
int main (int argc, char* argv[]) {
hidra2::Receiver* receiver = new hidra2::Receiver();
receiver->start_listener("127.0.0.1", 8099);
getchar();
receiver->stop_listener();
getchar();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment