Orocos Real-Time Toolkit  2.5.0
ConnPolicy.cpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 22 11:59:08 CEST 2009  ConnPolicy.cpp
00003 
00004                         ConnPolicy.cpp -  description
00005                            -------------------
00006     begin                : Thu October 22 2009
00007     copyright            : (C) 2009 Peter Soetens
00008     email                : peter@thesourcworks.com
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  * ConnPolicy.cpp
00041  *
00042  *  Created on: Oct 20, 2009
00043  *      Author: kaltan
00044  */
00045 
00046 #include "ConnPolicy.hpp"
00047 #include "Property.hpp"
00048 #include "PropertyBag.hpp"
00049 
00050 using namespace std;
00051 
00052 namespace RTT
00053 {
00054     ConnPolicy ConnPolicy::buffer(int size, int lock_policy /*= LOCK_FREE*/, bool init_connection /*= false*/, bool pull /*= false*/)
00055     {
00056         ConnPolicy result(BUFFER, lock_policy);
00057         result.init = init_connection;
00058         result.pull = pull;
00059         result.size = size;
00060         return result;
00061     }
00062 
00063     ConnPolicy ConnPolicy::data(int lock_policy /*= LOCK_FREE*/, bool init_connection /*= true*/, bool pull /*= false*/)
00064     {
00065         ConnPolicy result(DATA, lock_policy);
00066         result.init = init_connection;
00067         result.pull = pull;
00068         return result;
00069     }
00070 
00071     ConnPolicy::ConnPolicy(int type /* = DATA*/, int lock_policy /*= LOCK_FREE*/)
00072         : type(type), init(false), lock_policy(lock_policy), pull(false), size(0), transport(0), data_size(0) {}
00073 
00077     bool composeProperty(const PropertyBag& bag, ConnPolicy& result)
00078     {
00079         Property<int> i;
00080         Property<bool> b;
00081         Property<string> s;
00082         if ( bag.getType() != "ConnPolicy")
00083             return false;
00084         log(Debug) <<"Composing ConnPolicy..." <<endlog();
00085         i = bag.getProperty("type");
00086         if ( i.ready() )
00087             result.type = i.get();
00088         else if ( bag.find("type") ){
00089             log(Error) <<"ConnPolicy: wrong property type of 'type'."<<endlog();
00090             return false;
00091         }
00092         i = bag.getProperty("lock_policy");
00093         if ( i.ready() )
00094             result.lock_policy = i.get();
00095         else if ( bag.find("lock_policy") ){
00096             log(Error) <<"ConnPolicy: wrong property type of 'lock_policy'."<<endlog();
00097             return false;
00098         }
00099         i = bag.getProperty("size");
00100         if ( i.ready() )
00101             result.size = i.get();
00102         else if ( bag.find("size") ){
00103             log(Error) <<"ConnPolicy: wrong property type of 'size'."<<endlog();
00104             return false;
00105         }
00106         i = bag.getProperty("data_size");
00107         if ( i.ready() )
00108             result.data_size = i.get();
00109         else if ( bag.find("data_size") ){
00110             log(Error) <<"ConnPolicy: wrong property type of 'data_size'."<<endlog();
00111             return false;
00112         }
00113         i = bag.getProperty("transport");
00114         if ( i.ready() )
00115             result.transport = i.get();
00116         else if ( bag.find("transport") ){
00117             log(Error) <<"ConnPolicy: wrong property type of 'transport'."<<endlog();
00118             return false;
00119         }
00120 
00121         b = bag.getProperty("init");
00122         if ( b.ready() )
00123             result.init = b.get();
00124         else if ( bag.find("init") ){
00125             log(Error) <<"ConnPolicy: wrong property type of 'init'."<<endlog();
00126             return false;
00127         }
00128         b = bag.getProperty("pull");
00129         if ( b.ready() )
00130             result.pull = b.get();
00131         else if ( bag.find("pull") ){
00132             log(Error) <<"ConnPolicy: wrong property type of 'pull'."<<endlog();
00133             return false;
00134         }
00135 
00136         s = bag.getProperty("name_id");
00137         if ( s.ready() )
00138             result.name_id = s.get();
00139         else if ( bag.find("name_id") ){
00140             log(Error) <<"ConnPolicy: wrong property type of 'name_id'."<<endlog();
00141             return false;
00142         }
00143         return true;
00144     }
00145 
00149     void decomposeProperty(const ConnPolicy& cp, PropertyBag& targetbag)
00150     {
00151         log(Debug) <<"Decomposing ConnPolicy..." <<endlog();
00152         assert( targetbag.empty() );
00153         targetbag.setType("ConnPolicy");
00154         targetbag.ownProperty( new Property<int>("type","Data type", cp.type));
00155         targetbag.ownProperty( new Property<bool>("init","Initialize flag", cp.init));
00156         targetbag.ownProperty( new Property<int>("lock_policy","Locking Policy", cp.lock_policy));
00157         targetbag.ownProperty( new Property<bool>("pull","Fetch data over network", cp.pull));
00158         targetbag.ownProperty( new Property<int>("size","The size of a buffered connection", cp.size));
00159         targetbag.ownProperty( new Property<int>("transport","The prefered transport. Set to zero if unsure.", cp.transport));
00160         targetbag.ownProperty( new Property<int>("data_size","A hint about the data size of a single data sample. Set to zero if unsure.", cp.transport));
00161         targetbag.ownProperty( new Property<string>("name_id","The name of the connection to be formed.",cp.name_id));
00162     }
00165 }