Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / Notify / Bug_1385_Regression / Structured_Supplier.cpp
blob5853851ca201d6a94803e1abd06527088f26ff22
1 // ******************************************************************
2 // Include Section
3 // ******************************************************************
5 #include "ace/Auto_Ptr.h"
6 #include "ace/Get_Opt.h"
7 #include "ace/OS_NS_unistd.h"
9 #include "tao/ORB_Core.h"
11 #include "orbsvcs/CosNotifyChannelAdminS.h"
12 #include "orbsvcs/CosNotifyCommC.h"
13 #include "orbsvcs/CosNamingC.h"
14 #include "orbsvcs/TimeBaseC.h"
16 #include "goS.h"
17 #include "Notify_StructuredPushSupplier.h"
18 #include "Notify_Test_Client.h"
21 // ******************************************************************
22 // Data Section
23 // ******************************************************************
25 static TAO_Notify_Tests_StructuredPushSupplier* supplier_1 = 0;
26 static int max_events = 10;
27 static const ACE_TCHAR *ior_output_file = ACE_TEXT ("supplier.ior");
29 // ******************************************************************
30 // Subroutine Section
31 // ******************************************************************
32 class sig_i : public POA_sig
34 public:
35 sig_i (CORBA::ORB_ptr orb)
36 : orb_ (orb),
37 started_ (false)
41 void go (void)
43 started_ = true;
46 void done (void)
48 started_ = false;
51 void wait_for_startup ()
53 while (!started_)
55 ACE_Time_Value tv (0, 100 * 1000); // 100ms
56 orb_->run (tv);
60 void wait_for_completion ()
62 while (started_)
64 ACE_Time_Value tv (0, 100 * 1000); // 100ms
65 orb_->run (tv);
69 private:
70 CORBA::ORB_ptr orb_;
71 bool started_;
74 class Supplier_Client : public Notify_Test_Client
76 public:
77 virtual int parse_args (int argc, ACE_TCHAR *argv[]);
80 int
81 Supplier_Client::parse_args (int argc, ACE_TCHAR *argv[])
83 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:e:"));
84 int c;
86 while ((c = get_opts ()) != -1)
87 switch (c)
89 case 'e':
90 max_events = ACE_OS::atoi (get_opts.optarg);
91 break;
93 case 'o':
94 ior_output_file = get_opts.optarg;
95 break;
97 default:
98 ACE_ERROR_RETURN ((LM_ERROR,
99 "usage: %s "
100 "-o <iorfile> -e <# of events> -d"
101 "\n",
102 argv [0]),
103 -1);
106 // Indicates successful parsing of the command line
107 return 0;
111 static CosNotifyChannelAdmin::SupplierAdmin_ptr
112 create_supplieradmin (CosNotifyChannelAdmin::EventChannel_ptr ec)
114 CosNotifyChannelAdmin::AdminID adminid = 0;
115 CosNotifyChannelAdmin::SupplierAdmin_var admin =
116 ec->new_for_suppliers (CosNotifyChannelAdmin::AND_OP, adminid);
118 return CosNotifyChannelAdmin::SupplierAdmin::_duplicate (admin.in ());
122 static void
123 SendEvent (int id)
125 CosNotification::StructuredEvent event;
127 event.header.fixed_header.event_type.domain_name = CORBA::string_dup ("DOC_TAO");
128 event.header.fixed_header.event_type.type_name = CORBA::string_dup ("examples");
130 event.filterable_data.length (1);
131 event.filterable_data[0].name = CORBA::string_dup ("id");
132 event.filterable_data[0].value <<= id;
136 supplier_1->send_event (event);
138 catch (const CORBA::Exception& e)
140 e._tao_print_exception ("Error: ");
144 static void
145 create_suppliers (CosNotifyChannelAdmin::SupplierAdmin_ptr admin,
146 PortableServer::POA_ptr poa)
148 // startup the supplier
149 ACE_NEW_THROW_EX (supplier_1,
150 TAO_Notify_Tests_StructuredPushSupplier (),
151 CORBA::NO_MEMORY ());
153 supplier_1->init (poa);
154 supplier_1->connect (admin);
156 CosNotification::EventTypeSeq added (1);
157 CosNotification::EventTypeSeq removed (1);
158 added.length (1);
159 removed.length (1);
160 added[0].domain_name = CORBA::string_dup ("DOC_TAO");
161 added[0].type_name = CORBA::string_dup ("examples");
163 removed[0].domain_name = CORBA::string_dup ("*");
164 removed[0].type_name = CORBA::string_dup ("*");
166 supplier_1->offer_change (added, removed);
170 // ******************************************************************
171 // Main Section
172 // ******************************************************************
174 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
176 int status = 0;
177 ACE_Auto_Ptr< sig_i > sig_impl;
180 Supplier_Client client;
181 status = client.init (argc, argv);
183 if (status == 0)
185 CosNotifyChannelAdmin::EventChannel_var ec =
186 client.create_event_channel ("MyEventChannel", 0);
188 CORBA::ORB_ptr orb = client.orb ();
190 // Activate the signaler with the POA
191 sig_impl.reset ( new sig_i ( orb ) );
192 sig_var sig = sig_impl->_this ();
194 CORBA::String_var ior =
195 orb->object_to_string (sig.in ());
197 // If the ior_output_file exists, output the ior to it
198 if (ior_output_file != 0)
200 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
201 if (output_file == 0)
202 ACE_ERROR_RETURN ((LM_ERROR,
203 "Cannot open output file %s for "
204 "writing IOR: %C",
205 ior_output_file,
206 ior.in ()),
208 ACE_OS::fprintf (output_file, "%s", ior.in ());
209 ACE_OS::fclose (output_file);
212 CosNotifyChannelAdmin::SupplierAdmin_var admin =
213 create_supplieradmin (ec.in ());
214 if (!CORBA::is_nil (admin.in ()))
216 create_suppliers (admin.in (), client.root_poa ());
218 sig_impl->wait_for_startup ();
220 ACE_DEBUG ((LM_DEBUG,
221 "1 supplier sending %d events...\n", max_events));
222 for (int i = 0; i < max_events; ++i)
224 ACE_DEBUG ((LM_DEBUG, "+"));
225 SendEvent (i);
227 ACE_DEBUG ((LM_DEBUG,
228 "\nSupplier sent %d events.\n", max_events));
230 sig_impl->wait_for_completion ();
232 ACE_OS::unlink (ior_output_file);
234 supplier_1->disconnect ();
236 ec->destroy ();
240 catch (const CORBA::Exception& e)
242 e._tao_print_exception ("Error: ");
243 status = 1;
246 return status;