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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <string>
1
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    18
#include <cstring>
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    19
#include <unistd.h>
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <stdexcept>
1
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    21
#include <arpa/inet.h>
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    22
#include <sys/types.h>
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    23
#include <sys/socket.h>
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    24
#include <netinet/in.h>
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    25
#include <vector>
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    26
#include <memory>
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    27
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    28
#include "Socket.h"
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace relpipe {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
namespace out {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace socket {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    34
static const char PROTOCOL_TCP[] = "tcp://";
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    35
static const char PROTOCOL_UDP[] = "udp://";
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    36
static const char PROTOCOL_UDS[] = "uds://";
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    37
static const char PROTOCOL_SCTP[] = "sctp://";
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    38
static const char PROTOCOL_TCP_LISTEN[] = "tcp-listen://";
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    39
static const char PROTOCOL_UDP_LISTEN[] = "udp-listen://";
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    40
static const char PROTOCOL_UDS_LISTEN[] = "uds-listen://";
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    41
static const char PROTOCOL_SCTP_LISTEN[] = "sctp-listen://";
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    42
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    43
class TCPSocket : public Socket {
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
public:
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    46
	void send(const std::string& message) override {
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    47
		// TODO: TCP send()
1
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
		struct sockaddr_in a;
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
		memset((char *) &a, 0, sizeof (a));
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
		a.sin_family = AF_INET;
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
		a.sin_addr.s_addr = inet_addr("127.0.0.1"); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
		a.sin_port = htons(1234);
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
1
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
		int s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
		sendto(s, message.c_str(), message.size(), 0, (sockaddr*) & a, sizeof (a));
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    56
1
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
		close(s);
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    58
	}
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    60
	const std::string receive() override {
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    61
		// TODO: TCP receive()
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    62
		return "TODO: receive() a message";
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
};
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    67
template<const char* protocol, typename SocketClass>
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    68
class TemplateSocketFactory : public SocketFactory {
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    69
public:
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    70
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    71
	bool canHandle(const std::string& connectionString) override {
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    72
		return connectionString.rfind(protocol, 0) == 0;
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    73
	}
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    74
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    75
	Socket* open(const std::string& connectionString) override {
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    76
		// TODO: pass string to constructor
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    77
		// TODO: return shared_ptr?
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    78
		return new SocketClass();
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    79
	}
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    80
};
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    81
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    82
static std::vector<std::shared_ptr<SocketFactory>> factories{
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    83
	// FIXME: different classes than TCPSocket
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    84
	std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, TCPSocket>>(),
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    85
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, TCPSocket>>(),
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    86
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, TCPSocket>>(),
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    87
	std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, TCPSocket>>(),
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    88
};
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    89
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    90
std::shared_ptr<SocketFactory> SocketFactory::find(const std::string& connectionString) {
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    91
	for (auto f : factories) if (f->canHandle(connectionString)) return f;
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    92
	return std::shared_ptr<SocketFactory>();
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    93
}
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    94
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    95
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
}