summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Notify/Basic/ExtendedFilter.cpp
blob: 7d2504daf9742c71b52ad82b61f75062c2f9ee8f (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "ace/Arg_Shifter.h"
#include "ace/Get_Opt.h"
#include "tao/debug.h"
#include "ExtendedFilter.h"
#include "orbsvcs/CosNotifyFilterExtC.h"


ExtendedFilter::ExtendedFilter (void)
  : event_count_ (5)
{
}

ExtendedFilter::~ExtendedFilter (void)
{
}

int
ExtendedFilter::init (int argc, ACE_TCHAR* argv [])
{
  // Initialized the base class.
  Notify_Test_Client::init (argc,
                            argv);

  // Create all participents.
  this->create_EC ();

  CosNotifyChannelAdmin::AdminID adminid;

  this->supplier_admin_ =
    this->ec_->new_for_suppliers (this->ifgop_,
                                  adminid);

  ACE_ASSERT (!CORBA::is_nil (supplier_admin_.in ()));

  this->consumer_admin_ =
    this->ec_->new_for_consumers (this->ifgop_,
                                  adminid);

  ACE_ASSERT (!CORBA::is_nil (consumer_admin_.in ()));

  this->ffact_ =
    ec_->default_filter_factory ();

  return 0;
}

void
ExtendedFilter::run_test (void)
{
  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, " Obtaining FilterAdmin interface from ConsumerAdmin\n"));

  CosNotifyFilter::FilterAdmin_var ca_filter_admin =
    CosNotifyFilter::FilterAdmin::_narrow (consumer_admin_.in ());

  this->run_filter_test (consumer_admin_.in ());

  this->ec_->destroy ();
  this->ec2_->destroy ();
}

void
ExtendedFilter::run_filter_test (CosNotifyFilter::FilterAdmin_ptr filter_admin)
{
  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Narrowing CosNotifyFilterExt::FilterFactory\n"));

  CosNotifyFilterExt::FilterFactory_var extffact =
      CosNotifyFilterExt::FilterFactory::_narrow(ffact_.in());
  ACE_ASSERT (!CORBA::is_nil (extffact.in ()));

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Getting  ec2 FilterFactory\n"));
   CosNotifyFilter::FilterFactory_var  ffact2 = ec2_->default_filter_factory ();

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Narrowing CosNotifyFilterExt::FilterFactory\n"));
  CosNotifyFilterExt::FilterFactory_var extffact2 =
      CosNotifyFilterExt::FilterFactory::_narrow(ffact2.in());
  ACE_ASSERT (!CORBA::is_nil (extffact2.in ()));

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Adding a filter\n"));

  CosNotifyFilter::FilterID id_1 = this->add_filter (filter_admin);

  this->verify_filter_count (filter_admin, 1);

  if (TAO_debug_level)
    {
      ACE_DEBUG ((LM_DEBUG, "Calling print_filters\n"));
      this->print_filters (filter_admin);
    }

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Calling CosNotifyFilterExt::FilterFactory::get_filter\n"));

  CosNotifyFilter::Filter_var filter = extffact->get_filter (id_1);
  ACE_ASSERT (!CORBA::is_nil (filter.in ()));

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Calling CosNotifyFilterExt::FilterFactory::get_filterid\n"));

  CosNotifyFilter::FilterID id_dup = extffact->get_filterid (filter.in());
  ACE_ASSERT (id_dup == id_1);
#if defined (ACE_NDEBUG)
  ACE_UNUSED_ARG (id_dup );
#endif

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Calling CosNotifyFilterExt::FilterFactory::get_filterid of non existent filter\n"));

  bool id_lookup_failed = false;
  try {
    CosNotifyFilter::FilterID id_ne = extffact2->get_filterid (filter.in());
    ACE_UNUSED_ARG (id_ne );
  } catch (const CORBA::INTERNAL& ) {
    id_lookup_failed = true;
    }
  ACE_ASSERT (id_lookup_failed );
#if defined (ACE_NDEBUG)
  ACE_UNUSED_ARG (id_lookup_failed );
#endif

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Calling remove_filter\n"));

  // remove the filter.
  filter_admin->remove_filter (id_1);

  this->verify_filter_count (filter_admin, 0);

  if (TAO_debug_level)
    {
      ACE_DEBUG ((LM_DEBUG, "Calling print_filters\n"));
      this->print_filters (filter_admin);
    }

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Calling remove_all_filters\n"));

  filter_admin->remove_all_filters ();

  if (TAO_debug_level)
    {
      ACE_DEBUG ((LM_DEBUG, "Calling print_filters\n"));
      this->print_filters (filter_admin);
    }

  // Make sure all filters are removed -
  this->verify_filter_count (filter_admin, 0);

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Calling CosNotifyFilterExt::FilterFactory::remove_filter\n"));

  extffact->remove_filter(filter.in());
  filter = CosNotifyFilter::Filter::_nil ();

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Calling CosNotifyFilterExt::FilterFactory::get_filter of removed filter\n"));

  filter = extffact->get_filter (id_1);
  ACE_ASSERT (CORBA::is_nil (filter.in ()));

  ACE_DEBUG ((LM_DEBUG, "ExtendedFilters test has run successfully\n"));
}

