ConnectorComp.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00016 #include <iostream>
00017 #include <vector>
00018 #include <string>
00019 #include <rtm/CorbaNaming.h>
00020 #include <rtm/RTObject.h>
00021 #include <rtm/NVUtil.h>
00022 #include <rtm/CORBA_SeqUtil.h>
00023 #include <rtm/CorbaConsumer.h>
00024 #include <assert.h>
00025 #include <coil/stringutil.h>
00026 
00027 
00028 using namespace RTC;
00029 
00030 void usage()
00031 {
00032   std::cout << std::endl;
00033   std::cout << "usage: ConnectorComp [options].." << std::endl;
00034   std::cout << std::endl;
00035   std::cout << "  --port *        ";
00036   std::cout << ": Set port no 2809 (default:9876)" << std::endl;
00037   std::cout << "  --origin *      ";
00038   std::cout << ": Set origin of connection OUT (default:IN)" << std::endl;
00039   std::cout << "                  : Set origin OUT ,     ConsoleOut -> ConsoleIn" << std::endl;
00040   std::cout << "                  :  default connection, ConsoleIn  -> ConsoleOut" << std::endl;
00041   std::cout << "  --flush         ";
00042   std::cout << ": Set subscription type flush (default:flush)" << std::endl;
00043   std::cout << "  --new           ";
00044   std::cout << ": Set subscription type new" << std::endl;
00045   std::cout << "  --periodic [Hz] ";
00046   std::cout << ": Set subscription type periodic" << std::endl;
00047   std::cout << "  --policy [any]  ";
00048   std::cout << ": Set push policy ALL or FIFO or SKIP or NEW" << std::endl;
00049   std::cout << "  --skip [n]      ";
00050   std::cout << ": Set skip count 0..n" << std::endl;
00051   std::cout << "  --endian *      ";
00052   std::cout << ": Set endian type Little or Big" << std::endl;
00053   std::cout << "  --buffer : Set buffer configuration" << std::endl;
00054   std::cout << "  --buffer [inport|outport].buffer.[length|[read|write.[policy|timeout]] value" << std::endl;
00055   std::cout << "    policy:  [write.full_policy,read.empty_policy]" << std::endl;
00056   std::cout << "    timeout: [write.timeout,read.timeout]" << std::endl;
00057   std::cout << "    value:  " << std::endl;
00058   std::cout << "       full_policy:  [overwrite, do_nothing, block]" << std::endl;
00059   std::cout << "       empty_policy: [readback,  do_nothing, block]" << std::endl;
00060   std::cout << "       timeout: sec (e.g. 1.0)" << std::endl;
00061   std::cout << "       length:  The length of the ring buffer. (e.g. 10)" << std::endl;
00062   std::cout << std::endl;
00063   std::cout << "exsample:" << std::endl;
00064   std::cout << "  ConnectorComp --port 2809 --origin OUT" << std::endl;
00065   std::cout << "  ConnectorComp --flush" << std::endl;
00066   std::cout << "  ConnectorComp --new" << std::endl;
00067   std::cout << "  ConnectorComp --new --policy ALL" << std::endl;
00068   std::cout << "  ConnectorComp --new --policy SKIP --skip 100" << std::endl;
00069   std::cout << "  ConnectorComp --periodic 10" << std::endl;
00070   std::cout << "  ConnectorComp --periodic 10 --policy FIFO" << std::endl;
00071   std::cout << "  ConnectorComp --periodic 10 --policy NEW" << std::endl;
00072   std::cout << "  ConnectorComp --flush --endian Little" << std::endl;
00073   std::cout << "  ConnectorComp --buffer inport.buffer.read.empty_policy block \\" << std::endl;
00074   std::cout << "                --buffer inport.buffer.read.timeout 0.1 \\ " << std::endl;
00075   std::cout << "                --buffer inport.buffer.length 10 " << std::endl;
00076   std::cout << std::endl;
00077 }
00078 
00079 int main (int argc, char** argv)
00080 {
00081   int _argc(0);
00082   char** _argv(0);
00083 
00084   std::string subs_type("flush");
00085   std::string period;
00086   std::string push_policy;
00087   std::string skip_count;
00088   std::string endian;
00089   std::string port_no("9876");
00090   std::string connect_origin("in");
00091   std::map<std::string, std::string> buffer_prop;
00092 
00093   if (argc < 2)
00094     {
00095       usage();
00096     }
00097 
00098   for (int i = 1; i < argc; ++i)
00099     {
00100       std::string arg(argv[i]);
00101       coil::normalize(arg);
00102       if (arg == "--flush")         subs_type = "flush";
00103       else if (arg == "--new")      subs_type = "new";
00104       else if (arg == "--periodic")
00105         {
00106           subs_type = "periodic";
00107           if (++i < argc) period = argv[i];
00108           else            period = "1.0";
00109         }
00110       else if (arg == "--help")
00111         {
00112           usage();
00113           exit(1);
00114         }
00115       else if (arg == "--policy")
00116         {
00117           if (++i < argc)
00118             {
00119               std::string arg2(argv[i]);
00120               coil::normalize(arg2);
00121               push_policy = arg2;
00122             }
00123           else            push_policy = "new";
00124         }
00125       else if (arg == "--skip")
00126         {
00127           if (++i < argc) skip_count = argv[i];
00128           else            skip_count = "0";
00129         }
00130 
00131       if (arg == "--endian")
00132         {
00133           if (++i < argc)
00134             {
00135               std::string arg2(argv[i]);
00136               coil::normalize(arg2);
00137               endian = arg2;
00138             }
00139           else            endian = "";
00140         }
00141       if (arg == "--port")
00142         {
00143           if (++i < argc)
00144             {
00145               port_no = argv[i];
00146             }
00147         }
00148       if (arg == "--origin")
00149         {
00150           if (++i < argc)
00151             {
00152               std::string arg2(argv[i]);
00153               coil::normalize(arg2);
00154               connect_origin = arg2;
00155             }
00156         }
00157       if (arg == "--buffer")
00158         {
00159           if (++i < argc)
00160             {
00161               std::string key(argv[i]);
00162               key = coil::normalize(key);
00163               std::string val(argv[++i]);
00164               val = coil::normalize(val);
00165               buffer_prop.insert(std::pair< std::string, std::string >(key,val));
00166             }
00167         }
00168     }
00169   
00170   CORBA::ORB_var orb = CORBA::ORB_init(_argc, _argv);
00171   std::string name_server("localhost:");
00172   name_server.append(port_no);
00173   CorbaNaming naming(orb, name_server.c_str());
00174 
00175   CorbaConsumer<RTObject> conin, conout;
00176   PortServiceList_var pin;
00177   PortServiceList_var pout;
00178 
00179   // option dump
00180   std::cout << "      Name Server: " << name_server << std::endl;
00181   std::cout << "Subscription Type: " << subs_type << std::endl;
00182   if (period != "")
00183     std::cout << "           Period: " << period << " [Hz]" << std::endl;
00184 
00185   std::cout << "      push policy: " << push_policy << std::endl;
00186   std::cout << "       skip count: " << skip_count << std::endl;
00187 
00188   if (endian != "")
00189     std::cout << "           endian: " << endian << std::endl;
00190 
00191   if (connect_origin == "in")
00192    std::cout << "          connect: ConsoleIn -> ConsoleOut" << std::endl;
00193   else
00194     std::cout << "          connect: ConsoleOut -> ConsoleIn" << std::endl;
00195   std::cout << std::endl;
00196 
00197   // find ConsoleIn0 component
00198   conin.setObject(naming.resolve("ConsoleIn0.rtc"));
00199 
00200   // get ports
00201   pin = conin->get_ports();
00202   pin[(CORBA::ULong)0]->disconnect_all();
00203   assert(pin->length() > 0);
00204   // activate ConsoleIn0
00205   ExecutionContextList_var eclisti;
00206   eclisti = conin->get_owned_contexts();
00207   eclisti[(CORBA::ULong)0]->activate_component(RTObject::_duplicate(conin._ptr()));
00208 
00209   // find ConsoleOut0 component
00210   conout.setObject(naming.resolve("ConsoleOut0.rtc"));
00211   // get ports
00212   pout = conout->get_ports();
00213   pout[(CORBA::ULong)0]->disconnect_all();
00214   assert(pout->length() > 0);
00215   // activate ConsoleOut0
00216   ExecutionContextList_var eclisto;
00217   eclisto = conout->get_owned_contexts();
00218   eclisto[(CORBA::ULong)0]->activate_component(RTObject::_duplicate(conout._ptr()));
00219 
00220   // connect ports
00221   ConnectorProfile prof;
00222   prof.connector_id = (char*)"";
00223   prof.name = CORBA::string_dup("connector0");
00224   prof.ports.length(2);
00225   prof.ports[0] = pin[(CORBA::ULong)0];
00226   prof.ports[1] = pout[(CORBA::ULong)0];
00227   CORBA_SeqUtil::push_back(prof.properties,
00228                            NVUtil::newNV("dataport.interface_type",
00229                                          "corba_cdr"));
00230   CORBA_SeqUtil::push_back(prof.properties,
00231                            NVUtil::newNV("dataport.dataflow_type",
00232                                          "push"));
00233   if (subs_type != "")
00234     CORBA_SeqUtil::push_back(prof.properties,
00235                            NVUtil::newNV("dataport.subscription_type",
00236                                          subs_type.c_str()));
00237   else
00238     CORBA_SeqUtil::push_back(prof.properties,
00239                            NVUtil::newNV("dataport.subscription_type",
00240                                          "flush"));
00241   if (subs_type == "periodic" && period != "")
00242     CORBA_SeqUtil::push_back(prof.properties,
00243                            NVUtil::newNV("dataport.publisher.push_rate",
00244                                          period.c_str()));
00245   if (push_policy != "")
00246     CORBA_SeqUtil::push_back(prof.properties,
00247                            NVUtil::newNV("dataport.publisher.push_policy",
00248                                          push_policy.c_str()));
00249   if (push_policy == "skip" && skip_count != "")
00250     CORBA_SeqUtil::push_back(prof.properties,
00251                            NVUtil::newNV("dataport.publisher.skip_count",
00252                                          skip_count.c_str()));
00253   if (endian != "")
00254   CORBA_SeqUtil::push_back(prof.properties,
00255                            NVUtil::newNV("dataport.serializer.cdr.endian",
00256                                          endian.c_str()));
00257 
00258   std::map<std::string, std::string>::iterator it=buffer_prop.begin();
00259   while (it != buffer_prop.end()) {
00260     std::string key("dataport.");
00261     key += it->first;
00262     CORBA_SeqUtil::push_back(prof.properties,
00263                              NVUtil::newNV(key.c_str(),
00264                                            it->second.c_str()));
00265     ++it;
00266   }
00267 
00268   ReturnCode_t ret;
00269   if (connect_origin == "in")
00270     {
00271       ret = pin[(CORBA::ULong)0]->connect(prof);
00272     }
00273   else
00274     {
00275       ret = pout[(CORBA::ULong)0]->connect(prof);
00276     }
00277 
00278   assert(ret == RTC::RTC_OK);
00279 
00280   std::cout << "Connector ID: " << prof.connector_id << std::endl;
00281   NVUtil::dump(prof.properties);
00282 
00283   orb->destroy();
00284   exit(1);
00285 }


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:03