OpenRTMUtil.cpp
Go to the documentation of this file.
00001 #include <rtm/CorbaNaming.h>
00002 #include "OpenRTMUtil.h"
00003 
00004 int connectPorts(RTC::PortService_ptr outPort, RTC::PortService_ptr inPort)
00005 {
00006     RTC::ConnectorProfileList_var connectorProfiles = inPort->get_connector_profiles();
00007     for(CORBA::ULong i=0; i < connectorProfiles->length(); ++i){
00008         RTC::ConnectorProfile& connectorProfile = connectorProfiles[i];
00009         RTC::PortServiceList& connectedPorts = connectorProfile.ports;
00010         
00011         for(CORBA::ULong j=0; j < connectedPorts.length(); ++j){
00012             RTC::PortService_ptr connectedPortRef = connectedPorts[j];
00013             if(connectedPortRef->_is_equivalent(outPort)){
00014                 return 1;
00015             }
00016         }
00017     }
00018     // connect ports
00019     RTC::ConnectorProfile cprof;
00020     cprof.connector_id = "";
00021     cprof.name = CORBA::string_dup("connector0");
00022     cprof.ports.length(2);
00023     cprof.ports[0] = RTC::PortService::_duplicate(inPort);
00024     cprof.ports[1] = RTC::PortService::_duplicate(outPort);
00025 
00026     CORBA_SeqUtil::push_back(cprof.properties,
00027                        NVUtil::newNV("dataport.dataflow_type",
00028                                      "Push"));
00029     CORBA_SeqUtil::push_back(cprof.properties,
00030                        NVUtil::newNV("dataport.interface_type",
00031                                      "corba_cdr"));
00032     CORBA_SeqUtil::push_back(cprof.properties,
00033                        NVUtil::newNV("dataport.subscription_type",
00034                                      "flush"));
00035     RTC::ReturnCode_t result = inPort->connect(cprof);
00036 
00037     if(result == RTC::RTC_OK)
00038         return 0;
00039     else
00040         return -1;
00041 }
00042 
00043 void activateRtc(RTC::RtcBase* pRtc)
00044 {
00045     RTC::ExecutionContextList_var eclist = pRtc->get_owned_contexts();
00046     for(CORBA::ULong i=0; i < eclist->length(); ++i){
00047         if(!CORBA::is_nil(eclist[i])){
00048             eclist[i]->activate_component(pRtc->getObjRef());
00049             break;
00050         }
00051     }
00052 }
00053 
00054 void deactivateRtc(RTC::RtcBase* pRtc)
00055 {
00056     RTC::ExecutionContextList_var eclist = pRtc->get_owned_contexts();
00057     for(CORBA::ULong i=0; i < eclist->length(); ++i){
00058         if(!CORBA::is_nil(eclist[i])){
00059             eclist[i]->deactivate_component(pRtc->getObjRef());
00060             break;
00061         }
00062     }
00063 }
00064 
00065 const char *getServiceIOR(RTC::RTObject_var rtc, 
00066                           const char *sname)
00067 {
00068     const char *ior = NULL;
00069 
00070     RTC::PortServiceList ports;
00071     ports = *(rtc->get_ports());
00072 
00073     RTC::ComponentProfile* cprof;
00074     cprof = rtc->get_component_profile();
00075     std::string portname = std::string(cprof->instance_name) + "." + sname;
00076 
00077     for(unsigned int i=0; i < ports.length(); i++)
00078         {
00079             RTC::PortService_var port = ports[i];
00080             RTC::PortProfile* prof = port->get_port_profile();
00081             if(std::string(prof->name) == portname)
00082                 {
00083                     RTC::ConnectorProfile connProfile;
00084                     connProfile.name = "noname";
00085                     connProfile.connector_id = "";
00086                     connProfile.ports.length(1);
00087                     connProfile.ports[0] = port;
00088                     port->connect(connProfile);
00089 
00090                     connProfile.properties[0].value >>= ior;
00091 
00092                     port->disconnect(connProfile.connector_id);
00093 
00094                     return ior;
00095                 }
00096         }
00097 
00098     return ior;
00099 }
00100 
00101 void setConfiguration(RTC::RTObject_var rtc, 
00102                       const std::string& name, const std::string& value)
00103 {
00104     SDOPackage::Configuration_ptr cfg = rtc->get_configuration();
00105     SDOPackage::ConfigurationSetList_var cfgsets 
00106         = cfg->get_configuration_sets();
00107     if (cfgsets->length()==0){
00108         std::cerr << "configuration set is not found" << std::endl;
00109         return;
00110     }
00111     SDOPackage::ConfigurationSet& cfgset = cfgsets[0];
00112     SDOPackage::NVList& nv = cfgset.configuration_data;
00113     for (size_t i=0; i<nv.length(); i++){
00114         if (std::string(nv[i].name) == name){
00115             nv[i].value <<= value.c_str();
00116             cfg->set_configuration_set_values(cfgset);
00117             cfg->activate_configuration_set("default");
00118             return;
00119         }
00120     }
00121     std::cerr << "can't find property(" << name << ")" << std::endl;
00122 }
00123 
00124 RTC::RTObject_var findRTC(const std::string &rtcName)
00125 {
00126     RTC::Manager& manager = RTC::Manager::instance();
00127     std::string nameServer = manager.getConfig()["corba.nameservers"];
00128     int comPos = nameServer.find(",");
00129     if (comPos < 0){
00130         comPos = nameServer.length();
00131     }
00132     nameServer = nameServer.substr(0, comPos);
00133     RTC::CorbaNaming naming(manager.getORB(), nameServer.c_str());
00134     CosNaming::Name name;
00135     name.length(1);
00136     name[0].id = CORBA::string_dup(rtcName.c_str());
00137     name[0].kind = CORBA::string_dup("rtc");
00138     try{
00139         CORBA::Object_ptr obj = naming.resolve(name);
00140         return RTC::RTObject::_narrow(obj);
00141     }catch(...){
00142         return NULL;
00143     }
00144 }
00145 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed Sep 6 2017 02:35:55