Orocos Real-Time Toolkit  2.5.0
InputPortInterface.cpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 22 11:59:07 CEST 2009  InputPortInterface.cpp
00003 
00004                         InputPortInterface.cpp -  description
00005                            -------------------
00006     begin                : Thu October 22 2009
00007     copyright            : (C) 2009 Sylvain Joyeux
00008     email                : sylvain.joyeux@m4x.org
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 #include "PortInterface.hpp"
00040 #include "InputPortInterface.hpp"
00041 #include "OutputPortInterface.hpp"
00042 #include "DataFlowInterface.hpp"
00043 #include "../Logger.hpp"
00044 #include <exception>
00045 #include <stdexcept>
00046 
00047 using namespace RTT;
00048 using namespace detail;
00049 using namespace std;
00050 
00051 
00052 InputPortInterface::InputPortInterface(std::string const& name, ConnPolicy const& default_policy)
00053 : PortInterface(name)
00054   , cmanager(this)
00055   , default_policy( default_policy )
00056 #ifdef ORO_SIGNALLING_PORTS
00057   , new_data_on_port_event(0)
00058 #else
00059  , msignal_interface(false)
00060 #endif
00061 {}
00062 
00063 InputPortInterface::~InputPortInterface()
00064 {
00065     cmanager.disconnect();
00066 #ifdef ORO_SIGNALLING_PORTS
00067     if ( new_data_on_port_event) {
00068         delete new_data_on_port_event;
00069     }
00070 #endif
00071 }
00072 
00073 ConnPolicy InputPortInterface::getDefaultPolicy() const
00074 { return default_policy; }
00075 
00076 #ifdef ORO_SIGNALLING_PORTS
00077 InputPortInterface::NewDataOnPortEvent* InputPortInterface::getNewDataOnPortEvent()
00078 {
00079     if (!new_data_on_port_event)
00080         new_data_on_port_event = new NewDataOnPortEvent();
00081     return new_data_on_port_event;
00082 }
00083 #endif
00084 bool InputPortInterface::connectTo(PortInterface* other, ConnPolicy const& policy)
00085 {
00086     OutputPortInterface* output = dynamic_cast<OutputPortInterface*>(other);
00087     if (! output) {
00088         log(Error) << "InputPort "<< getName() <<" could not connect to "<< other->getName() << ": not an Output port." <<endlog();
00089         return false;
00090     }
00091     return output->createConnection(*this, policy);
00092 }
00093 
00094 bool InputPortInterface::connectTo(PortInterface* other)
00095 {
00096     return connectTo(other, default_policy);
00097 }
00098 
00099 bool InputPortInterface::addConnection(ConnID* port_id, ChannelElementBase::shared_ptr channel_output, const ConnPolicy& policy)
00100 {
00101     // input ports don't check the connection policy.
00102     cmanager.addConnection( port_id, channel_output, policy);
00103     return true;
00104 }
00105 
00106 bool InputPortInterface::channelReady(ChannelElementBase::shared_ptr channel)
00107 {
00108     if ( channel && channel->inputReady() )
00109         return true;
00110     if (channel) {
00111         // in-the-middle disconnection, we need to inform both ends of
00112         // the channel that it's going to be disposed. Both endpoints
00113         // will inform their ports with a removal request.
00114         // From a design perspective, this removal must be initiated
00115         // by our connection manager and not by us.
00116         channel->disconnect(false);
00117         channel->disconnect(true);
00118     }
00119 
00120     return false;
00121 }
00122 
00123 bool InputPortInterface::removeConnection(ConnID* conn)
00124 {
00125     return cmanager.removeConnection(conn);
00126 }
00127 
00128 #ifndef ORO_SIGNALLING_PORTS
00129 void InputPortInterface::signal()
00130 {
00131     if (iface && msignal_interface)
00132         iface->dataOnPort(this);
00133 }
00134 void InputPortInterface::signalInterface(bool true_false)
00135 {
00136     msignal_interface = true_false;
00137 }
00138 #endif
00139 FlowStatus InputPortInterface::read(DataSourceBase::shared_ptr source, bool copy_old_data)
00140 { throw std::runtime_error("calling default InputPortInterface::read(datasource) implementation"); }
00142 bool InputPortInterface::connected() const
00143 { return cmanager.connected(); }
00144 
00145 void InputPortInterface::clear()
00146 {
00147     cmanager.clear();
00148 }
00149 
00150 void InputPortInterface::disconnect()
00151 {
00152     cmanager.disconnect();
00153 }
00154 
00155 bool InputPortInterface::disconnect(PortInterface* port)
00156 {
00157     return cmanager.disconnect(port);
00158 }
00159