Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / Notify / MC / Structured_Consumer.cpp
blob9765a241f0fd7500fce7f293e8080282e8d87971
1 // ******************************************************************
2 // Include Section
3 // ******************************************************************
5 #include "ace/Get_Opt.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/OS_NS_stdio.h"
9 #include "Notify_Structured_Push_Consumer.h"
10 #include "MonitorTestInterfaceC.h"
12 #include "Notify_Test_Client.h"
14 // ******************************************************************
15 // Data Section
16 // ******************************************************************
18 static const ACE_TCHAR *ior = ACE_TEXT ("file://test_monitor.ior");
19 static const ACE_TCHAR *ready_output_file = ACE_TEXT ("ready.txt");
20 static unsigned int expected = 2000;
21 static unsigned int delay_period = 5;
22 static unsigned int delay_count = 0;
23 static Notify_Structured_Push_Consumer* consumer_1 = 0;
25 class Consumer_Client : public Notify_Test_Client
27 public:
28 virtual int parse_args (int argc, ACE_TCHAR *argv[]);
32 int
33 Consumer_Client::parse_args (int argc, ACE_TCHAR *argv[])
35 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:e:p:d:o:"));
36 int c;
38 while ((c = get_opts ()) != -1)
39 switch (c)
41 case 'k':
42 ior = get_opts.optarg;
43 break;
45 case 'e':
46 expected = ACE_OS::atoi (get_opts.optarg);
47 break;
49 case 'p':
50 delay_period = ACE_OS::atoi (get_opts.optarg);
51 break;
53 case 'd':
54 delay_count = ACE_OS::atoi (get_opts.optarg);
55 break;
57 case 'o':
58 ready_output_file = get_opts.optarg;
59 break;
61 default:
62 ACE_ERROR_RETURN ((LM_ERROR,
63 "usage: %s "
64 "-k <ior> "
65 "-e <expected events> "
66 "-d <delay every 'n' seconds> "
67 "-p <how many seconds to delay> "
68 "-o <readyfile> -e <# of events> "
69 "\n",
70 argv [0]),
71 -1);
73 // Indicates successful parsing of the command line
74 return 0;
78 static CosNotifyChannelAdmin::ConsumerAdmin_ptr
79 create_consumeradmin (CosNotifyChannelAdmin::EventChannel_ptr ec)
81 CosNotifyChannelAdmin::AdminID adminid = 0;
82 CosNotifyChannelAdmin::ConsumerAdmin_var admin =
83 ec->new_for_consumers (CosNotifyChannelAdmin::OR_OP, adminid);
85 return CosNotifyChannelAdmin::ConsumerAdmin::_duplicate (admin.in ());
89 static void
90 create_consumers (CosNotifyChannelAdmin::ConsumerAdmin_ptr admin,
91 Notify_Test_Client* client)
93 // startup the consumer
94 ACE_NEW_THROW_EX (consumer_1,
95 Notify_Structured_Push_Consumer ("consumer1",
96 expected,
97 *client),
98 CORBA::NO_MEMORY ());
99 consumer_1->set_delay_parameters (delay_count, delay_period);
100 consumer_1->init (client->root_poa ());
101 consumer_1->_connect (admin);
104 // ******************************************************************
105 // Main Section
106 // ******************************************************************
108 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
110 int status = 0;
114 Consumer_Client client;
115 status = client.init (argc, argv);
117 if (status != 0)
119 ACE_ERROR_RETURN ((LM_ERROR,
120 "Error: Client init failed.\n"),
124 CosNotifyChannelAdmin::EventChannel_var ec =
125 client.create_event_channel ("MyEventChannel", 0);
127 #ifdef TEST_QOS_MAX_QUEUE_LENGTH
128 ACE_OS::printf ("%s: setting max queue length to 1000\n", argv[0]);
129 CosNotification::AdminProperties properties(1);
130 properties.length(1);
131 properties[0].name = CORBA::string_dup (CosNotification::MaxQueueLength);
132 properties[0].value <<= 1000;
133 ec->set_admin(properties);
134 #endif //TEST_QOS_MAX_QUEUE_LENGTH
136 CORBA::ORB_ptr orb = client.orb ();
137 CORBA::Object_var object =
138 orb->string_to_object (ior);
140 MonitorTestInterface_var sig =
141 MonitorTestInterface::_narrow (object.in ());
143 if (CORBA::is_nil (sig.in ()))
145 ACE_ERROR_RETURN ((LM_ERROR,
146 "Error: Narrow failed.\n"),
150 CosNotifyChannelAdmin::ConsumerAdmin_var admin =
151 create_consumeradmin (ec.in ());
153 if (CORBA::is_nil (admin.in ()))
155 ACE_ERROR_RETURN ((LM_ERROR,
156 "Error: nil ConsumerAdmin.\n"),
160 create_consumers (admin.in (), &client);
162 sig->running (MonitorTestInterface::Consumer);
164 ACE_DEBUG ((LM_DEBUG, "\nConsumer waiting for events...\n"));
166 FILE* ready_file = ACE_OS::fopen (ACE_TEXT_ALWAYS_CHAR(ready_output_file), "w");
168 if (ready_file == 0)
170 ACE_ERROR_RETURN ((LM_ERROR,
171 "Cannot open ready file for writing\n"),
175 ACE_OS::fprintf (ready_file, "ready\n");
176 ACE_OS::fclose (ready_file);
178 client.ORB_run ();
179 #ifdef PAUSE_ON_EXIT
180 _cputs( "All events received. Still connected.\n");
181 _cputs( "Hit a key to exit consumer: " );
182 _getch();
183 #endif // PAUSE_ON_EXIT
184 ACE_DEBUG ((LM_DEBUG, "Consumer done.\n"));
185 consumer_1->disconnect ();
187 ec->destroy ();
189 sig->finished (MonitorTestInterface::Consumer);
191 catch (const CORBA::Exception& e)
193 e._tao_print_exception ("Consumer Error: ");
194 status = 1;
197 return status;