28 #include <sys/socket.h> 29 #include <netinet/in.h> 30 #include <sys/types.h> 33 #include "TcpReporting.hpp" 34 #include <rtt/Activity.hpp> 35 #include <rtt/Logger.hpp> 36 #include <rtt/os/Mutex.hpp> 38 #include "socketmarshaller.hpp" 53 :
public RTT::Activity
65 _sock = ::socket(PF_INET, SOCK_STREAM, 0);
68 Logger::log() << Logger::Error <<
"Socket creation failed." << Logger::endl;
72 struct sockaddr_in localsocket;
73 struct sockaddr remote;
74 int adrlen =
sizeof(remote);
76 localsocket.sin_family = AF_INET;
77 localsocket.sin_port = htons(_port);
78 localsocket.sin_addr.s_addr = INADDR_ANY;
79 if( ::bind(_sock, (
struct sockaddr*)&localsocket,
sizeof(localsocket) ) < 0 )
85 #define TRY_OTHER_PORTS 87 #ifdef TRY_OTHER_PORTS 90 while( errno == EADDRINUSE && i < 5 && r < 0 )
92 localsocket.sin_port = htons(_port + i);
93 r = ::bind(_sock, (
struct sockaddr*)&localsocket,
sizeof(localsocket) );
98 Logger::log() << Logger::Info <<
"Port occupied, use port " << (_port+i-1) <<
" instead." << Logger::endl;
101 if( errno == EADDRINUSE )
103 Logger::log() << Logger::Error <<
"Binding of port failed: address already in use." << Logger::endl;
105 Logger::log() << Logger::Error <<
"Binding of port failed with errno " << errno << Logger::endl;
109 #ifdef TRY_OTHER_PORTS 114 if( ::listen(_sock, 2) < 0 )
116 Logger::log() << Logger::Info <<
"Cannot listen on socket" << Logger::endl;
122 int socket = ::accept( _sock, &remote,
123 reinterpret_cast<socklen_t*>(&adrlen) );
130 Logger::log() << Logger::Info <<
"Incoming connection" << Logger::endl;
131 _marshaller->addConnection(
new Orocos::TCP::Socket(socket) );
138 : Activity(10), _marshaller(marshaller)
144 Logger::log() << Logger::Info <<
"Starting server on port " << port << Logger::endl;
145 this->Activity::start();
149 void removeInstance()
169 Logger::log() << Logger::Error <<
"Could not listen on port " << _port << Logger::endl;
171 Logger::log() << Logger::Info <<
"Shutting down server" << Logger::endl;
176 virtual bool breakLoop()
182 int sock = ::socket(PF_INET, SOCK_STREAM, 0);
185 struct sockaddr_in socket;
186 socket.sin_family = AF_INET;
187 socket.sin_port = htons(_port);
188 socket.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
189 ::connect( sock, (
struct sockaddr*)&socket,
sizeof(socket) );
200 ListenThread::_instance =
new ListenThread( marshaller, port );
204 static void destroyInstance()
206 ListenThread::_instance->breakLoop();
216 port_prop(
"port",
"port to listen/send to",3142)
219 this->properties()->addProperty( port_prop);
222 TcpReporting::~TcpReporting()
233 port=port_prop.value();
237 bool TcpReporting::startHook()
239 RTT::Logger::In in(
"TcpReporting::startup");
242 ListenThread::createInstance(
fbody, port );
243 return ReportingComponent::startHook();
246 void TcpReporting::stopHook()
249 ListenThread::destroyInstance();
251 ReportingComponent::stopHook();
bool addMarshaller(RTT::marsh::MarshallInterface *headerM, RTT::marsh::MarshallInterface *bodyM)
Adds a Plugin to receive incomming data.
A component which writes data reports to a tcp/ip socket.
marsh::MarshallInterface which sends data to multiple sockets.
This file contains the macros and definitions to create dynamically loadable components.
bool removeMarshallers()
Remove and delete all added Marshallers.
The Orocos Component Library.
bool configureHook()
Implementation of base::TaskCore::configureHook().
A Component for periodically reporting Component Port contents to a human readable text format...
TcpReporting(std::string fr_name="ReportingComponent")
Create a reporting component which starts up a server.
const RTT::PropertyBag * getReport()
Return a property bag.
RTT::SocketMarshaller * fbody
marsh::MarshallInterface
ListenThread is a thread which waits for new incoming connections from clients.