author | František Kučera <franta-hg@frantovo.cz> |
Fri, 29 Jul 2022 18:03:49 +0200 | |
branch | v_0 |
changeset 4 | 8d036e5e5fcc |
parent 3 | e701e06ff561 |
child 5 | e57e2a2798b2 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
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 | 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 | 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> |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
27 |
#include <regex> |
2 | 28 |
|
29 |
#include "Socket.h" |
|
0 | 30 |
|
31 |
namespace relpipe { |
|
32 |
namespace out { |
|
33 |
namespace socket { |
|
34 |
||
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
35 |
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
|
36 |
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
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
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
|
41 |
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
|
42 |
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
|
43 |
|
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
44 |
class FD { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
45 |
private: |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
46 |
int fd; |
0 | 47 |
public: |
48 |
||
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
49 |
FD(int fd) : fd(fd) { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
50 |
}; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
51 |
|
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
52 |
virtual ~FD() { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
53 |
close(fd); |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
54 |
} |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
55 |
|
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
56 |
int getFD() { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
57 |
return fd; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
58 |
} |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
59 |
}; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
60 |
|
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
61 |
/** |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
62 |
* @param connectionString |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
63 |
* @return protocol, hostname or path, port (optional) |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
64 |
*/ |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
65 |
static std::tuple<std::string, std::string, uint16_t> parseConnectionString(const std::string& connectionString) { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
66 |
// TODO: support „:“ in domain socket paths? |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
67 |
std::regex pattern("([^:]+)://([^:]+)(:([0-9]+))?"); |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
68 |
|
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
69 |
} |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
70 |
|
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
71 |
class UDPSocket : public Socket { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
72 |
private: |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
73 |
struct sockaddr_in remoteAddress; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
74 |
public: |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
75 |
|
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
76 |
static std::shared_ptr<Socket> open(const std::string& connectionString) { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
77 |
std::shared_ptr<UDPSocket> s = std::make_shared<UDPSocket>(); |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
78 |
memset((char *) &s->remoteAddress, 0, sizeof (s->remoteAddress)); |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
79 |
s->remoteAddress.sin_family = AF_INET; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
80 |
s->remoteAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255) |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
81 |
s->remoteAddress.sin_port = htons(1234); |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
82 |
return s; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
83 |
} |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
84 |
|
2 | 85 |
void send(const std::string& message) override { |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
86 |
FD s(::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)); |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
87 |
sendto(s.getFD(), message.c_str(), message.size(), 0, (sockaddr*) & remoteAddress, sizeof (remoteAddress)); |
2 | 88 |
} |
0 | 89 |
|
2 | 90 |
const std::string receive() override { |
91 |
// TODO: TCP receive() |
|
92 |
return "TODO: receive() a message"; |
|
0 | 93 |
} |
94 |
||
95 |
}; |
|
96 |
||
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
97 |
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
|
98 |
class TemplateSocketFactory : public SocketFactory { |
2 | 99 |
public: |
100 |
||
101 |
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
|
102 |
return connectionString.rfind(protocol, 0) == 0; |
2 | 103 |
} |
104 |
||
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
105 |
std::shared_ptr<Socket> open(const std::string& connectionString) override { |
2 | 106 |
// 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
|
107 |
// TODO: return shared_ptr? |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
108 |
return SocketClass::open(connectionString); |
2 | 109 |
} |
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
110 |
}; |
2 | 111 |
|
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
112 |
static std::vector<std::shared_ptr<SocketFactory>> factories |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
113 |
{ |
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
114 |
// FIXME: different classes than TCPSocket |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
115 |
std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, UDPSocket >> (), |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
116 |
std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, UDPSocket >> (), |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
117 |
std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, UDPSocket >> (), |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
118 |
std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, UDPSocket >> (), |
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
119 |
}; |
2 | 120 |
|
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
121 |
std::shared_ptr<SocketFactory> SocketFactory::find(const std::string& connectionString) { |
2 | 122 |
for (auto f : factories) if (f->canHandle(connectionString)) return f; |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
123 |
throw std::logic_error("Unable to find a SocketFactory for connection string: " + connectionString); |
2 | 124 |
} |
125 |
||
126 |
||
0 | 127 |
} |
128 |
} |
|
129 |
} |