Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / CosEvent / Timeout / TimeoutTestMain.cpp
blob98172797c7bb186a6934f168195879d695cee3b0
1 #include "TestEventConsumer_i.h"
3 #include "orbsvcs/CosEventCommC.h"
4 #include "orbsvcs/CosEventChannelAdminC.h"
5 #include "orbsvcs/CosNamingC.h"
7 #include "ace/Auto_Ptr.h"
8 #include "ace/Task.h"
9 #include "ace/Log_Msg.h"
10 #include "ace/OS_NS_strings.h"
11 #include "ace/OS_NS_unistd.h"
13 namespace
15 const char *forty_bytes = "1234567890123456789012345678901234567890";
18 struct SupplierTask : ACE_Task_Base
20 static const int N_ITERATIONS = 15;
22 SupplierTask (CosEventChannelAdmin::ProxyPushConsumer_ptr ppc,
23 CORBA::ORB_ptr orb)
24 : consumer_ (CosEventChannelAdmin::ProxyPushConsumer::_duplicate (ppc)),
25 orb_ (CORBA::ORB::_duplicate (orb))
29 CosEventChannelAdmin::ProxyPushConsumer_var consumer_;
30 CORBA::ORB_var orb_;
32 int svc()
34 // Create an event (just a string in this case).
35 CORBA::String_var eventData = forty_bytes;
36 int delay_ms = 500;
38 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Supplier starting...\n")));
40 for (int i = 0; i < N_ITERATIONS; ++i)
42 // Insert the event data into an any.
43 CORBA::Any any;
44 any <<= eventData.in ();
46 // Now push the event to the consumer
47 this->consumer_->push (any);
49 ACE_Time_Value event_delay (0, 1000 * delay_ms);
50 ACE_OS::sleep (event_delay);
52 this->orb_->shutdown ();
53 return 0;
57 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
59 try
61 // Initialize the ORB.
62 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
63 CORBA::ORB_var s_orb;
65 // Find the Naming Service.
66 CORBA::Object_var obj = orb->resolve_initial_references ("NameService");
67 CosNaming::NamingContextExt_var root_context =
68 CosNaming::NamingContextExt::_narrow (obj.in ());
70 obj = root_context->resolve_str ("CosEventService");
72 // Downcast the object reference to an EventChannel reference.
73 CosEventChannelAdmin::EventChannel_var ec =
74 CosEventChannelAdmin::EventChannel::_narrow (obj.in ());
75 if (CORBA::is_nil (ec.in ()))
77 ACE_DEBUG ((LM_DEBUG,
78 ACE_TEXT ("Could not narrow the EventChannel.\n")));
79 return 1;
81 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Found the EventChannel.\n")));
83 bool consumer = false;
84 bool supplier = false;
85 bool hang = false;
86 for (int i=1; i < argc; ++i)
88 if (0 == ACE_OS::strcasecmp (argv[i], ACE_TEXT ("-consumer")))
89 consumer = true;
90 else if (0 == ACE_OS::strcasecmp (argv[i], ACE_TEXT ("-supplier")))
91 supplier = true;
92 else if (0 == ACE_OS::strcasecmp (argv[i], ACE_TEXT ("-hang")))
93 hang = true;
96 TestEventConsumer_i servant (orb.in (), hang);
98 if (consumer)
100 // Register it with the RootPOA.
101 obj = orb->resolve_initial_references ("RootPOA");
102 PortableServer::POA_var poa =
103 PortableServer::POA::_narrow (obj.in ());
104 PortableServer::ObjectId_var oid = poa->activate_object (&servant);
105 CORBA::Object_var consumer_obj = poa->id_to_reference (oid.in ());
106 CosEventComm::PushConsumer_var consumer =
107 CosEventComm::PushConsumer::_narrow (consumer_obj.in ());
109 // Get a ConsumerAdmin object from the EventChannel.
110 CosEventChannelAdmin::ConsumerAdmin_var consumerAdmin =
111 ec->for_consumers ();
113 // Get a ProxyPushSupplier from the ConsumerAdmin.
114 CosEventChannelAdmin::ProxyPushSupplier_var supplier =
115 consumerAdmin->obtain_push_supplier ();
117 // Connect to the ProxyPushSupplier, passing our PushConsumer object
118 // reference to it.
119 supplier->connect_push_consumer (consumer.in ());
120 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Consumer connected\n")));
122 // Activate the POA via its POAManager.
123 PortableServer::POAManager_var poa_manager = poa->the_POAManager ();
124 poa_manager->activate ();
125 servant.activate ();
128 ACE_Auto_Ptr<SupplierTask> pST;
129 if (supplier)
131 // The supplier will use its own ORB.
132 CORBA::String_var ec_str = orb->object_to_string (ec.in ());
134 int no_args = 0;
135 ACE_TCHAR **no_argv = 0;
136 s_orb = CORBA::ORB_init (no_args, no_argv,
137 "Supplier_pure_client_ORB");
139 CORBA::Object_var s_ec_obj = s_orb->string_to_object (ec_str.in ());
141 CosEventChannelAdmin::EventChannel_var s_ec =
142 CosEventChannelAdmin::EventChannel::_narrow (s_ec_obj.in ());
144 // Get a SupplierAdmin object from the EventChannel.
145 CosEventChannelAdmin::SupplierAdmin_var supplierAdmin =
146 s_ec->for_suppliers ();
148 // Get a ProxyPushConsumer from the SupplierAdmin.
149 CosEventChannelAdmin::ProxyPushConsumer_var consumer =
150 supplierAdmin->obtain_push_consumer ();
152 // Connect to the ProxyPushConsumer as a PushSupplier
153 // (passing a nil PushSupplier object reference to it because
154 // we don't care to be notified about disconnects).
155 consumer->connect_push_supplier
156 (CosEventComm::PushSupplier::_nil ());
158 SupplierTask *tmp = 0;
159 ACE_NEW_RETURN (tmp, SupplierTask (consumer.in (), s_orb.in ()), -1);
160 pST.reset (tmp);
161 pST->activate ();
164 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Ready to receive events...\n")));
166 // Enter the ORB event loop.
167 orb->run ();
169 ACE_Thread_Manager::instance ()->wait ();
171 if (!CORBA::is_nil (s_orb.in ()))
173 s_orb->destroy ();
176 orb->destroy ();
177 return 0;
179 catch (const CORBA::Exception &ex)
181 ex._tao_print_exception (
182 ACE_TEXT (
183 "TimeoutTest: Caught CORBA::Exception:"));
186 return 1;