Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / tests / Notify / Ordering / Structured_Supplier.cpp
blob7e7b31b43ef8a8a8999b7b7e4d6a25e2a2c7b9aa
1 #include "Notify_StructuredPushSupplier.h"
2 #include "goS.h"
4 #include "Notify_Test_Client.h"
6 #include "orbsvcs/CosNotifyChannelAdminS.h"
7 #include "orbsvcs/CosNotifyCommC.h"
8 #include "orbsvcs/CosNamingC.h"
9 #include "orbsvcs/TimeBaseC.h"
11 #include "tao/ORB_Core.h"
13 #include "ace/Get_Opt.h"
14 #include "ace/OS_NS_unistd.h"
15 #include <memory>
17 static TAO_Notify_Tests_StructuredPushSupplier* supplier_1 = 0;
18 static CORBA::Short order_policy = CosNotification::FifoOrder;
19 static int num_events = 30;
20 static const ACE_TCHAR *ior_output_file = ACE_TEXT ("supplier.ior");
22 class sig_i : public POA_sig
24 public:
25 sig_i(CORBA::ORB_ptr orb)
26 : orb_(orb)
27 , started_(false)
31 void go ()
33 started_ = true;
36 void done ()
38 started_ = false;
41 void wait_for_startup()
43 while (! started_) {
44 ACE_Time_Value tv(0, 100 * 1000); // 100ms
45 orb_->run(tv);
49 void wait_for_completion()
51 while (started_) {
52 ACE_Time_Value tv(0, 100 * 1000); // 100ms
53 orb_->run(tv);
57 private:
58 CORBA::ORB_ptr orb_;
59 bool started_;
62 class Supplier_Client : public Notify_Test_Client
64 public:
65 virtual int parse_args (int argc, ACE_TCHAR *argv[]);
69 int
70 Supplier_Client::parse_args (int argc, ACE_TCHAR *argv[])
72 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:e:d:"));
73 int c;
75 while ((c = get_opts ()) != -1)
76 switch (c)
78 case 'd':
80 const ACE_TCHAR* order = get_opts.optarg;
81 if (ACE_OS::strcmp (order, ACE_TEXT ("fifo")) == 0)
83 order_policy = CosNotification::FifoOrder;
85 else if (ACE_OS::strcmp (order, ACE_TEXT ("priority")) == 0)
87 order_policy = CosNotification::PriorityOrder;
89 else if (ACE_OS::strcmp (order, ACE_TEXT ("deadline")) == 0)
91 order_policy = CosNotification::DeadlineOrder;
92 #if !defined (ACE_HAS_TIMED_MESSAGE_BLOCKS)
93 ACE_ERROR_RETURN ((LM_ERROR,
94 "This order policy requires timed message "
95 "blocks.\nPlease #define "
96 "ACE_HAS_TIMED_MESSAGE_BLOCKS in your "
97 "config.h\n"), -1);
98 #endif
100 else
102 ACE_ERROR_RETURN ((LM_ERROR,
103 "Unknown order policy: %s\n",
104 order_policy),
105 -1);
107 break;
110 case 'e':
111 num_events = ACE_OS::atoi (get_opts.optarg);
112 break;
114 case 'o':
115 ior_output_file = get_opts.optarg;
116 break;
118 default:
119 ACE_ERROR_RETURN ((LM_ERROR,
120 "usage: %s "
121 "-o <iorfile> -e <# of events> "
122 "-d <fifo|priority|deadline>"
123 "\n",
124 argv [0]),
125 -1);
128 // Indicates successful parsing of the command line
129 return 0;
133 static CosNotifyChannelAdmin::SupplierAdmin_ptr
134 create_supplieradmin (CosNotifyChannelAdmin::EventChannel_ptr ec)
136 CosNotifyChannelAdmin::AdminID adminid = 0;
137 CosNotifyChannelAdmin::SupplierAdmin_var admin =
138 ec->new_for_suppliers (CosNotifyChannelAdmin::AND_OP,
139 adminid);
142 return CosNotifyChannelAdmin::SupplierAdmin::_duplicate (admin.in ());
146 static void
147 SendEvent (int id)
149 CosNotification::StructuredEvent event;
151 event.header.fixed_header.event_type.domain_name = CORBA::string_dup ("a");
152 event.header.fixed_header.event_type.type_name = CORBA::string_dup ("b");
153 event.header.fixed_header.event_name = CORBA::string_dup ("test");
155 event.header.variable_header.length (3);
156 event.header.variable_header[0].name = CORBA::string_dup ("id");
157 event.header.variable_header[0].value <<= (CORBA::Long) id;
158 event.header.variable_header[1].name =
159 CORBA::string_dup (CosNotification::Priority);
160 event.header.variable_header[1].value <<= (CORBA::Short) id;
161 event.header.variable_header[2].name =
162 CORBA::string_dup (CosNotification::Timeout);
163 event.header.variable_header[2].value <<= (TimeBase::TimeT) (num_events - id);
167 supplier_1->send_event (event);
169 catch (const CORBA::Exception& e)
171 e._tao_print_exception ("Error: ");
175 static void
176 create_suppliers (CosNotifyChannelAdmin::SupplierAdmin_ptr admin,
177 PortableServer::POA_ptr poa)
179 // startup the supplier
180 ACE_NEW_THROW_EX (supplier_1,
181 TAO_Notify_Tests_StructuredPushSupplier (),
182 CORBA::NO_MEMORY ());
184 supplier_1->init (poa);
186 supplier_1->connect (admin);
189 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
191 std::unique_ptr<sig_i> sig_impl;
194 Supplier_Client client;
195 int status = client.init (argc, argv);
196 ACE_UNUSED_ARG(status);
197 ACE_ASSERT(status == 0);
199 CosNotifyChannelAdmin::EventChannel_var ec =
200 client.create_event_channel ("MyEventChannel", 0);
202 CosNotification::QoSProperties qos (1);
203 qos.length (1);
204 qos[0].name = CORBA::string_dup (CosNotification::OrderPolicy);
205 qos[0].value <<= order_policy;
206 ec->set_qos (qos);
208 CORBA::ORB_ptr orb = client.orb ();
210 sig_impl.reset( new sig_i(orb));
211 sig_var sig = sig_impl->_this ();
213 CORBA::String_var ior =
214 orb->object_to_string (sig.in ());
216 if (ior_output_file != 0)
218 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
219 if (output_file == 0)
220 ACE_ERROR_RETURN ((LM_ERROR,
221 "Cannot open output file %s for "
222 "writing IOR: %C",
223 ior_output_file,
224 ior.in ()),
226 ACE_OS::fprintf (output_file, "%s", ior.in ());
227 ACE_OS::fclose (output_file);
230 CosNotifyChannelAdmin::SupplierAdmin_var admin =
231 create_supplieradmin (ec.in ());
232 ACE_ASSERT(!CORBA::is_nil (admin.in ()));
233 create_suppliers (admin.in (), client.root_poa ());
235 sig_impl->wait_for_startup();
237 ACE_DEBUG((LM_DEBUG, "1 supplier sending %d events...\n", num_events));
238 for (int i = 0; i < num_events; ++i)
240 ACE_DEBUG((LM_DEBUG, "+"));
241 SendEvent (i);
243 ACE_DEBUG((LM_DEBUG, "\nSupplier sent %d events.\n", num_events));
245 sig_impl->wait_for_completion();
247 ACE_OS::unlink (ior_output_file);
249 supplier_1->disconnect();
251 ec->destroy();
253 return 0;
255 catch (const CORBA::Exception& e)
257 e._tao_print_exception ("Error: ");
260 return 1;