TOBI Interface A  0.1
/home/breidi/Dropbox/libtia/include/tia-private/network/tcp_data_server.h
Go to the documentation of this file.
00001 /*
00002     This file is part of the TOBI Interface A (TiA) library.
00003 
00004     Commercial Usage
00005     Licensees holding valid Graz University of Technology Commercial
00006     licenses may use this file in accordance with the Graz University
00007     of Technology Commercial License Agreement provided with the
00008     Software or, alternatively, in accordance with the terms contained in
00009     a written agreement between you and Graz University of Technology.
00010 
00011     --------------------------------------------------
00012 
00013     GNU Lesser General Public License Usage
00014     Alternatively, this file may be used under the terms of the GNU Lesser
00015     General Public License version 3.0 as published by the Free Software
00016     Foundation and appearing in the file lgpl.txt included in the
00017     packaging of this file.  Please review the following information to
00018     ensure the GNU General Public License version 3.0 requirements will be
00019     met: http://www.gnu.org/copyleft/lgpl.html.
00020 
00021     In case of GNU Lesser General Public License Usage ,the TiA library
00022     is distributed in the hope that it will be useful,
00023     but WITHOUT ANY WARRANTY; without even the implied warranty of
00024     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025     GNU General Public License for more details.
00026 
00027     You should have received a copy of the GNU Lesser General Public License
00028     along with the TiA library. If not, see <http://www.gnu.org/licenses/>.
00029 
00030     Copyright 2010 Graz University of Technology
00031     Contact: TiA@tobi-project.org
00032 */
00033 
00041 #ifndef TCPDATASERVER_H
00042 #define TCPDATASERVER_H
00043 
00044 // Standard
00045 #include <set>
00046 #include <vector>
00047 
00048 // Boost
00049 #include <boost/thread/condition.hpp>
00050 
00051 // local
00052 #include "tcp_server.h"
00053 
00054 namespace tia
00055 {
00056 // forward declarations
00057 class DataPacket;
00058 
00059 //-----------------------------------------------------------------------------
00060 
00068 class TCPDataConnection : public TCPServer, public boost::enable_shared_from_this<TCPDataConnection>
00069 {
00070 public:
00074   typedef boost::shared_ptr<TCPDataConnection> pointer;
00075 
00080   static pointer create(boost::asio::io_service& io_service)
00081   {
00082     return pointer(new TCPDataConnection(io_service));
00083   }
00084 
00085 public:
00089   virtual ~TCPDataConnection(){}
00090 
00095   bool connected() const {return connection_; }
00096 
00100   boost::asio::ip::tcp::endpoint localEndpoint() const
00101   {
00102     return acceptor_.local_endpoint();
00103   }
00104 
00109   void sendDataPacket(DataPacket& packet);
00110 
00111 protected:
00112   virtual void startAccept();
00113 
00117   virtual void handleAccept(const TCPConnection::pointer& new_connection,
00118         const boost::system::error_code& error);
00119 
00120 private:
00124   TCPDataConnection(boost::asio::io_service& io_service);
00125 
00129   void handleWrite(const boost::system::error_code& e,
00130       std::size_t bytes_transferred);
00131 
00132 private:
00133   TCPConnection::pointer         connection_;  
00134 
00135   boost::asio::ip::tcp::endpoint remote_endpoint_;  
00136 
00137   boost::mutex                   mutex_; 
00138 };
00139 
00140 //-----------------------------------------------------------------------------
00141 
00148 class TCPDataServer
00149 {
00150 public:
00154   TCPDataServer(boost::asio::io_service& io_service);
00155 
00160   bool connected(const boost::asio::ip::tcp::endpoint& endpoint) const;
00161 
00166   boost::asio::ip::tcp::endpoint addConnection();
00167 
00172   void removeConnection(const boost::asio::ip::tcp::endpoint& endpoint);
00173 
00178   void enableTransmission(const boost::asio::ip::tcp::endpoint& endpoint, bool enable);
00179 
00186   void sendDataPacket(DataPacket& packet);
00187 
00188 private:
00189   typedef std::map<boost::asio::ip::tcp::endpoint, TCPDataConnection::pointer> ClientConnectionMap;
00190 
00191   boost::asio::io_service&  io_service_; 
00192   ClientConnectionMap       connections_; 
00193   ClientConnectionMap       connections_transmission_enabled_; 
00194   mutable boost::mutex      mutex_; 
00195 };
00196 
00197 } // Namespace tobiss
00198 
00199 #endif //TCPDATASERVER_H
00200 
00201 // End Of File
 All Data Structures Files Functions Variables Typedefs Enumerations