src/Socket.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 29 Jul 2022 18:03:49 +0200
branchv_0
changeset 4 8d036e5e5fcc
parent 3 e701e06ff561
child 5 e57e2a2798b2
permissions -rw-r--r--
configuration: --connection-string --connection-option (role, mode)

/**
 * 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 <memory>
#include <regex>

#include "Socket.h"

namespace relpipe {
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 FD {
private:
	int fd;
public:

	FD(int fd) : fd(fd) {
	};

	virtual ~FD() {
		close(fd);
	}

	int getFD() {
		return fd;
	}
};

/**
 * @param connectionString
 * @return protocol, hostname or path, port (optional)
 */
static std::tuple<std::string, std::string, uint16_t> parseConnectionString(const std::string& connectionString) {
	// TODO: support „:“ in domain socket paths?
	std::regex pattern("([^:]+)://([^:]+)(:([0-9]+))?");
	
}

class UDPSocket : public Socket {
private:
	struct sockaddr_in remoteAddress;
public:

	static std::shared_ptr<Socket> open(const std::string& connectionString) {
		std::shared_ptr<UDPSocket> s = std::make_shared<UDPSocket>();
		memset((char *) &s->remoteAddress, 0, sizeof (s->remoteAddress));
		s->remoteAddress.sin_family = AF_INET;
		s->remoteAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
		s->remoteAddress.sin_port = htons(1234);
		return s;
	}

	void send(const std::string& message) override {
		FD s(::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
		sendto(s.getFD(), message.c_str(), message.size(), 0, (sockaddr*) & remoteAddress, sizeof (remoteAddress));
	}

	const std::string receive() override {
		// TODO: TCP receive()
		return "TODO: receive() a message";
	}

};

template<const char* protocol, typename SocketClass>
class TemplateSocketFactory : public SocketFactory {
public:

	bool canHandle(const std::string& connectionString) override {
		return connectionString.rfind(protocol, 0) == 0;
	}

	std::shared_ptr<Socket> open(const std::string& connectionString) override {
		// TODO: pass string to constructor
		// TODO: return shared_ptr?
		return SocketClass::open(connectionString);
	}
};

static std::vector<std::shared_ptr<SocketFactory>> factories
{
	// FIXME: different classes than TCPSocket
	std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, UDPSocket >> (),
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, UDPSocket >> (),
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, UDPSocket >> (),
	std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, UDPSocket >> (),
};

std::shared_ptr<SocketFactory> SocketFactory::find(const std::string& connectionString) {
	for (auto f : factories) if (f->canHandle(connectionString)) return f;
	throw std::logic_error("Unable to find a SocketFactory for connection string: " + connectionString);
}


}
}
}