src/Socket.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 28 Jul 2022 02:45:11 +0200
branchv_0
changeset 3 e701e06ff561
parent 2 fb399fd4f053
child 4 8d036e5e5fcc
permissions -rw-r--r--
add some factories: generic/template version

/**
 * 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 "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 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";
	}

};

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

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

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

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>>(),
};

std::shared_ptr<SocketFactory> SocketFactory::find(const std::string& connectionString) {
	for (auto f : factories) if (f->canHandle(connectionString)) return f;
	return std::shared_ptr<SocketFactory>();
}


}
}
}