ebusd_send added.

TCPListener renamed to TCPServer.
TCPClient added.
This commit is contained in:
Roland Jax
2014-06-02 18:58:19 +02:00
parent 9e6ba69f70
commit bc7b4d91b0
8 changed files with 149 additions and 22 deletions
+1
View File
@@ -13,3 +13,4 @@
*.o
*.dirstamp
/ebusd_feed
/ebusd_send
+7 -1
View File
@@ -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
+9 -10
View File
@@ -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<int>("p_port"), "127.0.0.1");
m_Server = new TCPServer(A.getParam<int>("p_port"), "127.0.0.1");
else
m_Listener = new TCPListener(A.getParam<int>("p_port"), "0.0.0.0");
m_Server = new TCPServer(A.getParam<int>("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;
+1 -1
View File
@@ -72,7 +72,7 @@ public:
private:
std::list<Connection*> m_connections;
WQueue<Message*>* m_queue;
TCPListener* m_Listener;
TCPServer* m_Server;
Notify m_notify;
bool m_listening;
bool m_running;
+2 -2
View File
@@ -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",
+69
View File
@@ -0,0 +1,69 @@
/*
* Copyright (C) Roland Jax 2012-2014 <roland.jax@liwest.at>
*
* 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 <iostream>
#include <cstdlib>
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<bool>("p_help") == true) {
A.printArgs();
exit(EXIT_SUCCESS);
}
TCPClient* client = new TCPClient();
TCPSocket* socket = client->connect(A.getParam<const char*>("p_server"), A.getParam<int>("p_port"));
//~ TCPSocket* socket = client->connect(A.getParam<const char*>("p_server"), A.getParam<int>("p_port"));
delete socket;
delete client;
return 0;
}
+44 -3
View File
@@ -18,8 +18,14 @@
*/
#include "tcpsocket.h"
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
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;
+16 -5
View File
@@ -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();