Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / EventServices / RTEC_Basic / EchoEventSupplierMain.cpp
blob44d8d7f1944ea12f59f73d8d324faebb66bcfd1a
1 // EchoEventSupplierMain.cpp
2 // Main program for a PushSupplier of Echo events.
4 #include "EchoEventSupplier_i.h"
6 #include "orbsvcs/RtecEventCommC.h"
7 #include "orbsvcs/RtecEventChannelAdminC.h"
8 #include "orbsvcs/Time_Utilities.h"
9 #include "orbsvcs/Event_Utilities.h"
10 #include "orbsvcs/CosNamingC.h"
12 #include <iostream>
13 const RtecEventComm::EventSourceID MY_SOURCE_ID = ACE_ES_EVENT_SOURCE_ANY + 1;
14 const RtecEventComm::EventType MY_EVENT_TYPE = ACE_ES_EVENT_UNDEFINED + 1;
16 const int EVENT_DELAY_MS = 10;
18 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
20 try
22 // Initialize the ORB.
23 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
25 // Find the Naming Service.
26 CORBA::Object_var obj = orb->resolve_initial_references("NameService");
27 CosNaming::NamingContextExt_var root_context = CosNaming::NamingContextExt::_narrow(obj.in());
29 // Get the Event Channel using Naming Services
30 obj = root_context->resolve_str("EventService");
32 // Downcast the object reference to an EventChannel reference.
33 RtecEventChannelAdmin::EventChannel_var ec =
34 RtecEventChannelAdmin::EventChannel::_narrow(obj.in());
35 if (CORBA::is_nil(ec.in())) {
36 std::cerr << "Could not resolve EchoEventChannel." << std::endl;
37 return 1;
40 // Get a SupplierAdmin object from the EventChannel.
41 RtecEventChannelAdmin::SupplierAdmin_var admin = ec->for_suppliers();
43 // Get a ProxyPushConsumer from the SupplierAdmin.
44 RtecEventChannelAdmin::ProxyPushConsumer_var consumer =
45 admin->obtain_push_consumer();
47 // Get the RootPOA.
48 obj = orb->resolve_initial_references("RootPOA");
49 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in());
51 // Instantiate an EchoEventSupplier_i servant.
52 PortableServer::Servant_var<EchoEventSupplier_i> servant =
53 new EchoEventSupplier_i(orb.in());
55 // Register it with the RootPOA.
56 PortableServer::ObjectId_var oid = poa->activate_object(servant.in());
57 CORBA::Object_var supplier_obj = poa->id_to_reference(oid.in());
58 RtecEventComm::PushSupplier_var supplier =
59 RtecEventComm::PushSupplier::_narrow(supplier_obj.in());
61 // Publish the events the supplier provides.
62 ACE_SupplierQOS_Factory qos;
63 qos.insert (MY_SOURCE_ID, // Supplier's unique id
64 MY_EVENT_TYPE, // Event type
65 0, // handle to the rt_info structure
66 1); // number of calls
68 // Connect as a supplier of the published events.
69 consumer->connect_push_supplier (supplier.in (),
70 qos.get_SupplierQOS ());
72 // Activate the POA via its POAManager.
73 PortableServer::POAManager_var poa_manager = poa->the_POAManager();
74 poa_manager->activate();
76 // Create an event (just a string in this case).
77 const CORBA::String_var eventData = CORBA::string_dup("Hello, world.");
79 // Create an event set for one event
80 RtecEventComm::EventSet events (1);
81 events.length (1);
83 // Initialize event header.
84 events[0].header.source = MY_SOURCE_ID;
85 events[0].header.type = MY_EVENT_TYPE;
87 // Initialize data fields in event.
88 events[0].data.any_value <<= eventData;
90 std::cout << "Supplier starting sending of events" << std::endl;
92 while (1) {
93 consumer->push (events);
94 ACE_Time_Value tv(0, 1000 * EVENT_DELAY_MS);
95 orb->run(tv);
98 orb->destroy();
100 return 0;
102 catch(const CORBA::Exception& ex)
104 std::cerr << "Supplier::main() Caught CORBA::Exception" << std::endl << ex << std::endl;
107 return 1;