Orocos Real-Time Toolkit  2.6.0
CorbaLib.cpp
00001 /***************************************************************************
00002   tag: FMTC  Tue Mar 11 21:49:22 CET 2008  CorbaLib.cpp
00003 
00004                         CorbaLib.cpp -  description
00005                            -------------------
00006     begin                : Tue March 11 2008
00007     copyright            : (C) 2008 FMTC
00008     email                : peter.soetens@fmtc.be
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 "corba.h"
00040 #ifdef CORBA_IS_TAO
00041 #include "corba.h"
00042 #include <tao/PortableServer/PortableServer.h>
00043 #else
00044 #include <omniORB4/CORBA.h>
00045 #include <omniORB4/poa.h>
00046 #endif
00047 
00048 #include "TransportPlugin.hpp"
00049 #include "CorbaTemplateProtocol.hpp"
00050 #include "RTTCorbaConversion.hpp"
00051 #include "../../types/TransportPlugin.hpp"
00052 #include "../../types/TypekitPlugin.hpp"
00053 
00054 using namespace std;
00055 using namespace RTT::detail;
00056 
00057 namespace RTT {
00058     namespace corba {
00059 
00064         class CorbaFallBackProtocol
00065             : public CorbaTypeTransporter
00066         {
00067             bool warn;
00068         public:
00069             CorbaFallBackProtocol(bool do_warn = true) : warn(do_warn) {}
00070             virtual CORBA::Any* createAny(DataSourceBase::shared_ptr source) const
00071             {
00072                 if (warn) {
00073                     Logger::In in("CorbaFallBackProtocol");
00074                     log(Error) << "Could not send data of type '"<< source->getTypeName()<<"' : data type not known to CORBA Transport." <<Logger::endl;
00075                 }
00076                 source->evaluate();
00077                 return new CORBA::Any();
00078             }
00079 
00080             virtual bool updateAny( base::DataSourceBase::shared_ptr source, CORBA::Any& any) const
00081             {
00082                 if (warn) {
00083                     Logger::In in("CorbaFallBackProtocol");
00084                     log(Error) << "Could not send data of type '"<< source->getTypeName()<<"' : data type not known to CORBA Transport." <<Logger::endl;
00085                 }
00086                 source->evaluate();
00087                 return false;
00088             }
00089 
00090             virtual base::DataSourceBase::shared_ptr createDataSource(const CORBA::Any* any) const
00091             {
00092                 return base::DataSourceBase::shared_ptr();
00093             }
00094 
00098             virtual bool updateFromAny(const CORBA::Any* blob, DataSourceBase::shared_ptr target) const
00099             {
00100                 if (warn) {
00101                     Logger::In in("CorbaFallBackProtocol");
00102                     log(Error) << "Could not update type '"<<target->getTypeName()<<"' with received data : data type not known to CORBA Transport." <<Logger::endl;
00103                 }
00104                 return false;
00105             }
00106 
00107             virtual ChannelElementBase::shared_ptr createStream(base::PortInterface* port, const ConnPolicy& policy, bool is_sender) const {
00108                 Logger::In in("CorbaFallBackProtocol");
00109                 log(Error) << "Could create Stream for port '"<<port->getName()<<"' : data type not known to CORBA Transport." <<Logger::endl;
00110                 return ChannelElementBase::shared_ptr();
00111             }
00112 
00113             virtual base::ChannelElementBase* buildDataStorage(ConnPolicy const& policy) const { return 0; }
00114 
00115             virtual CRemoteChannelElement_i* createChannelElement_i(DataFlowInterface*, ::PortableServer::POA* poa, bool) const {
00116                 Logger::In in("CorbaFallBackProtocol");
00117                 log(Error) << "Could create Channel : data type not known to CORBA Transport." <<Logger::endl;
00118                 return 0;
00119 
00120             }
00121 
00122             virtual base::ChannelElementBase* buildChannelOutput(base::InputPortInterface& port,
00123                 ConnPolicy const& policy) const {
00124                 Logger::In in("CorbaFallBackProtocol");
00125                 log(Error) << "Could create outputHalf for port "<<port.getName()<<": data type not known to CORBA Transport." <<Logger::endl;
00126                 return 0;
00127             }
00128 
00129             virtual base::ChannelElementBase* buildChannelInput(base::OutputPortInterface& port,
00130                 ConnPolicy const& policy) const {
00131                 Logger::In in("CorbaFallBackProtocol");
00132                 log(Error) << "Could create outputHalf for port "<<port.getName()<<": data type not known to CORBA Transport." <<Logger::endl;
00133                 return 0;
00134             }
00135           virtual base::DataSourceBase::shared_ptr createPropertyDataSource(CService_ptr serv, const std::string& vname) {
00136               CORBA::String_var tname = serv->getPropertyTypeName( CORBA::string_dup(vname.c_str()));
00137               log(Warning) << "Corba: Remote property '"<< vname << "' has unknown type " << tname.in()  << endlog();
00138               return base::DataSourceBase::shared_ptr( );
00139           }
00140 
00141           virtual base::DataSourceBase::shared_ptr createAttributeDataSource(CService_ptr serv, const std::string& vname) {
00142               CORBA::String_var tname = serv->getAttributeTypeName( CORBA::string_dup( vname.c_str()));
00143               log(Warning) << "Corba: Remote attribute '"<< vname << "' has unknown type " << tname.in()  << endlog();
00144               return base::DataSourceBase::shared_ptr( );
00145           }
00146         };
00147 
00148         bool CorbaLibPlugin::registerTransport(std::string name, TypeInfo* ti)
00149         {
00150             if ( name == "unknown_t") // register fallback also.
00151                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaFallBackProtocol());
00152             if ( name == "int" )
00153                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<int>() );
00154             if ( name == "double" )
00155                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<double>() );
00156             if ( name == "float" )
00157                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<float>() );
00158             if ( name == "uint" )
00159                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<unsigned int>() );
00160             if ( name == "char" )
00161                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<char>() );
00162             //if ( name == "long" )
00163             //    return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<long>() );
00164 //                if ( name == "PropertyBag" )
00165 //                    return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<PropertyBag>() );
00166             if ( name == "bool" )
00167                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<bool>() );
00168 #ifndef RTT_NO_STD_TYPES
00169             if ( name == "string" )
00170                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<std::string>() );
00171             if ( name == "array" )
00172                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol< std::vector<double> >() );
00173 #endif
00174             if ( name == "void" )
00175                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaFallBackProtocol(false)); // warn=false
00176             if ( name == "ConnPolicy")
00177                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<ConnPolicy>() );
00178             if ( name == "TaskContext")
00179                 return ti->addProtocol(ORO_CORBA_PROTOCOL_ID, new CorbaTemplateProtocol<TaskContext*>() );
00180 
00181             return false;
00182         }
00183 
00184         std::string CorbaLibPlugin::getTransportName() const {
00185             return "CORBA";
00186         }
00187 
00188         std::string CorbaLibPlugin::getTypekitName() const {
00189             return "rtt-types";
00190         }
00191 
00192         std::string CorbaLibPlugin::getName() const {
00193             return "rtt-corba-types";
00194         }
00195     }
00196 }
00197 
00198 ORO_TYPEKIT_PLUGIN( RTT::corba::CorbaLibPlugin )