TOBI Interface A  0.1
/home/breidi/Dropbox/libtia/src/tia/newtia/server_impl/fusty_data_server_impl.cpp
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 
00034 #include "tia-private/newtia/server_impl/fusty_data_server_impl.h"
00035 #include "tia-private/newtia/tia_exceptions.h"
00036 
00037 namespace tia
00038 {
00039 
00040 //-----------------------------------------------------------------------------
00041 FustyDataServerImpl::FustyDataServerImpl (TCPDataServer& tcp_data_server,
00042                                           UDPDataServer& udp_data_server)
00043     : tcp_data_server_ (tcp_data_server),
00044       udp_data_server_ (udp_data_server),
00045       next_free_connection_id_ (0)
00046 {
00047 
00048 }
00049 
00050 //-----------------------------------------------------------------------------
00051 FustyDataServerImpl::~FustyDataServerImpl ()
00052 {
00053 
00054 }
00055 
00056 //-----------------------------------------------------------------------------
00057 Port FustyDataServerImpl::localPort (ConnectionID connection) const
00058 {
00059     if (tcp_connections_.count (connection))
00060         return id_tcp_endpoint_map_.at (connection).port ();
00061     else
00062         return udp_data_server_.destination ().port ();
00063 
00064     throw TiAException ("FustyDataServerImpl::localPort: No connection with that id.");
00065 }
00066 
00067 //-----------------------------------------------------------------------------
00068  std::string FustyDataServerImpl::getTargetIP () const
00069 {
00070    return(udp_data_server_.getTargetIP ());
00071 }
00072 
00073 //-----------------------------------------------------------------------------
00074 ConnectionID  FustyDataServerImpl::addConnection (bool udp)
00075 {
00076     ConnectionID new_connection_id = next_free_connection_id_;
00077     next_free_connection_id_++;
00078 
00079     if (udp)
00080     {
00081         udp_data_server_.incClientCount ();
00082         udp_connections_.insert (new_connection_id);
00083     }
00084     else
00085     {
00086         // std::cout << "call addconneciton on tcp server" << std::endl;
00087         boost::asio::ip::tcp::endpoint tcp_endpoint = tcp_data_server_.addConnection ();
00088         // std::cout << "call addconneciton on tcp server done" << std::endl;
00089         tcp_connections_.insert (new_connection_id);
00090         id_tcp_endpoint_map_[new_connection_id] = tcp_endpoint;
00091     }
00092 
00093     return new_connection_id;
00094 }
00095 
00096 //-----------------------------------------------------------------------------
00097 bool FustyDataServerImpl::hasConnection (ConnectionID connection) const
00098 {
00099     if (tcp_connections_.count (connection))
00100         return true;
00101     if (udp_connections_.count (connection))
00102         return true;
00103 
00104     return false;
00105 }
00106 
00107 //-----------------------------------------------------------------------------
00108 bool FustyDataServerImpl::transmitting (ConnectionID connection) const
00109 {
00110     return transmitting_.count (connection);
00111 }
00112 
00113 //-----------------------------------------------------------------------------
00114 bool FustyDataServerImpl::removeConnection (ConnectionID connection)
00115 {
00116     if (tcp_connections_.count (connection))
00117     {
00118         tcp_connections_.erase (connection);
00119         tcp_data_server_.removeConnection (id_tcp_endpoint_map_[connection]);
00120         id_tcp_endpoint_map_.erase (connection);
00121         return true;
00122     }
00123     else if (udp_connections_.count (connection))
00124     {
00125         udp_connections_.erase (connection);
00126         udp_data_server_.decClientCount ();
00127         return true;
00128     }
00129     return false;
00130 }
00131 
00132 //-----------------------------------------------------------------------------
00133 void FustyDataServerImpl::startTransmission (ConnectionID connection)
00134 {
00135     if (tcp_connections_.count (connection))
00136         tcp_data_server_.enableTransmission (id_tcp_endpoint_map_[connection], true);
00137 
00138     transmitting_.insert (connection);
00139 }
00140 
00141 //-----------------------------------------------------------------------------
00142 void FustyDataServerImpl::stopTransmission (ConnectionID connection)
00143 {
00144     if (tcp_connections_.count (connection))
00145         tcp_data_server_.enableTransmission (id_tcp_endpoint_map_[connection], false);
00146 
00147     transmitting_.erase (connection);
00148 }
00149 
00150 
00151 
00152 }
 All Data Structures Files Functions Variables Typedefs Enumerations