src/Socket.cpp
branchv_0
changeset 2 fb399fd4f053
parent 1 e3265afd1111
child 3 e701e06ff561
equal deleted inserted replaced
1:e3265afd1111 2:fb399fd4f053
       
     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>
       
    18 #include <cstring>
       
    19 #include <unistd.h>
       
    20 #include <stdexcept>
       
    21 #include <arpa/inet.h>
       
    22 #include <sys/types.h>
       
    23 #include <sys/socket.h>
       
    24 #include <netinet/in.h>
       
    25 #include <vector>
       
    26 
       
    27 #include "Socket.h"
       
    28 
       
    29 namespace relpipe {
       
    30 namespace out {
       
    31 namespace socket {
       
    32 
       
    33 class TCPSocket : public Socket {
       
    34 public:
       
    35 
       
    36 	void send(const std::string& message) override {
       
    37 		// TODO: TCP send()
       
    38 		struct sockaddr_in a;
       
    39 		memset((char *) &a, 0, sizeof (a));
       
    40 		a.sin_family = AF_INET;
       
    41 		a.sin_addr.s_addr = inet_addr("127.0.0.1"); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
       
    42 		a.sin_port = htons(1234);
       
    43 
       
    44 		int s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
       
    45 		sendto(s, message.c_str(), message.size(), 0, (sockaddr*) & a, sizeof (a));
       
    46 
       
    47 		close(s);
       
    48 	}
       
    49 
       
    50 	const std::string receive() override {
       
    51 		// TODO: TCP receive()
       
    52 		return "TODO: receive() a message";
       
    53 	}
       
    54 
       
    55 };
       
    56 
       
    57 static class TCPSocketFactory : public SocketFactory {
       
    58 public:
       
    59 
       
    60 	bool canHandle(const std::string& connectionString) override {
       
    61 		return connectionString.rfind("tcp://", 0) == 0;
       
    62 	}
       
    63 
       
    64 	Socket* open(const std::string& connectionString) override {
       
    65 		// TODO: pass string to constructor
       
    66 		return new TCPSocket();
       
    67 	}
       
    68 } tcpSocketFactory;
       
    69 
       
    70 static std::vector<SocketFactory*> factories{&tcpSocketFactory};
       
    71 
       
    72 SocketFactory* SocketFactory::find(const std::string& connectionString) {
       
    73 	for (auto f : factories) if (f->canHandle(connectionString)) return f;
       
    74 	return nullptr;
       
    75 }
       
    76 
       
    77 
       
    78 }
       
    79 }
       
    80 }