diff --git a/.gitignore b/.gitignore index c819aec0..4f6ee9ec 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ *.o *.dirstamp /ebusd_feed +/ebusd_send diff --git a/Makefile.am b/Makefile.am index 079eda9f..43d12696 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,7 +4,9 @@ ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS} AM_CXXFLAGS = $(LIBEBUS_CFLAGS) -I$(top_srcdir)/utils -fpic -Wall -Wextra -bin_PROGRAMS = ebusd ebusd_feed +bin_PROGRAMS = ebusd \ + ebusd_feed \ + ebusd_send ebusd_SOURCES = src/main.cpp \ src/baseloop.cpp \ @@ -26,3 +28,7 @@ ebusd_feed_SOURCES = tools/ebusd_feed.cpp \ utils/appl.cpp ebusd_feed_LDADD = $(LIBEBUS_LIBS) + +ebusd_send_SOURCES = tools/ebusd_send.cpp \ + utils/appl.cpp \ + utils/tcpsocket.cpp diff --git a/src/network.cpp b/src/network.cpp index d426fa2d..544936aa 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -117,13 +117,12 @@ void* Connection::run() Network::Network(const bool localhost) : m_listening(false), m_running(false) { - // Start Listener if (localhost == true) - m_Listener = new TCPListener(A.getParam("p_port"), "127.0.0.1"); + m_Server = new TCPServer(A.getParam("p_port"), "127.0.0.1"); else - m_Listener = new TCPListener(A.getParam("p_port"), "0.0.0.0"); + m_Server = new TCPServer(A.getParam("p_port"), "0.0.0.0"); - if (m_Listener && m_Listener->start() == 0) + if (m_Server && m_Server->start() == 0) m_listening = true; } @@ -141,7 +140,7 @@ Network::~Network() if (m_running == true) stop(); - delete m_Listener; + delete m_Server; } void* Network::run() @@ -157,10 +156,10 @@ void* Network::run() FD_ZERO(&checkfds); FD_SET(m_notify.notifyFD(), &checkfds); - FD_SET(m_Listener->getFD(), &checkfds); + FD_SET(m_Server->getFD(), &checkfds); - (m_notify.notifyFD() > m_Listener->getFD()) ? - (maxfd = m_notify.notifyFD()) : (maxfd = m_Listener->getFD()); + (m_notify.notifyFD() > m_Server->getFD()) ? + (maxfd = m_notify.notifyFD()) : (maxfd = m_Server->getFD()); for (;;) { fd_set readfds; @@ -186,8 +185,8 @@ void* Network::run() } // new data from socket - if (FD_ISSET(m_Listener->getFD(), &readfds)) { - TCPSocket* socket = m_Listener->newSocket(); + if (FD_ISSET(m_Server->getFD(), &readfds)) { + TCPSocket* socket = m_Server->newSocket(); if (socket == NULL) continue; diff --git a/src/network.h b/src/network.h index f4abcc32..930aa936 100644 --- a/src/network.h +++ b/src/network.h @@ -72,7 +72,7 @@ public: private: std::list m_connections; WQueue* m_queue; - TCPListener* m_Listener; + TCPServer* m_Server; Notify m_notify; bool m_listening; bool m_running; diff --git a/tools/ebusd_feed.cpp b/tools/ebusd_feed.cpp index 07c76da0..39c787a7 100644 --- a/tools/ebusd_feed.cpp +++ b/tools/ebusd_feed.cpp @@ -33,7 +33,7 @@ Appl& A = Appl::Instance(); void define_args() { A.addItem("p_device", Appl::Param("/dev/ttyUSB60"), "d", "device", - "dummy serial device (default: /dev/ttyUSB60)\n\t\t(hint: socat -d -d pty,raw,echo=0 pty,raw,echo=0)", + "dummy serial device (/dev/ttyUSB60)\n\t\t(hint: socat -d -d pty,raw,echo=0 pty,raw,echo=0)", Appl::type_string, Appl::opt_mandatory); A.addItem("p_file", Appl::Param(""), "f", "file", @@ -41,7 +41,7 @@ void define_args() Appl::type_string, Appl::opt_mandatory); A.addItem("p_time", Appl::Param(10000), "t", "time", - "wait time [ms] (default: 10000)", + "wait time [ms] (10000)", Appl::type_long, Appl::opt_mandatory); A.addItem("p_help", Appl::Param(false), "h", "help", diff --git a/tools/ebusd_send.cpp b/tools/ebusd_send.cpp new file mode 100644 index 00000000..def155d2 --- /dev/null +++ b/tools/ebusd_send.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (C) Roland Jax 2012-2014 + * + * This file is part of ebusd. + * + * ebusd 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, either version 3 of the License, or + * (at your option) any later version. + * + * ebusd 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 ebusd. If not, see http://www.gnu.org/licenses/. + */ + +#include "appl.h" +#include "tcpsocket.h" +#include +#include + +Appl& A = Appl::Instance(); + +void define_args() +{ + A.addItem("p_server", Appl::Param("localhost"), "s", "server", + "servername or IP (localhost)", + Appl::type_string, Appl::opt_mandatory); + + A.addItem("p_port", Appl::Param(8888), "p", "port", + "\tport (8888)", + Appl::type_int, Appl::opt_mandatory); + + A.addItem("p_help", Appl::Param(false), "h", "help", + "print this message", + Appl::type_bool, Appl::opt_none); +} + +int main(int argc, char* argv[]) +{ + // define Arguments and Application variables + define_args(); + + // parse Arguments + if (A.parseArgs(argc, argv) == false) { + A.printArgs(); + exit(EXIT_FAILURE); + } + + // print Help + if (A.getParam("p_help") == true) { + A.printArgs(); + exit(EXIT_SUCCESS); + } + + TCPClient* client = new TCPClient(); + TCPSocket* socket = client->connect(A.getParam("p_server"), A.getParam("p_port")); + //~ TCPSocket* socket = client->connect(A.getParam("p_server"), A.getParam("p_port")); + + delete socket; + delete client; + + return 0; + +} + diff --git a/utils/tcpsocket.cpp b/utils/tcpsocket.cpp index 4a24ea3c..ed43ac80 100644 --- a/utils/tcpsocket.cpp +++ b/utils/tcpsocket.cpp @@ -18,8 +18,14 @@ */ #include "tcpsocket.h" +#include +#include +#include +#include #include +#include #include +#include #include TCPSocket::TCPSocket(int sfd, struct sockaddr_in* address) : m_sfd(sfd) @@ -39,7 +45,43 @@ bool TCPSocket::isValid() } -int TCPListener::start() +TCPSocket* TCPClient::connect(const std::string& server, const int& port) +{ + struct sockaddr_in address; + int ret; + + memset((char*) &address, 0, sizeof(address)); + + if (inet_addr(server.c_str()) == INADDR_NONE) { + struct hostent* he; + + he = gethostbyname(server.c_str()); + if (he == NULL) + return NULL; + + memcpy(&address.sin_addr, he->h_addr_list[0], he->h_length); + } else { + ret = inet_aton(server.c_str(), &address.sin_addr); + if (ret == 0) + return NULL; + } + + address.sin_family = AF_INET; + address.sin_port = port; + + int sfd = socket(AF_INET, SOCK_STREAM, 0); + if (sfd < 0) + return NULL; + + ret = ::connect(sfd, (struct sockaddr*) &address, sizeof(address)); + if (ret < 0) + return NULL; + + return new TCPSocket(sfd, &address); +} + + +int TCPServer::start() { if (m_listening == true) return 0; @@ -72,7 +114,7 @@ int TCPListener::start() return result; } -TCPSocket* TCPListener::newSocket() +TCPSocket* TCPServer::newSocket() { if (m_listening == false) return NULL; @@ -83,7 +125,6 @@ TCPSocket* TCPListener::newSocket() memset(&address, 0, sizeof(address)); int sfd = accept(m_lfd, (struct sockaddr*) &address, &len); - if (sfd < 0) return NULL; diff --git a/utils/tcpsocket.h b/utils/tcpsocket.h index 00f9c141..eb61fe03 100644 --- a/utils/tcpsocket.h +++ b/utils/tcpsocket.h @@ -29,7 +29,8 @@ class TCPSocket { public: - friend class TCPListener; + friend class TCPClient; + friend class TCPServer; ~TCPSocket() { close(m_sfd); } @@ -47,18 +48,28 @@ private: int m_port; std::string m_ip; - TCPSocket(int sd, struct sockaddr_in* address); + TCPSocket(int sfd, struct sockaddr_in* address); }; -class TCPListener +class TCPClient { public: - TCPListener(int port, std::string address) + TCPSocket* connect(const std::string& server, const int& port); + +private: + +}; + +class TCPServer +{ + +public: + TCPServer(const int port, const std::string address) : m_lfd(0), m_port(port), m_address(address), m_listening(false) {} - ~TCPListener() { if (m_lfd > 0) {close(m_lfd);} } + ~TCPServer() { if (m_lfd > 0) {close(m_lfd);} } int start(); TCPSocket* newSocket();