add some factories v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 28 Jul 2022 02:03:11 +0200
branchv_0
changeset 2 fb399fd4f053
parent 1 e3265afd1111
child 3 e701e06ff561
add some factories
nbproject/configurations.xml
src/CMakeLists.txt
src/Socket.cpp
src/Socket.h
src/SocketHandler.h
--- a/nbproject/configurations.xml	Tue Jul 26 23:22:18 2022 +0200
+++ b/nbproject/configurations.xml	Thu Jul 28 02:03:11 2022 +0200
@@ -42,6 +42,7 @@
   <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
     <df root="." name="0">
       <df name="src">
+        <in>Socket.cpp</in>
         <in>relpipe-out-socket.cpp</in>
       </df>
     </df>
@@ -92,6 +93,10 @@
           <preBuildFirst>true</preBuildFirst>
         </preBuild>
       </makefileType>
+      <item path="src/Socket.cpp" ex="false" tool="1" flavor2="0">
+        <ccTool flags="0">
+        </ccTool>
+      </item>
       <item path="src/relpipe-out-socket.cpp" ex="false" tool="1" flavor2="0">
         <ccTool flags="0">
         </ccTool>
--- a/src/CMakeLists.txt	Tue Jul 26 23:22:18 2022 +0200
+++ b/src/CMakeLists.txt	Thu Jul 28 02:03:11 2022 +0200
@@ -29,7 +29,7 @@
 # Executable output:
 add_executable(
 	${EXECUTABLE_FILE}
-	SocketHandler.h
+	Socket.cpp
 	relpipe-out-socket.cpp
 )
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Socket.cpp	Thu Jul 28 02:03:11 2022 +0200
@@ -0,0 +1,80 @@
+/**
+ * Relational pipes
+ * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string>
+#include <cstring>
+#include <unistd.h>
+#include <stdexcept>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <vector>
+
+#include "Socket.h"
+
+namespace relpipe {
+namespace out {
+namespace socket {
+
+class TCPSocket : public Socket {
+public:
+
+	void send(const std::string& message) override {
+		// TODO: TCP send()
+		struct sockaddr_in a;
+		memset((char *) &a, 0, sizeof (a));
+		a.sin_family = AF_INET;
+		a.sin_addr.s_addr = inet_addr("127.0.0.1"); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
+		a.sin_port = htons(1234);
+
+		int s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+		sendto(s, message.c_str(), message.size(), 0, (sockaddr*) & a, sizeof (a));
+
+		close(s);
+	}
+
+	const std::string receive() override {
+		// TODO: TCP receive()
+		return "TODO: receive() a message";
+	}
+
+};
+
+static class TCPSocketFactory : public SocketFactory {
+public:
+
+	bool canHandle(const std::string& connectionString) override {
+		return connectionString.rfind("tcp://", 0) == 0;
+	}
+
+	Socket* open(const std::string& connectionString) override {
+		// TODO: pass string to constructor
+		return new TCPSocket();
+	}
+} tcpSocketFactory;
+
+static std::vector<SocketFactory*> factories{&tcpSocketFactory};
+
+SocketFactory* SocketFactory::find(const std::string& connectionString) {
+	for (auto f : factories) if (f->canHandle(connectionString)) return f;
+	return nullptr;
+}
+
+
+}
+}
+}
--- a/src/Socket.h	Tue Jul 26 23:22:18 2022 +0200
+++ b/src/Socket.h	Thu Jul 28 02:03:11 2022 +0200
@@ -30,31 +30,22 @@
 namespace socket {
 
 class Socket {
-private:
 public:
-
-	virtual ~Socket() {
-	}
-
-	void send(const std::string& message) {
+	virtual ~Socket() = default;
+	// virtual void setOption(const std::string& uri, const std::string& value) = 0;
+	virtual void send(const std::string& message) = 0;
+	virtual const std::string receive() = 0;
+};
 
-		struct sockaddr_in a;
-		memset((char *) &a, 0, sizeof (a));
-		a.sin_family = AF_INET;
-		a.sin_addr.s_addr = inet_addr("127.0.0.1"); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
-		a.sin_port = htons(1234);
+class SocketFactory {
+public:
+	virtual ~SocketFactory() = default;
+	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);
+};
 
-		int s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
-		sendto(s, message.c_str(), message.size(), 0, (sockaddr*) & a, sizeof (a));
-		
-		close(s);
-
-		// TODO: send message
-	}
-	
-	// virtual const std::string receive();
-
-};
 
 }
 }
--- a/src/SocketHandler.h	Tue Jul 26 23:22:18 2022 +0200
+++ b/src/SocketHandler.h	Thu Jul 28 02:03:11 2022 +0200
@@ -54,7 +54,8 @@
 
 	SocketHandler(Configuration configuration) : configuration(configuration) {
 		// TODO: do not throw exception from the constructor: Socket::open()
-		socket = std::make_shared<Socket>(); // TODO: create a TCP, UDP… socket
+		// 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
 	}
 
 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {