Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / examples / Log / RTEvent / RTEvent_Consumer.cpp
blob8dbbc806d8e5604051527318f253b335ecf48ac0
1 #include "RTEvent_Consumer.h"
2 #include "orbsvcs/RtecEventChannelAdminC.h"
3 #include "orbsvcs/Event_Service_Constants.h"
4 #include "ace/OS_main.h"
6 #define NAMING_SERVICE_NAME "NameService"
7 #define EVENT_TLS_LOG_FACTORY_NAME "RTEventLogFactory"
9 int
10 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
12 Consumer consumer;
14 return consumer.run (argc, argv);
17 // ****************************************************************
19 Consumer::Consumer (void)
20 : event_count_ (0)
24 int
25 Consumer::run (int argc, ACE_TCHAR* argv[])
27 try
29 // ORB initialization boiler plate...
30 CORBA::ORB_var orb =
31 CORBA::ORB_init (argc, argv);
33 // Do *NOT* make a copy because we don't want the ORB to outlive
34 // the run() method.
35 this->orb_ = orb.in ();
37 if (argc <= 1)
39 ACE_ERROR ((LM_ERROR,
40 "Usage: Consumer <event_channel_ior>\n"));
41 return 1;
44 CORBA::Object_var object =
45 orb->resolve_initial_references ("RootPOA");
46 PortableServer::POA_var poa =
47 PortableServer::POA::_narrow (object.in ());
48 PortableServer::POAManager_var poa_manager =
49 poa->the_POAManager ();
50 poa_manager->activate ();
52 // Obtain the event channel, we could use a naming service, a
53 // command line argument or resolve_initial_references(), but
54 // this is simpler...
55 /* object =
56 orb->string_to_object (argv[1]);
58 RtecEventChannelAdmin::EventChannel_var event_channel =
59 RtecEventChannelAdmin::EventChannel::_narrow (object.in ());
62 // Obtain the event channel
63 CORBA::Object_var naming_obj =
64 this->orb_->resolve_initial_references (NAMING_SERVICE_NAME);
66 // Need to check return value for errors.
67 if (CORBA::is_nil (naming_obj.in ()))
68 throw CORBA::UNKNOWN ();
70 this->naming_context_ =
71 CosNaming::NamingContext::_narrow (naming_obj.in ());
73 CosNaming::Name name (1);
74 name.length (1);
75 name[0].id = CORBA::string_dup (EVENT_TLS_LOG_FACTORY_NAME);
77 CORBA::Object_var obj =
78 this->naming_context_->resolve (name);
80 this->event_log_factory_ =
81 RTEventLogAdmin::EventLogFactory::_narrow (obj.in ());
84 // The canonical protocol to connect to the EC
86 this->supplier_ =
87 this->event_log_factory_->obtain_push_supplier ();
89 RtecEventComm::PushConsumer_var consumer =
90 this->_this ();
92 // Simple subscription, but usually the helper classes in
93 // $TAO_ROOT/orbsvcs/Event_Utils.h are a better way to do this.
94 RtecEventChannelAdmin::ConsumerQOS qos;
95 qos.dependencies.length (2);
96 RtecEventComm::EventHeader& h0 =
97 qos.dependencies[0].event.header;
98 h0.type = ACE_ES_DISJUNCTION_DESIGNATOR;
99 h0.source = ACE_ES_EVENT_SOURCE_ANY;
101 RtecEventComm::EventHeader& h1 =
102 qos.dependencies[1].event.header;
103 h1.type = ACE_ES_EVENT_UNDEFINED; // first free event type
104 h1.source = ACE_ES_EVENT_SOURCE_ANY;
106 this->supplier_->connect_push_consumer (consumer.in (), qos);
108 // Wait for events, using work_pending()/perform_work() may help
109 // or using another thread, this example is too simple for that.
110 orb->run ();
112 // We don't do any cleanup, it is hard to do it after shutdown,
113 // and would complicate the example; plus it is almost
114 // impossible to do cleanup after ORB->run() because the POA is
115 // in the holding state. Applications should use
116 // work_pending()/perform_work() to do more interesting stuff.
117 // Check the supplier for the proper way to do cleanup.
119 catch (const CORBA::Exception& ex)
121 ex._tao_print_exception ("Consumer::run");
122 return 1;
124 return 0;
127 void
128 Consumer::push (const RtecEventComm::EventSet& events)
130 ACE_UNUSED_ARG (events);
132 this->event_count_ ++;
134 ACE_DEBUG ((LM_DEBUG,
135 "Consumer (%P|%t): %d log generated events received\n",
136 this->event_count_));
139 void
140 Consumer::disconnect_push_consumer (void)
142 // In this example we shutdown the ORB when we disconnect from the
143 // EC (or rather the EC disconnects from us), but this doesn't have
144 // to be the case....
145 this->orb_->shutdown (0);