Go to the documentation of this file.00001
00010 #include "MyServiceConsumer.h"
00011 #include <rtm/CORBA_SeqUtil.h>
00012 #include <vector>
00013 #include <stdlib.h>
00014 #include <coil/Async.h>
00015 #include <functional>
00016
00017
00018
00019 static const char* myserviceconsumer_spec[] =
00020 {
00021 "implementation_id", "MyServiceConsumer",
00022 "type_name", "MyServiceConsumer",
00023 "description", "MyService Consumer Sample component",
00024 "version", "0.1",
00025 "vendor", "AIST",
00026 "category", "Generic",
00027 "activity_type", "DataFlowComponent",
00028 "max_instance", "10",
00029 "language", "C++",
00030 "lang_type", "compile",
00031 ""
00032 };
00033
00034
00035 MyServiceConsumer::MyServiceConsumer(RTC::Manager* manager)
00036 : RTC::DataFlowComponentBase(manager),
00037
00038 m_MyServicePort("MyService"),
00039
00040 async_set_value(0), async_echo(0)
00041 {
00042 }
00043
00044 MyServiceConsumer::~MyServiceConsumer()
00045 {
00046 }
00047
00048
00049 RTC::ReturnCode_t MyServiceConsumer::onInitialize()
00050 {
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 m_MyServicePort.registerConsumer("myservice0", "MyService", m_myservice0);
00061
00062
00063 addPort(m_MyServicePort);
00064
00065
00066
00067 return RTC::RTC_OK;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 RTC::ReturnCode_t MyServiceConsumer::onExecute(RTC::UniqueId ec_id)
00107 {
00108 try
00109 {
00110 std::cout << std::endl;
00111 std::cout << "Command list: " << std::endl;
00112 std::cout << " echo [msg] : echo message." << std::endl;
00113 std::cout << " set_value [value]: set value." << std::endl;
00114 std::cout << " get_value : get current value." << std::endl;
00115 std::cout << " get_echo_history : get input messsage history." << std::endl;
00116 std::cout << " get_value_history: get input value history." << std::endl;
00117 std::cout << "> ";
00118
00119 std::string args;
00120 std::string::size_type pos;
00121 std::vector<std::string> argv;
00122 std::getline(std::cin, args);
00123
00124 pos = args.find_first_of(" ");
00125 if (pos != std::string::npos)
00126 {
00127 argv.push_back(args.substr(0, pos));
00128 argv.push_back(args.substr(++pos));
00129 }
00130 else
00131 {
00132 argv.push_back(args);
00133 }
00134
00135 if (async_echo != 0 && async_echo->finished())
00136 {
00137 std::cout << "echo() finished: " << m_result << std::endl;
00138 delete async_echo;
00139 async_echo = 0;
00140 }
00141
00142 if (argv[0] == "echo" && argv.size() > 1)
00143 {
00144 if (async_echo == 0)
00145 {
00146
00147
00148 async_echo =
00149 coil::AsyncInvoker(&m_myservice0,
00150 echo_functor(argv[1], m_result));
00151 async_echo->invoke();
00152
00153 }
00154 else
00155 {
00156 std::cout << "set_value() still invoking" << std::endl;
00157 }
00158 return RTC::RTC_OK;
00159 }
00160
00161 if (argv[0] == "set_value" && argv.size() > 1)
00162 {
00163 CORBA::Float val(atof(argv[1].c_str()));
00164 coil::AsyncInvoker(&m_myservice0, set_value_functor(val),
00165 true)->invoke();
00166 std::cout << "Set remote value: " << val << std::endl;
00167
00168 return RTC::RTC_OK;
00169 }
00170
00171 if (argv[0] == "get_value")
00172 {
00173 std::cout << "Current remote value: "
00174 << m_myservice0->get_value() << std::endl;
00175 return RTC::RTC_OK;
00176 }
00177
00178 if (argv[0] == "get_echo_history")
00179 {
00180 CORBA_SeqUtil::for_each(*(m_myservice0->get_echo_history()),
00181 seq_print<const char*>());
00182 return RTC::RTC_OK;
00183 }
00184
00185 if (argv[0] == "get_value_history")
00186 {
00187 CORBA_SeqUtil::for_each(*(m_myservice0->get_value_history()),
00188 seq_print<CORBA::Float>());
00189 return RTC::RTC_OK;
00190 }
00191
00192 std::cout << "Invalid command or argument(s)." << std::endl;
00193 }
00194 catch (...)
00195 {
00196 std::cout << "No service connected." << std::endl;
00197 }
00198 return RTC::RTC_OK;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 extern "C"
00240 {
00241
00242 void MyServiceConsumerInit(RTC::Manager* manager)
00243 {
00244 coil::Properties profile(myserviceconsumer_spec);
00245 manager->registerFactory(profile,
00246 RTC::Create<MyServiceConsumer>,
00247 RTC::Delete<MyServiceConsumer>);
00248 }
00249
00250 };
00251
00252