void
ExtendedFilter::verify_filter_count (CosNotifyFilter::FilterAdmin_ptr filter_admin, CORBA::ULong expected_count)
{
  expected_count = expected_count; // if we don;t do this, we get a warning on linux about arg not used.
  CosNotifyFilter::FilterIDSeq_var filter_seq = filter_admin->get_all_filters ();
  ACE_ASSERT (filter_seq->length () == expected_count);
}

CosNotifyFilter::FilterID
ExtendedFilter::add_filter (CosNotifyFilter::FilterAdmin_ptr filter_admin)
{
  // setup a filter at the filter admin
  CosNotifyFilter::Filter_var filter =
    this->ffact_->create_filter ("ETCL");

  ACE_ASSERT (!CORBA::is_nil (filter.in ()));

  const char* test_filter_string = "A > B";

  CosNotifyFilter::ConstraintExpSeq constraint_list (1);
  constraint_list.length (1);

  constraint_list[0].event_types.length (0);
  constraint_list[0].constraint_expr = CORBA::string_dup (test_filter_string);

  CosNotifyFilter::ConstraintInfoSeq_var cons_info = filter->add_constraints (constraint_list);

  CosNotifyFilter::FilterID id = filter_admin->add_filter (filter.in ());

  // Print the ID
  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Added Filter %d\n", id));

  return id;
}

void
ExtendedFilter::print_filters (CosNotifyFilter::FilterAdmin_ptr filter_admin)
{
  CosNotifyFilter::FilterIDSeq_var filter_seq = filter_admin->get_all_filters ();

  ACE_DEBUG ((LM_DEBUG, "Getting all %d filters...\n", filter_seq->length ()));

  for (CORBA::ULong i = 0; i < filter_seq->length (); ++i)
    {
        ACE_DEBUG ((LM_DEBUG, " Filter %d\n", filter_seq[i]));
    }
}


void
ExtendedFilter::create_EC (void)
{
  CosNotifyChannelAdmin::ChannelID id, id2;

  this->ec_ = notify_factory_->create_channel (this->initial_qos_,
                                               this->initial_admin_,
                                               id);

  this->ec2_ = notify_factory_->create_channel (this->initial_qos_,
                                               this->initial_admin_,
                                               id2);

  ACE_ASSERT (!CORBA::is_nil (ec_.in ()));
}

//***************************************************************************

int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  ExtendedFilter events;

  if (events.parse_args (argc, argv) == -1)
    {
      return 1;
    }

  try
    {
      events.init (argc,
                   argv);

      events.run_test ();
    }
  catch (const CORBA::Exception& se)
    {
      se._tao_print_exception ("Error: ");
      return 1;
    }

  return 0;
}