ebusd_send added.
TCPListener renamed to TCPServer. TCPClient added.
This commit is contained in:
@@ -13,3 +13,4 @@
|
|||||||
*.o
|
*.o
|
||||||
*.dirstamp
|
*.dirstamp
|
||||||
/ebusd_feed
|
/ebusd_feed
|
||||||
|
/ebusd_send
|
||||||
|
|||||||
+7
-1
@@ -4,7 +4,9 @@ ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
|
|||||||
|
|
||||||
AM_CXXFLAGS = $(LIBEBUS_CFLAGS) -I$(top_srcdir)/utils -fpic -Wall -Wextra
|
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 \
|
ebusd_SOURCES = src/main.cpp \
|
||||||
src/baseloop.cpp \
|
src/baseloop.cpp \
|
||||||
@@ -26,3 +28,7 @@ ebusd_feed_SOURCES = tools/ebusd_feed.cpp \
|
|||||||
utils/appl.cpp
|
utils/appl.cpp
|
||||||
|
|
||||||
ebusd_feed_LDADD = $(LIBEBUS_LIBS)
|
ebusd_feed_LDADD = $(LIBEBUS_LIBS)
|
||||||
|
|
||||||
|
ebusd_send_SOURCES = tools/ebusd_send.cpp \
|
||||||
|
utils/appl.cpp \
|
||||||
|
utils/tcpsocket.cpp
|
||||||
|
|||||||
+9
-10
@@ -117,13 +117,12 @@ void* Connection::run()
|
|||||||
|
|
||||||
Network::Network(const bool localhost) : m_listening(false), m_running(false)
|
Network::Network(const bool localhost) : m_listening(false), m_running(false)
|
||||||
{
|
{
|
||||||
// Start Listener
|
|
||||||
if (localhost == true)
|
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
|
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;
|
m_listening = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -141,7 +140,7 @@ Network::~Network()
|
|||||||
if (m_running == true)
|
if (m_running == true)
|
||||||
stop();
|
stop();
|
||||||
|
|
||||||
delete m_Listener;
|
delete m_Server;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* Network::run()
|
void* Network::run()
|
||||||
@@ -157,10 +156,10 @@ void* Network::run()
|
|||||||
|
|
||||||
FD_ZERO(&checkfds);
|
FD_ZERO(&checkfds);
|
||||||
FD_SET(m_notify.notifyFD(), &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()) ?
|
(m_notify.notifyFD() > m_Server->getFD()) ?
|
||||||
(maxfd = m_notify.notifyFD()) : (maxfd = m_Listener->getFD());
|
(maxfd = m_notify.notifyFD()) : (maxfd = m_Server->getFD());
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
fd_set readfds;
|
fd_set readfds;
|
||||||
@@ -186,8 +185,8 @@ void* Network::run()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// new data from socket
|
// new data from socket
|
||||||
if (FD_ISSET(m_Listener->getFD(), &readfds)) {
|
if (FD_ISSET(m_Server->getFD(), &readfds)) {
|
||||||
TCPSocket* socket = m_Listener->newSocket();
|
TCPSocket* socket = m_Server->newSocket();
|
||||||
if (socket == NULL)
|
if (socket == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -72,7 +72,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
std::list<Connection*> m_connections;
|
std::list<Connection*> m_connections;
|
||||||
WQueue<Message*>* m_queue;
|
WQueue<Message*>* m_queue;
|
||||||
TCPListener* m_Listener;
|
TCPServer* m_Server;
|
||||||
Notify m_notify;
|
Notify m_notify;
|
||||||
bool m_listening;
|
bool m_listening;
|
||||||
bool m_running;
|
bool m_running;
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ Appl& A = Appl::Instance();
|
|||||||
void define_args()
|
void define_args()
|
||||||
{
|
{
|
||||||
A.addItem("p_device", Appl::Param("/dev/ttyUSB60"), "d", "device",
|
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);
|
Appl::type_string, Appl::opt_mandatory);
|
||||||
|
|
||||||
A.addItem("p_file", Appl::Param(""), "f", "file",
|
A.addItem("p_file", Appl::Param(""), "f", "file",
|
||||||
@@ -41,7 +41,7 @@ void define_args()
|
|||||||
Appl::type_string, Appl::opt_mandatory);
|
Appl::type_string, Appl::opt_mandatory);
|
||||||
|
|
||||||
A.addItem("p_time", Appl::Param(10000), "t", "time",
|
A.addItem("p_time", Appl::Param(10000), "t", "time",
|
||||||
"wait time [ms] (default: 10000)",
|
"wait time [ms] (10000)",
|
||||||
Appl::type_long, Appl::opt_mandatory);
|
Appl::type_long, Appl::opt_mandatory);
|
||||||
|
|
||||||
A.addItem("p_help", Appl::Param(false), "h", "help",
|
A.addItem("p_help", Appl::Param(false), "h", "help",
|
||||||
|
|||||||
@@ -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
@@ -18,8 +18,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "tcpsocket.h"
|
#include "tcpsocket.h"
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
TCPSocket::TCPSocket(int sfd, struct sockaddr_in* address) : m_sfd(sfd)
|
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)
|
if (m_listening == true)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -72,7 +114,7 @@ int TCPListener::start()
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
TCPSocket* TCPListener::newSocket()
|
TCPSocket* TCPServer::newSocket()
|
||||||
{
|
{
|
||||||
if (m_listening == false)
|
if (m_listening == false)
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -83,7 +125,6 @@ TCPSocket* TCPListener::newSocket()
|
|||||||
memset(&address, 0, sizeof(address));
|
memset(&address, 0, sizeof(address));
|
||||||
|
|
||||||
int sfd = accept(m_lfd, (struct sockaddr*) &address, &len);
|
int sfd = accept(m_lfd, (struct sockaddr*) &address, &len);
|
||||||
|
|
||||||
if (sfd < 0)
|
if (sfd < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
|||||||
+16
-5
@@ -29,7 +29,8 @@ class TCPSocket
|
|||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
friend class TCPListener;
|
friend class TCPClient;
|
||||||
|
friend class TCPServer;
|
||||||
|
|
||||||
~TCPSocket() { close(m_sfd); }
|
~TCPSocket() { close(m_sfd); }
|
||||||
|
|
||||||
@@ -47,18 +48,28 @@ private:
|
|||||||
int m_port;
|
int m_port;
|
||||||
std::string m_ip;
|
std::string m_ip;
|
||||||
|
|
||||||
TCPSocket(int sd, struct sockaddr_in* address);
|
TCPSocket(int sfd, struct sockaddr_in* address);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class TCPListener
|
class TCPClient
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
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) {}
|
: 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();
|
int start();
|
||||||
TCPSocket* newSocket();
|
TCPSocket* newSocket();
|
||||||
|
|||||||
Reference in New Issue
Block a user