136 # include <iostream.h>
139 #ifdef HAVE_STD_IOSTREAM
153 #include "CosEventComm.hh"
154 #include "CosEventChannelAdmin.hh"
157 static void usage(
int argc,
char **argv);
159 class Consumer_i :
virtual public POA_CosEventComm::PullConsumer {
162 void disconnect_pull_consumer ();
166 cout <<
"Pull Consumer: disconnected." << endl;
174 CORBA::ORB_ptr
orb = CORBA::ORB_init(argc,argv);
180 int sleepInterval =0;
181 const char* channelName =
"EventChannel";
184 while ((c =
getopt(argc,argv,
"td:rs:n:h")) != EOF)
188 case 't': trymode =
true;
191 case 'd': discnum = atoi(
optarg);
194 case 'r': refnil =
true;
197 case 's': sleepInterval = atoi(
optarg);
200 case 'n': channelName =
optarg;
204 default :
usage(argc,argv);
210 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
212 signal(SIGPIPE, SIG_IGN);
216 CosEventChannelAdmin::EventChannel_var channel;
218 const char* action=
"";
220 CORBA::Object_var obj;
230 action=
"resolve initial reference 'RootPOA'";
231 obj=
orb->resolve_initial_references(
"RootPOA");
232 PortableServer::POA_var rootPoa =PortableServer::POA::_narrow(obj);
233 if(CORBA::is_nil(rootPoa))
234 throw CORBA::OBJECT_NOT_EXIST();
236 action=
"activate the RootPOA's POAManager";
237 PortableServer::POAManager_var pman =rootPoa->the_POAManager();
246 action=
"convert URI from command line into object reference";
251 action=
"resolve initial reference 'NameService'";
252 obj=
orb->resolve_initial_references(
"NameService");
253 CosNaming::NamingContext_var rootContext=
254 CosNaming::NamingContext::_narrow(obj);
255 if(CORBA::is_nil(rootContext))
256 throw CORBA::OBJECT_NOT_EXIST();
258 action=
"find EventChannel in NameService";
259 cout << action << endl;
260 obj=rootContext->resolve(
str2name(channelName));
263 action=
"narrow object reference to event channel";
264 channel=CosEventChannelAdmin::EventChannel::_narrow(obj);
265 if(CORBA::is_nil(channel))
267 cerr <<
"Failed to narrow Event Channel reference." << endl;
272 catch(CORBA::ORB::InvalidName& ex) {
273 cerr<<
"Failed to "<<action<<
". ORB::InvalidName"<<endl;
276 catch(CosNaming::NamingContext::InvalidName& ex) {
277 cerr<<
"Failed to "<<action<<
". NamingContext::InvalidName"<<endl;
280 catch(CosNaming::NamingContext::NotFound& ex) {
281 cerr<<
"Failed to "<<action<<
". NamingContext::NotFound"<<endl;
284 catch(CosNaming::NamingContext::CannotProceed& ex) {
285 cerr<<
"Failed to "<<action<<
". NamingContext::CannotProceed"<<endl;
288 catch(CORBA::TRANSIENT& ex) {
289 cerr<<
"Failed to "<<action<<
". TRANSIENT"<<endl;
292 catch(CORBA::OBJECT_NOT_EXIST& ex) {
293 cerr<<
"Failed to "<<action<<
". OBJECT_NOT_EXIST"<<endl;
296 catch(CORBA::SystemException& ex) {
297 cerr<<
"Failed to "<<action<<
".";
298 #if defined(HAVE_OMNIORB4)
299 cerr<<
" "<<ex._name();
300 if(ex.NP_minorString())
301 cerr<<
" ("<<ex.NP_minorString()<<
")";
306 catch(CORBA::Exception& ex) {
307 cerr<<
"Failed to "<<action<<
"."
308 #if defined(HAVE_OMNIORB4)
317 CosEventChannelAdmin::ConsumerAdmin_var consumer_admin;
321 consumer_admin = channel->for_consumers ();
322 if (CORBA::is_nil (consumer_admin))
324 cerr <<
"Event Channel returned nil Consumer Admin!" << endl;
329 catch (CORBA::COMM_FAILURE& ex) {
330 cerr <<
"Caught COMM_FAILURE exception "
331 <<
"obtaining Consumer Admin! Retrying..."
336 cout <<
"Obtained Consumer Admin." << endl;
342 CosEventChannelAdmin::ProxyPullSupplier_var proxy_supplier;
346 proxy_supplier = consumer_admin->obtain_pull_supplier ();
347 if (CORBA::is_nil (proxy_supplier))
349 cerr <<
"Consumer Admin returned nil Proxy Supplier!" << endl;
354 catch (CORBA::COMM_FAILURE& ex) {
355 cerr <<
"Caught COMM_FAILURE Exception "
356 <<
"obtaining Pull Supplier! Retrying..."
361 cout <<
"Obtained ProxyPullSupplier." << endl;
365 CosEventComm::PullConsumer_ptr cptr =CosEventComm::PullConsumer::_nil();
367 cptr=consumer->_this();
373 proxy_supplier->connect_pull_consumer(cptr);
376 catch (CORBA::BAD_PARAM& ex) {
377 cerr <<
"Caught BAD_PARAM exception connecting Pull Consumer!"<<endl;
380 catch (CosEventChannelAdmin::AlreadyConnected& ex) {
381 cerr <<
"Proxy Pull Supplier already connected!"
385 catch (CORBA::COMM_FAILURE& ex) {
386 cerr <<
"Caught COMM_FAILURE Exception "
387 <<
"connecting Pull Consumer! Retrying..."
392 cout <<
"Connected Pull Consumer." << endl;
397 for (
int i=0; (discnum == 0) || (i < discnum); i++)
402 CORBA::Boolean has_event;
403 data = proxy_supplier->try_pull(has_event);
404 cout <<
"Consumer: try_pull() called. Data : " << flush;
414 cout <<
"None" << endl;
417 catch (CosEventComm::Disconnected& ex) {
419 cerr <<
"Failed. Caught Disconnected Exception !" << endl;
421 catch (CORBA::COMM_FAILURE& ex) {
423 cerr <<
"Failed. Caught COMM_FAILURE Exception !" << endl;
425 catch (CORBA::Exception& ex) {
427 cerr<<
"CORBA exception, unable to try_pull()"
437 cout <<
"Pull Consumer: pull() called. ";
439 data = proxy_supplier->pull();
442 cout <<
"Data : " << l << endl;
445 catch(CORBA::TRANSIENT&) {
446 cout <<
"caught TRANSIENT." << endl;
447 omni_thread::sleep(1);
449 catch (CosEventComm::Disconnected& ex) {
451 cerr <<
"Failed. Caught Disconnected exception!" << endl;
454 catch (CORBA::COMM_FAILURE& ex) {
456 cerr <<
"Failed. Caught COMM_FAILURE exception!" << endl;
459 catch (CORBA::SystemException& ex) {
461 cerr<<
"System exception, unable to pull()";
463 cerr<<
": "<<ex._name();
464 if(ex.NP_minorString())
465 cerr<<
" ("<<ex.NP_minorString()<<
")";
470 catch (CORBA::Exception& ex) {
472 cerr<<
"CORBA exception, unable to pull()"
486 proxy_supplier->disconnect_pull_supplier();
489 catch (CORBA::COMM_FAILURE& ex) {
490 cerr <<
"Caught COMM_FAILURE exception "
491 <<
"disconnecting Pull Consumer! Retrying..."
496 cout <<
"Disconnected Pull Consumer." << endl;
499 cout <<
"Sleeping " << sleepInterval <<
" seconds." << endl;
500 omni_thread::sleep(sleepInterval);
511 "\nCreate a PullConsumer to receive events from a channel.\n"
512 "syntax: "<<(argc?argv[0]:
"pullcons")<<
" OPTIONS [CHANNEL_URI]\n"
514 "CHANNEL_URI: The event channel may be specified as a URI.\n"
515 " This may be an IOR, or a corbaloc::: or corbaname::: URI.\n"
517 "OPTIONS: DEFAULT:\n"
518 " -t enable try_pull mode\n"
519 " -r connect using a nil reference\n"
520 " -d NUM disconnect after receiving NUM events [0 - never disconnect]\n"
521 " -s SECS sleep SECS seconds after disconnecting [0]\n"
522 " -n NAME channel name (if URI is not specified) [\"EventChannel\"]\n"
523 " -h display this help text\n" << endl;
int getopt(int argc, char *argv[], const char *optionS)
CosNaming::Name str2name(const char *namestr)
Converts stringified name to naming service name.
int main(int argc, char **argv)
The main process entry point.
static void usage(int argc, char **argv)
void disconnect_pull_consumer()