--- a/src/Socket.cpp Thu Jul 28 02:03:11 2022 +0200
+++ b/src/Socket.cpp Thu Jul 28 02:45:11 2022 +0200
@@ -23,6 +23,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <vector>
+#include <memory>
#include "Socket.h"
@@ -30,6 +31,15 @@
namespace out {
namespace socket {
+static const char PROTOCOL_TCP[] = "tcp://";
+static const char PROTOCOL_UDP[] = "udp://";
+static const char PROTOCOL_UDS[] = "uds://";
+static const char PROTOCOL_SCTP[] = "sctp://";
+static const char PROTOCOL_TCP_LISTEN[] = "tcp-listen://";
+static const char PROTOCOL_UDP_LISTEN[] = "udp-listen://";
+static const char PROTOCOL_UDS_LISTEN[] = "uds-listen://";
+static const char PROTOCOL_SCTP_LISTEN[] = "sctp-listen://";
+
class TCPSocket : public Socket {
public:
@@ -54,24 +64,32 @@
};
-static class TCPSocketFactory : public SocketFactory {
+template<const char* protocol, typename SocketClass>
+class TemplateSocketFactory : public SocketFactory {
public:
bool canHandle(const std::string& connectionString) override {
- return connectionString.rfind("tcp://", 0) == 0;
+ return connectionString.rfind(protocol, 0) == 0;
}
Socket* open(const std::string& connectionString) override {
// TODO: pass string to constructor
- return new TCPSocket();
+ // TODO: return shared_ptr?
+ return new SocketClass();
}
-} tcpSocketFactory;
+};
-static std::vector<SocketFactory*> factories{&tcpSocketFactory};
+static std::vector<std::shared_ptr<SocketFactory>> factories{
+ // FIXME: different classes than TCPSocket
+ std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, TCPSocket>>(),
+ std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, TCPSocket>>(),
+ std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, TCPSocket>>(),
+ std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, TCPSocket>>(),
+};
-SocketFactory* SocketFactory::find(const std::string& connectionString) {
+std::shared_ptr<SocketFactory> SocketFactory::find(const std::string& connectionString) {
for (auto f : factories) if (f->canHandle(connectionString)) return f;
- return nullptr;
+ return std::shared_ptr<SocketFactory>();
}
--- a/src/Socket.h Thu Jul 28 02:03:11 2022 +0200
+++ b/src/Socket.h Thu Jul 28 02:45:11 2022 +0200
@@ -43,7 +43,7 @@
virtual bool canHandle(const std::string& connectionString) = 0;
// virtual void setOption(const std::string& uri, const std::string& value) = 0;
virtual Socket* open(const std::string& connectionString) = 0;
- static SocketFactory* find(const std::string& connectionString);
+ static std::shared_ptr<SocketFactory> find(const std::string& connectionString);
};
--- a/src/SocketHandler.h Thu Jul 28 02:03:11 2022 +0200
+++ b/src/SocketHandler.h Thu Jul 28 02:45:11 2022 +0200
@@ -41,7 +41,7 @@
private:
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
Configuration configuration;
- shared_ptr<Socket> socket;
+ std::shared_ptr<Socket> socket;
struct CurrentRelation {
relpipe::common::type::StringX name;
@@ -55,7 +55,7 @@
SocketHandler(Configuration configuration) : configuration(configuration) {
// TODO: do not throw exception from the constructor: Socket::open()
// socket = std::make_shared<TCPSocket>(); // TODO: create a TCP, UDP… socket
- socket.reset(SocketFactory::find("tcp://TODO:connectionString")->open("tcp://TODO:connectionString2")); // TODO: connection string
+ socket.reset(SocketFactory::find("tcp://TODO:connectionString")->open("tcp://TODO:connectionString2")); // TODO: connection string + check nullptr
}
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {