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

Refactored error class

parent 1b99ceae
No related branches found
No related tags found
No related merge requests found
...@@ -31,6 +31,7 @@ class ErrorInterface { ...@@ -31,6 +31,7 @@ class ErrorInterface {
virtual void Append(const std::string& value) noexcept = 0; virtual void Append(const std::string& value) noexcept = 0;
virtual ErrorType GetErrorType() const noexcept = 0; virtual ErrorType GetErrorType() const noexcept = 0;
virtual ~ErrorInterface() = default; // needed for unique_ptr to delete itself virtual ~ErrorInterface() = default; // needed for unique_ptr to delete itself
/*TODO: Add these functions, so it will be really easy and convenient to use the error class /*TODO: Add these functions, so it will be really easy and convenient to use the error class
* virtual inline bool operator == (const Error& rhs) const * virtual inline bool operator == (const Error& rhs) const
* virtual inline bool operator != (const Error& rhs) const * virtual inline bool operator != (const Error& rhs) const
...@@ -41,6 +42,7 @@ class ErrorInterface { ...@@ -41,6 +42,7 @@ class ErrorInterface {
*/ */
}; };
class ErrorTemplateInterface { class ErrorTemplateInterface {
public: public:
virtual ErrorType GetErrorType() const noexcept = 0; virtual ErrorType GetErrorType() const noexcept = 0;
...@@ -49,6 +51,7 @@ class ErrorTemplateInterface { ...@@ -49,6 +51,7 @@ class ErrorTemplateInterface {
* virtual inline bool operator ErrorTemplateInterface() const * virtual inline bool operator ErrorTemplateInterface() const
*/ */
/*
virtual inline bool operator == (const Error* rhs) const { virtual inline bool operator == (const Error* rhs) const {
return rhs != nullptr && return rhs != nullptr &&
operator==(*rhs); operator==(*rhs);
...@@ -58,6 +61,7 @@ class ErrorTemplateInterface { ...@@ -58,6 +61,7 @@ class ErrorTemplateInterface {
return rhs != nullptr && return rhs != nullptr &&
operator!=(*rhs); operator!=(*rhs);
} }
*/
virtual inline bool operator == (const Error& rhs) const { virtual inline bool operator == (const Error& rhs) const {
return rhs != nullptr && return rhs != nullptr &&
...@@ -77,6 +81,12 @@ static inline bool operator != (const Error& lhs, const ErrorTemplateInterface& ...@@ -77,6 +81,12 @@ static inline bool operator != (const Error& lhs, const ErrorTemplateInterface&
return rhs.operator != (lhs); return rhs.operator != (lhs);
} }
static inline std::ostream& operator<<(std::ostream& os, const Error& err) {
os << err->Explain();
return os;
}
class SimpleError: public ErrorInterface { class SimpleError: public ErrorInterface {
private: private:
std::string error_; std::string error_;
......
...@@ -2,6 +2,7 @@ set(TARGET_NAME receiver) ...@@ -2,6 +2,7 @@ set(TARGET_NAME receiver)
set(SOURCE_FILES set(SOURCE_FILES
src/receiver.h src/receiver.cpp src/receiver.h src/receiver.cpp
src/network_producer_peer.h src/network_producer_peer.cpp src/network_producer_peer_handlers.cpp src/network_producer_peer.h src/network_producer_peer.cpp src/network_producer_peer_handlers.cpp
src/receiver_error.h
) )
......
...@@ -10,7 +10,7 @@ int main (int argc, char* argv[]) { ...@@ -10,7 +10,7 @@ int main (int argc, char* argv[]) {
receiver->StartListener(address, &err); receiver->StartListener(address, &err);
if(err) { if(err) {
std::cerr << "Fail to start receiver: " << err->Explain() << std::endl; std::cerr << "Fail to start receiver: " << err << std::endl;
return 1; return 1;
} }
std::cout << "StartListener on " << address << std::endl; std::cout << "StartListener on " << address << std::endl;
...@@ -21,7 +21,7 @@ int main (int argc, char* argv[]) { ...@@ -21,7 +21,7 @@ int main (int argc, char* argv[]) {
std::cout << "Stop listener..." << std::endl; std::cout << "Stop listener..." << std::endl;
receiver->StopListener(&err);//TODO might not work since Accept is a blocking call :/ receiver->StopListener(&err);//TODO might not work since Accept is a blocking call :/
if(err) { if(err) {
std::cerr << "Fail to stop receiver: " << err->Explain() << std::endl; std::cerr << "Fail to stop receiver: " << err << std::endl;
return 1; return 1;
} }
......
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include "receiver.h" #include "receiver.h"
#include "network_producer_peer.h" #include "receiver_error.h"
const int hidra2::Receiver::kMaxUnacceptedConnectionsBacklog = 5; const int hidra2::Receiver::kMaxUnacceptedConnectionsBacklog = 5;
...@@ -48,10 +48,10 @@ void hidra2::Receiver::AcceptThreadLogic() { ...@@ -48,10 +48,10 @@ void hidra2::Receiver::AcceptThreadLogic() {
std::string address; std::string address;
FileDescriptor peer_fd; FileDescriptor peer_fd;
Error io_error; Error err;
auto client_info_tuple = io->InetAccept(listener_fd_, &io_error); auto client_info_tuple = io->InetAccept(listener_fd_, &err);
if(io_error) { if(err) {
std::cerr << "An error occurred while accepting an incoming connection" << std::endl; std::cerr << "An error occurred while accepting an incoming connection: " << err << std::endl;
return; return;
} }
std::tie(address, peer_fd) = *client_info_tuple; std::tie(address, peer_fd) = *client_info_tuple;
......
...@@ -9,51 +9,6 @@ ...@@ -9,51 +9,6 @@
namespace hidra2 { namespace hidra2 {
enum class ReceiverErrorType {
kAlreadyListening,
};
class ReceiverError : public SimpleError {
private:
ReceiverErrorType receiver_error_type_;
public:
ReceiverError(const std::string& error, ReceiverErrorType receiver_error_type) : SimpleError(error,
ErrorType::kReceiverError) {
receiver_error_type_ = receiver_error_type;
}
ReceiverErrorType GetReceiverErrorType() const noexcept {
return receiver_error_type_;
}
};
class ReceiverErrorTemplate : public SimpleErrorTemplate {
protected:
ReceiverErrorType receiver_error_type_;
public:
ReceiverErrorTemplate(const std::string& error, ReceiverErrorType receiver_error_type) : SimpleErrorTemplate(error,
ErrorType::kIOError) {
receiver_error_type_ = receiver_error_type;
}
inline ReceiverErrorType GetReceiverErrorType() const noexcept {
return receiver_error_type_;
}
inline Error Generate() const noexcept override {
return Error(new ReceiverError(error_, receiver_error_type_));
}
inline bool operator == (const Error& rhs) const override {
return SimpleErrorTemplate::operator==(rhs)
&& GetReceiverErrorType() == ((ReceiverError*)rhs.get())->GetReceiverErrorType();
}
};
namespace ReceiverErrorTemplates {
auto const kAlreadyListening = ReceiverErrorTemplate{"Receiver is already listening", ReceiverErrorType::kAlreadyListening};
};
class Receiver : public HasIO { class Receiver : public HasIO {
friend NetworkProducerPeer; friend NetworkProducerPeer;
private: private:
......
#ifndef HIDRA2_RECEIVER_ERROR_H
#define HIDRA2_RECEIVER_ERROR_H
#include "common/error.h"
namespace hidra2 {
enum class ReceiverErrorType {
kAlreadyListening,
};
class ReceiverError : public SimpleError {
private:
ReceiverErrorType receiver_error_type_;
public:
ReceiverError(const std::string& error, ReceiverErrorType receiver_error_type) : SimpleError(error,
ErrorType::kReceiverError) {
receiver_error_type_ = receiver_error_type;
}
ReceiverErrorType GetReceiverErrorType() const noexcept {
return receiver_error_type_;
}
};
class ReceiverErrorTemplate : public SimpleErrorTemplate {
protected:
ReceiverErrorType receiver_error_type_;
public:
ReceiverErrorTemplate(const std::string& error, ReceiverErrorType receiver_error_type) : SimpleErrorTemplate(error,
ErrorType::kIOError) {
receiver_error_type_ = receiver_error_type;
}
inline ReceiverErrorType GetReceiverErrorType() const noexcept {
return receiver_error_type_;
}
inline Error Generate() const noexcept override {
return Error(new ReceiverError(error_, receiver_error_type_));
}
inline bool operator==(const Error& rhs) const override {
return SimpleErrorTemplate::operator==(rhs)
&& GetReceiverErrorType() == ((ReceiverError*) rhs.get())->GetReceiverErrorType();
}
};
namespace ReceiverErrorTemplates {
auto const
kAlreadyListening = ReceiverErrorTemplate{"Receiver is already listening", ReceiverErrorType::kAlreadyListening};
};
}
#endif //HIDRA2_RECEIVER_ERROR_H
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