ConnectorComp.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00016 #include <iostream>
00017 #include <vector>
00018 #include <string>
00019 #include <rtm/idl/OpenRTMSkel.h>
00020 #include <rtm/CorbaNaming.h>
00021 #include <rtm/RTObject.h>
00022 #include <rtm/NVUtil.h>
00023 #include <rtm/CORBA_SeqUtil.h>
00024 #include <rtm/CorbaConsumer.h>
00025 #include <assert.h>
00026 #include <coil/stringutil.h>
00027 
00028 
00029 using namespace RTC;
00030 
00031 void usage()
00032 {
00033   std::cout << std::endl;
00034   std::cout << "usage: ConnectorCompExt [options].." << std::endl;
00035   std::cout << std::endl;
00036   std::cout << "  --flush         ";
00037   std::cout << ": Set subscription type flush" << std::endl;
00038   std::cout << "  --new           ";
00039   std::cout << ": Set subscription type new" << std::endl;
00040   std::cout << "  --periodic [Hz] ";
00041   std::cout << ": Set subscription type periodic" << std::endl;
00042   std::cout << "  --policy [any]  ";
00043   std::cout << ": Set push policy ALL or FIFO or SKIP or NEW" << std::endl;
00044   std::cout << "  --skip [n]      ";
00045   std::cout << ": Set skip count 0..n" << std::endl;
00046   std::cout << std::endl;
00047   std::cout << "exsample:" << std::endl;
00048   std::cout << "  ConnectorCompExt --flush" << std::endl;
00049   std::cout << "  ConnectorCompExt --new" << std::endl;
00050   std::cout << "  ConnectorCompExt --new --policy ALL" << std::endl;
00051   std::cout << "  ConnectorCompExt --new --policy SKIP --skip 100" << std::endl;
00052   std::cout << "  ConnectorCompExt --periodic 10" << std::endl;
00053   std::cout << "  ConnectorCompExt --periodic 10 --policy FIFO" << std::endl;
00054   std::cout << "  ConnectorCompExt --periodic 10 --policy NEW" << std::endl;
00055   std::cout << std::endl;
00056 }
00057 
00058 int main (int argc, char** argv)
00059 {
00060   int _argc(0);
00061   char** _argv(0);
00062 
00063   std::string subs_type;
00064   std::string period;
00065   std::string push_policy;
00066   std::string skip_count;
00067   if (argc < 2)
00068     {
00069       usage();
00070     }
00071 
00072   for (int i = 1; i < argc; ++i)
00073     {
00074       std::string arg(argv[i]);
00075       coil::normalize(arg);
00076       if (arg == "--flush")         subs_type = "flush";
00077       else if (arg == "--new")      subs_type = "new";
00078       else if (arg == "--periodic")
00079         {
00080           subs_type = "periodic";
00081           if (++i < argc) period = argv[i];
00082           else            period = "1.0";
00083         }
00084       else if (arg == "--help")
00085         {
00086           usage();
00087           exit(1);
00088         }
00089       else if (arg == "--policy")
00090         {
00091           if (++i < argc)
00092             {
00093               std::string arg2(argv[i]);
00094               coil::normalize(arg2);
00095               push_policy = arg2;
00096             }
00097           else            push_policy = "new";
00098         }
00099       else if (arg == "--skip")
00100         {
00101           if (++i < argc) skip_count = argv[i];
00102           else            skip_count = "0";
00103         }
00104       else
00105         {
00106           subs_type = "flush";
00107         }
00108     }
00109   
00110   std::cout << "Subscription Type: " << subs_type << std::endl;
00111   if (period != "")
00112     std::cout << "Period: " << period << " [Hz]" << std::endl;
00113   std::cout << "push policy: " << push_policy << std::endl;
00114   std::cout << "skip count: " << skip_count << std::endl;
00115 
00116 
00117   CORBA::ORB_var orb = CORBA::ORB_init(_argc, _argv);
00118   CorbaNaming naming(orb, "localhost:9876");
00119 
00120   CorbaConsumer<RTObject> conin, conout;
00121   CorbaConsumer<OpenRTM::ExtTrigExecutionContextService> ec0, ec1;
00122 
00123   PortServiceList_var pin;
00124   PortServiceList_var pout;
00125 
00126   // find ConsoleIn0 component
00127   conin.setObject(naming.resolve("ConsoleIn0.rtc"));
00128 
00129   // get ports
00130   pin = conin->get_ports();
00131   pin[(CORBA::ULong)0]->disconnect_all();
00132   assert(pin->length() > 0);
00133   // activate ConsoleIn0
00134   ExecutionContextList_var eclisti;
00135   eclisti = conin->get_owned_contexts();
00136   eclisti[(CORBA::ULong)0]->activate_component(RTObject::_duplicate(conin._ptr()));
00137   ec0.setObject(eclisti[(CORBA::ULong)0]);
00138 
00139   // find ConsoleOut0 component
00140   conout.setObject(naming.resolve("ConsoleOut0.rtc"));
00141   // get ports
00142   pout = conout->get_ports();
00143   pout[(CORBA::ULong)0]->disconnect_all();
00144   assert(pout->length() > 0);
00145   // activate ConsoleOut0
00146   ExecutionContextList_var eclisto;
00147   eclisto = conout->get_owned_contexts();
00148   eclisto[(CORBA::ULong)0]->activate_component(RTObject::_duplicate(conout._ptr()));
00149   ec1.setObject(eclisto[(CORBA::ULong)0]);
00150 
00151   // connect ports
00152   ConnectorProfile prof;
00153   prof.connector_id = "";
00154   prof.name = CORBA::string_dup("connector0");
00155   prof.ports.length(2);
00156   prof.ports[0] = pin[(CORBA::ULong)0];
00157   prof.ports[1] = pout[(CORBA::ULong)0];
00158   CORBA_SeqUtil::push_back(prof.properties,
00159                            NVUtil::newNV("dataport.interface_type",
00160                                          "corba_cdr"));
00161   CORBA_SeqUtil::push_back(prof.properties,
00162                            NVUtil::newNV("dataport.dataflow_type",
00163                                          "push"));
00164   if (subs_type != "")
00165     CORBA_SeqUtil::push_back(prof.properties,
00166                            NVUtil::newNV("dataport.subscription_type",
00167                                          subs_type.c_str()));
00168   else
00169     CORBA_SeqUtil::push_back(prof.properties,
00170                            NVUtil::newNV("dataport.subscription_type",
00171                                          "flush"));
00172   if (subs_type == "periodic" && period != "")
00173     CORBA_SeqUtil::push_back(prof.properties,
00174                            NVUtil::newNV("dataport.publisher.push_rate",
00175                                          period.c_str()));
00176   if (push_policy != "")
00177     CORBA_SeqUtil::push_back(prof.properties,
00178                            NVUtil::newNV("dataport.publisher.push_policy",
00179                                          push_policy.c_str()));
00180   if (push_policy == "skip" && skip_count != "")
00181     CORBA_SeqUtil::push_back(prof.properties,
00182                            NVUtil::newNV("dataport.publisher.skip_count",
00183                                          skip_count.c_str()));
00184 
00185   ReturnCode_t ret;
00186   ret = pin[(CORBA::ULong)0]->connect(prof);
00187   assert(ret == RTC::RTC_OK);
00188 
00189   std::cout << "Connector ID: " << prof.connector_id << std::endl;
00190   NVUtil::dump(prof.properties);
00191 
00192   std::string cmd;
00193   while (1)
00194     {
00195       try
00196         {
00197           std::cout << std::endl;
00198           std::cout << std::endl;
00199           std::cout << "0: tick ConsoleIn component" << std::endl;
00200           std::cout << "1: tick ConsoleOut component" << std::endl;
00201           std::cout << "2: tick both components" << std::endl;
00202           std::cout << "cmd? >";
00203           std::cin >> cmd;
00204           if (cmd == "0")
00205             {
00206               ec0->tick();
00207             }
00208           else if (cmd == "1")
00209             {
00210               ec1->tick();
00211             }
00212           else if (cmd == "2")
00213             {
00214               ec0->tick();
00215               ec1->tick();
00216             }
00217         }
00218       catch (...)
00219         {
00220         }
00221     }
00222   orb->destroy();
00223   exit(1);
00224 }
00225 


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:15