OrocosComponentLibrary  2.8.3
socketmarshaller.cpp
1 /***************************************************************************
2 
3  socketmarshaller.cpp - description
4  -------------------
5  begin : Mon August 7 2006
6  copyright : (C) 2006 Bas Kemper
7  email : kst@baskemper.be
8 
9  ***************************************************************************
10  * This library is free software; you can redistribute it and/or *
11  * modify it under the terms of the GNU Lesser General Public *
12  * License as published by the Free Software Foundation; either *
13  * version 2.1 of the License, or (at your option) any later version. *
14  * *
15  * This library is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18  * Lesser General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU Lesser General Public *
21  * License along with this library; if not, write to the Free Software *
22  * Foundation, Inc., 59 Temple Place, *
23  * Suite 330, Boston, MA 02111-1307 USA *
24  * *
25  ***************************************************************************/
26 
27 #include <rtt/Logger.hpp>
28 #include <rtt/Property.hpp>
29 #include <rtt/base/PropertyIntrospection.hpp>
30 #include <rtt/os/Mutex.hpp>
31 #include "TcpReporting.hpp"
32 #include "socketmarshaller.hpp"
33 #include "datasender.hpp"
34 
35 using RTT::Logger;
36 
37 namespace RTT
38 {
39  SocketMarshaller::SocketMarshaller(OCL::TcpReporting* reporter)
40  : _reporter(reporter)
41  {
42  }
43 
44  SocketMarshaller::~SocketMarshaller()
45  {
46  closeAllConnections();
47  }
48 
49  void SocketMarshaller::addConnection(OCL::TCP::Socket* os)
50  {
51  lock.lock();
52  OCL::TCP::Datasender* conn = new OCL::TCP::Datasender(this, os);
53  _connections.push_front( conn );
54  conn->Activity::start();
55  lock.unlock();
56  }
57 
58  void SocketMarshaller::closeAllConnections()
59  {
60  // TODO: locking, proper connection shutdown
61  while( !_connections.empty() )
62  {
63  removeConnection( _connections.front() );
64  }
65  }
66 
67  void SocketMarshaller::flush()
68  {}
69 
70  void SocketMarshaller::removeConnection(OCL::TCP::Datasender* sender)
71  {
72  lock.lock();
73  _connections.remove( sender );
74  sender->breakLoop();
75  delete sender;
76  lock.unlock();
77  }
78 
79  OCL::TcpReporting* SocketMarshaller::getReporter() const
80  {
81  return _reporter;
82  }
83 
84  void SocketMarshaller::serialize(RTT::base::PropertyBase*)
85  {
86  // This method is pure virtual in the parent class.
87  Logger::log() << Logger::Error << "Unexpected call to SocketMarshaller::serialize" <<
88  Logger::endl;
89  }
90 
91  void SocketMarshaller::serialize(const PropertyBag &v)
92  {
93  lock.lock();
94  // TODO: sending data does not run in parallel!
95  for( std::list<OCL::TCP::Datasender*>::iterator it = _connections.begin();
96  it != _connections.end(); )
97  {
98  if( (*it)->isValid() )
99  {
100  (*it)->serialize(v);
101  it++;
102  } else {
103  OCL::TCP::Datasender* torm = *it;
104  it++;
105  removeConnection( torm );
106  }
107  }
108  lock.unlock();
109  }
110 
111  void SocketMarshaller::shutdown()
112  {
113  closeAllConnections();
114  }
115 }
This class manages the connection with one single client.
Definition: datasender.hpp:59
A component which writes data reports to a tcp/ip socket.
Definition: Category.hpp:10