1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
// This supplier requires that the Notify_Service is started with
// -IOROutput notify.ior -channel -nonamesvc
// at minimum.
#include "Event_i.h"
#include "orbsvcs/CosEventCommS.h"
#include "orbsvcs/CosNotifyChannelAdminC.h"
#include "tao/corba.h"
#include "tao/ORB_Core.h"
#include <iostream>
#include <stdexcept>
#include "ace/Get_Opt.h"
const ACE_TCHAR *ec_ior_output_file = ACE_TEXT ("ec.ior");
const ACE_TCHAR *hostname = ACE_TEXT ("localhost");
const ACE_TCHAR *port = ACE_TEXT("8888");
//const char* notify_ior = "corbaloc::localhost:8888/NotifyEventChannelFactory";
int
parse_args (int argc, ACE_TCHAR *argv[])
{
ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:h:p:"));
int c;
while ((c = get_opts ()) != -1)
switch (c)
{
case 'o':
ec_ior_output_file = get_opts.opt_arg ();
break;
case 'h':
hostname = get_opts.opt_arg ();
break;
case 'p':
port = get_opts.opt_arg ();
break;
case '?':
default:
;
}
// Indicates successful parsing of the command line
return 0;
}
class TestConsumer : public POA_CosEventComm::PushConsumer
{
int num_events_;
CosEventChannelAdmin::ConsumerAdmin_var admin_;
CosEventChannelAdmin::ProxyPushSupplier_var supplier_;
PortableServer::ObjectId_var id_;
int event_count_;
bool connected_;
PortableServer::POA_ptr poa_;
CORBA::ORB_ptr orb_;
public:
TestConsumer(int evts,
CosNotifyChannelAdmin::EventChannelFactory_ptr ecf,
PortableServer::POA_ptr poa,
CORBA::ORB_ptr orb)
: num_events_(evts)
, event_count_(0)
, connected_(false)
, poa_(poa)
, orb_(orb)
{
if (CORBA::is_nil(ecf))
throw std::invalid_argument("TestConsumer::CTOR: is_nil(ecf)");
CosNotifyChannelAdmin::ChannelID id;
CosNotification::QoSProperties initial_qos;
CosNotification::AdminProperties initial_admin;
CosEventChannelAdmin::EventChannel_var ec
= ecf->create_channel (initial_qos,
initial_admin,
id);
CORBA::String_var ior = orb_->object_to_string (ec.in());
// If the ec_ior_output_file exists, output the ior to it
if (ec_ior_output_file != 0)
{
FILE *output_file= ACE_OS::fopen (ec_ior_output_file, ACE_TEXT("w"));
if (output_file == 0)
throw std::invalid_argument("Cannot open channel ior output file");
ACE_OS::fprintf (output_file, "%s", ior.in ());
ACE_OS::fclose (output_file);
}
ACE_DEBUG((LM_DEBUG, "TestConsumer: write channel ior to file %s\n", ec_ior_output_file));
admin_ = ec->for_consumers();
CORBA::Object_var obj = admin_->obtain_push_supplier();
supplier_ = CosEventChannelAdmin::ProxyPushSupplier::_unchecked_narrow(obj.in());
id_ = poa->activate_object(this);
obj = poa->id_to_reference(id_.in());
CosEventComm::PushConsumer_var consumer = CosEventComm::PushConsumer::_narrow(obj.in());
supplier_->connect_push_consumer(consumer.in());
connected_ = true;
}
virtual ~TestConsumer() {
}
virtual void disconnect_push_consumer()
{
std::cout << "disconnect_push_consumer()." << std::endl;
}
virtual void push(const CORBA::Any& a)
{
MyEvent* vt;
a >>= vt;
std::cout << std::endl
<< "Received MyEvent name=" << vt->name()
<< ", kind=" << vt->kind()
<< ", size=" << vt->size()
<< std::endl;
vt->dump();
if ( ++ event_count_ >= num_events_ && num_events_ > 0) {
std::cout << "Consumer disconnecting after receiving "
<< event_count_ << " events." << std::endl;
disconnect();
}
}
void disconnect() {
try {
if (connected_) {
connected_ = false;
poa_->deactivate_object(id_.in());
supplier_->disconnect_push_supplier();
orb_->shutdown(0);
std::cout << "Consumer disconnected." << std::endl;
}
} catch(const CORBA::Exception& e) {
std::cerr << "TestConsumer::disconnect() exception: " << e << std::endl;
}
}
void disconnect_self() {
}
};
int ACE_TMAIN (int ac, ACE_TCHAR* av[]) {
int num_events = 0;
try {
CORBA::ORB_var orb = CORBA::ORB_init(ac, av);
if (parse_args (ac, av) != 0)
return 1;
CORBA::ValueFactoryBase_var factory = new MyEventFactory;
CORBA::String_var id = _tc_MyEvent->id();
orb->register_value_factory(id.in(), factory.in());
CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa =
PortableServer::POA::_unchecked_narrow(obj.in());
PortableServer::POAManager_var mgr = poa->the_POAManager();
// "corbaloc::localhost:8888/NotifyEventChannelFactory"
ACE_CString notify_ior ("corbaloc::");
notify_ior += ACE_TEXT_ALWAYS_CHAR (hostname);
notify_ior += ":";
notify_ior += ACE_TEXT_ALWAYS_CHAR (port);
notify_ior += "/NotifyEventChannelFactory";
obj = orb->string_to_object(notify_ior.c_str());
CosNotifyChannelAdmin::EventChannelFactory_var ecf
= CosNotifyChannelAdmin::EventChannelFactory::_unchecked_narrow(obj.in());
if (ac > 1) {
num_events = ACE_OS::atoi(av[1]);
}
mgr->activate();
{
TestConsumer consumer(num_events, ecf.in(), poa.in(), orb.in());
ACE_Time_Value tvMaxRunTime(300);
orb->run(tvMaxRunTime);
}
poa->destroy(1, 1);
orb->destroy();
return 0;
} catch(const CORBA::Exception& e) {
std::cerr << "Consumer: main() exception: " << e << std::endl;
} catch(const std::invalid_argument& e) {
std::cerr << "Consumer: main() exception: " << e.what () << std::endl;
} catch(...) {
std::cerr << "Consumer: main() unknown exception: " << std::endl;
}
return 1;
}
|