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
|
// $Id$
#include "Constants.h"
#include "orbsvcs/Event/EC_Lifetime_Utils_T.h"
#include "orbsvcs/Event_Utilities.h"
#include "orbsvcs/RtecEventChannelAdminC.h"
#include "orbsvcs/RtecEventCommS.h"
class EC_Consumer:
public POA_RtecEventComm::PushConsumer
{
public:
/// Constructor.
EC_Consumer (CORBA::ORB_var orb,
RtecEventChannelAdmin::EventChannel_var ec);
/// PushConsumer methods.
//@{
/// Logs each event. Initiates shutdown after receiving 100 events.
virtual void push (const RtecEventComm::EventSet &events);
/// No-op.
virtual void disconnect_push_consumer (void);
//@}
private:
/// Helper - destroys EC, shutdowns the ORB and prints number of
/// events received.
void disconnect (void);
/// Number of events pushed to us by EC.
size_t n_events_;
/// Cache these pointers for cleanup.
CORBA::ORB_var orb_;
RtecEventChannelAdmin::EventChannel_var ec_;
};
EC_Consumer::EC_Consumer (CORBA::ORB_var orb,
RtecEventChannelAdmin::EventChannel_var ec)
: n_events_ (0),
orb_ (orb),
ec_ (ec)
{
}
void
EC_Consumer::push (const RtecEventComm::EventSet &events)
{
for (CORBA::ULong i = 0; i < events.length (); ++i)
{
++this->n_events_;
ACE_DEBUG ((LM_DEBUG, " Received event\n"));
}
if (this->n_events_ >= 100)
this->disconnect ();
}
void
EC_Consumer::disconnect_push_consumer (void)
{
}
void
EC_Consumer::disconnect (void)
{
if (this->n_events_ == 100)
{
ACE_DEBUG ((LM_DEBUG,
"SUCCESS: consumer received 100 events, as expected\n"));
}
this->ec_->destroy ();
this->orb_->shutdown (0);
}
////////////////////////////////////////////////////////////
int
check_for_nil (CORBA::Object_ptr obj, const char *message)
{
if (CORBA::is_nil (obj))
ACE_ERROR_RETURN ((LM_ERROR,
"ERROR: Object reference <%s> is nil\n",
message),
-1);
else
return 0;
}
int
parse_args (int /* argc */, char ** /* argv */)
{
return 0;
}
int
main (int argc, char *argv[])
{
try
{
// Initialize ORB and POA, POA Manager, parse args.
CORBA::ORB_var orb =
CORBA::ORB_init (argc, argv);
if (parse_args (argc, argv) == -1)
return 1;
CORBA::Object_var obj =
orb->resolve_initial_references ("RootPOA");
PortableServer::POA_var poa =
PortableServer::POA::_narrow (obj.in ());
if (check_for_nil (poa.in (), "POA") == -1)
return 1;
PortableServer::POAManager_var manager =
poa->the_POAManager ();
// Obtain reference to EC.
obj = orb->resolve_initial_references ("Event_Service");
RtecEventChannelAdmin::EventChannel_var ec =
RtecEventChannelAdmin::EventChannel::_narrow (obj.in ());
if (check_for_nil (ec.in (), "EC") == -1)
return 1;
// Create the consumer and register it with POA.
TAO_EC_Servant_Var<EC_Consumer> consumer_impl =
new EC_Consumer (orb, ec);
if (!consumer_impl.in ())
return 1;
RtecEventComm::PushConsumer_var consumer;
TAO_EC_Object_Deactivator consumer_deactivator;
activate (consumer,
poa.in (),
consumer_impl.in (),
consumer_deactivator);
consumer_deactivator.disallow_deactivation ();
// Obtain reference to ConsumerAdmin.
RtecEventChannelAdmin::ConsumerAdmin_var consumer_admin =
ec->for_consumers ();
// Obtain ProxyPushSupplier and connect this consumer.
RtecEventChannelAdmin::ProxyPushSupplier_var supplier =
consumer_admin->obtain_push_supplier ();
ACE_ConsumerQOS_Factory qos;
qos.start_disjunction_group (1);
qos.insert_type (ACE_ES_EVENT_ANY, 0);
supplier->connect_push_consumer (consumer.in (),
qos.get_ConsumerQOS ());
// Allow processing of CORBA requests.
manager->activate ();
// Receive events from EC.
orb->run ();
}
catch (const CORBA::Exception& ex)
{
ex._tao_print_exception ("Exception in Consumer:");
return 1;
}
return 0;
}
|