PeerParser.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Tue Dec 21 22:43:07 CET 2004  PeerParser.cxx
00003 
00004                         PeerParser.cxx -  description
00005                            -------------------
00006     begin                : Tue December 21 2004
00007     copyright            : (C) 2004 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.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 
00040 #include "PeerParser.hpp"
00041 #include "parser-debug.hpp"
00042 #include "parse_exception.hpp"
00043 #include "../TaskContext.hpp"
00044 #include "parser-types.hpp"
00045 #include "../internal/GlobalService.hpp"
00046 
00047 #include <boost/bind.hpp>
00048 #include <boost/iterator/iterator_traits.hpp>
00049 
00050 namespace RTT
00051 {
00052     using boost::bind;
00053     using namespace detail;
00054     using namespace std;
00055     using namespace boost;
00056 
00057     error_status<> PeerParser::handle_no_peer(scanner_t const& scan, parser_error<PeerErrors, iter_t>&e )
00058     {
00059         int length = advance_on_error;
00060         // before, this was not necessary !
00061         while (advance_on_error != 0) {
00062             ++scan;
00063             --advance_on_error;
00064         }
00065         // ok, got as far as possible, _peer contains the furthest we got.
00066         //std::cerr<<"Returning accept, advance "<< length <<std::endl;
00067         return error_status<>( error_status<>::accept, length );
00068     }
00069 
00070         void PeerParser::done()
00071         {
00072             //std::cerr<<"Peerparser operating in "<<  context->getName()<<std::endl;
00073             mlastobject = "this";
00074 
00075             // if size() > 1, it must be a peer or service
00076             // first browse the peers
00077             while ( callqueue.size() > 0 && _peer->hasPeer( callqueue.front() ) ) {
00078                 //std::cerr<< _peer->getName() <<" has peer " << callqueue.front()<<std::endl;
00079                 _peer = _peer->getPeer( callqueue.front() );
00080 
00081                 if ( _peer->ready() == false ) {
00082                     throw parse_exception_semantic_error
00083                                     ("Attempt to use TaskContext "+ callqueue.front() +" which is not ready to use." );
00084                 }
00085 
00086                 callqueue.pop();
00087             }
00088 
00089             // BC: user uses 'states.' or 'programs'.
00090             if ( !callqueue.empty() ) {
00091                 std::string name = callqueue.front();
00092                 if ( (name == "states" || name == "programs") && _peer->provides()->hasService(name) == 0) {
00093                     log(Warning) << "'"<<name<<"' peer not found. The use of '"<<name<<"' has been deprecated."<<endlog();
00094                     log(Warning) << "Modify your script to use the program's or state machine's name directly."<<endlog();
00095                     callqueue.pop();
00096                 }
00097             }
00098 
00099             mcurobject = _peer->provides();
00100 
00101             // all peers done, now traverse services:
00102             while ( callqueue.size() > 0 && mcurobject->hasService( callqueue.front() ) ) {
00103                 //std::cerr<< mcurobject->getName() <<" has object " << callqueue.front()<<std::endl;
00104                 mcurobject = mcurobject->provides( callqueue.front() );
00105                 mlastobject = callqueue.front();
00106                 callqueue.pop();
00107             }
00108 
00109             // last resort: browse the global service if not a single match and items in queue
00110             if (mcurobject == context->provides() && callqueue.size() != 0 ) {
00111                 mcurobject = GlobalService::Instance();
00112                 while ( callqueue.size() && mcurobject->hasService( callqueue.front() ) ) {
00113                     mcurobject = mcurobject->provides( callqueue.front() );
00114                     mlastobject = callqueue.front();
00115                     callqueue.pop();
00116                 }
00117             }
00118 
00119             // Something went wrong, a peer or object was not found:
00120             if ( mfullpath && callqueue.size() != 0 ) {
00121                 // print to user the mismatch :
00122                 string object = callqueue.front();
00123                 while ( !callqueue.empty() )
00124                     callqueue.pop();
00125                 iter_t begin;
00126                 if ( _peer->provides() == mcurobject )
00127                     throw_(begin, "From TaskContext '"+context->getName()+"': Task '"+ _peer->getName()+"' has no child Service '"+object+"'." );
00128                 else
00129                     throw_(begin, "From TaskContext '"+context->getName()+"': Service '"+ mcurobject->getName()+"' has no child Service '"+object+"'." );
00130             }
00131             mfoundpath = true;
00132         }
00133 
00134         PeerParser::PeerParser(TaskContext* c, CommonParser& cp, bool fullpath)
00135             : commonparser(cp), mcurobject(c->provides()), mlastobject("this"), context(c), _peer(context), mfullpath(fullpath), mfoundpath(false), advance_on_error(0)
00136         {
00137             BOOST_SPIRIT_DEBUG_RULE( my_guard );
00138             BOOST_SPIRIT_DEBUG_RULE( peerpath );
00139             BOOST_SPIRIT_DEBUG_RULE( peerlocator );
00140             peerpath =
00141                 ( +(commonparser.notassertingidentifier >> ".")[boost::bind( &PeerParser::seenobjectname, this, _1, _2 ) ] )[boost::bind(&PeerParser::done, this)];
00142 
00143             // find as far as possible a peer without throwing an exception
00144             // outside our interface
00145             peerlocator =
00146                 my_guard( *((commonparser.notassertingidentifier >> ".")[boost::bind( &PeerParser::locatepeer, this, _1, _2 ) ]))
00147                         [ boost::bind(&PeerParser::handle_no_peer, this, _1, _2) ]
00148                 ;
00149         }
00150 
00151     void PeerParser::reset()
00152     {
00153         _peer = context;
00154         mcurobject = context->provides();
00155         mlastobject = "this";
00156         advance_on_error = 0;
00157         mfoundpath = false;
00158         while( !callqueue.empty() )
00159             callqueue.pop();
00160     }
00161 
00162     void PeerParser::seenobjectname( iter_t begin, iter_t end )
00163     {
00164         std::string name( begin, end );
00165         name.erase( name.length() -1  ); // compensate for extra "."
00166         callqueue.push( name );
00167 //         std::cerr << "seen " << name <<std::endl;
00168     }
00169 
00170     void PeerParser::locatepeer( iter_t begin, iter_t end )
00171     {
00172         std::string name( begin, end );
00173         name.erase( name.length() -1  ); // compensate for extra "."
00174 
00175         if ( mcurobject == _peer->provides() && _peer->hasPeer( name ) ) {
00176             _peer = _peer->getPeer( name );
00177             if ( _peer->ready() == false ) {
00178                 throw parse_exception_semantic_error
00179                                 ("Attempt to use TaskContext "+name+" which is not ready to use." );
00180             }
00181             mcurobject = _peer->provides();
00182             advance_on_error += end.base() - begin.base();
00183 
00184 //            cout << "PP located "<<name <<endl;
00185         }
00186         else if ( mcurobject->hasService(name) ) {
00187             mcurobject = mcurobject->provides(name);
00188             advance_on_error += end.base() - begin.base();
00189         }
00190         // check global service if we're still on the top-level:
00191         else if (mcurobject == _peer->provides() && GlobalService::Instance()->hasService(name) ) {
00192             mcurobject = GlobalService::Instance()->provides(name);
00193             advance_on_error += end.base() - begin.base();
00194             mfoundpath = true;
00195         } else {
00196             if ( name == "states" || name == "programs") {
00197                 log(Warning) << "'"<<name<<"' peer not found. The use of '"<<name<<"' has been deprecated."<<endlog();
00198                 log(Warning) << "Modify your script to use the program's or state machine's name directly."<<endlog();
00199                 advance_on_error += end.base() - begin.base();
00200                 return;
00201             }
00202 //            cout << "PP failed "<<name <<endl;
00203             // store object name for higher level access.
00204             // do not consume it though.
00205 //            cout << std::string(begin, end)<<endl;
00206             mlastobject = name;
00207             if (mfullpath) {
00208                 mcurobject.reset(); //when partial paths are OK, leave curobject pointing to last valid object.
00209                 mfoundpath = false;
00210             }
00211             throw_(begin, peer_not_found );
00212         }
00213     }
00214 
00215     rule_t& PeerParser::parser()
00216     {
00217         return peerpath;
00218     }
00219 
00220     rule_t& PeerParser::locator()
00221     {
00222         return peerlocator;
00223     }
00224 
00225     TaskContext* PeerParser::peer()
00226     {
00227         return _peer;
00228     }
00229 
00230     Service::shared_ptr PeerParser::taskObject()
00231     {
00232         return mcurobject;
00233     }
00234 
00235     std::string PeerParser::object()
00236     {
00237         return mlastobject;
00238     }
00239 
00240     bool PeerParser::foundPath() 
00241     {
00242         return mfoundpath;
00243     }
00244 }
00245 


rtt
Author(s): RTT Developers
autogenerated on Fri Sep 9 2016 04:01:55