--- /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;
+}
+
+
+}
+}
+}