summaryrefslogtreecommitdiff
path: root/CIAO/connectors/dds4ccm/tests/LateBinding/ReadGet/Receiver/RG_LateBinding_Receiver_impl.cpp
blob: e57ae4c813efb9e68b7f97a3e98b6f13cb8165ff (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// $Id$
#include "RG_LateBinding_Receiver_impl.h"
#include "tao/ORB_Core.h"
#include "ace/Reactor.h"

namespace CIAO_RG_LateBinding_Receiver_Impl
{

  /**
   * Check last
   */
  LastSampleChecker::LastSampleChecker (RG_LateBinding_Receiver_impl &callback,
                                        const ::CORBA::UShort &iterations)
    : callback_ (callback)
      , iterations_ (iterations)
  {
  }

  LastSampleChecker::~LastSampleChecker ()
  {
  }

  int
  LastSampleChecker::handle_timeout (const ACE_Time_Value &, const void *)
  {
    ACE_DEBUG ((LM_DEBUG, "Checking if last sample "
                          "is available in DDS...\n"));
    if (this->callback_.check_last ())
      {
        this->callback_.start_read ();
      }
    return 0;
  }

  /**
   * RG_LateBinding_Receiver_impl
   */
  RG_LateBinding_Receiver_impl::RG_LateBinding_Receiver_impl (
      ::RG_LateBinding::CCM_Receiver_Context_ptr ctx,
      const ::CORBA::UShort & iterations,
      const ::CORBA::UShort & keys)
    : ciao_context_ (
        ::RG_LateBinding::CCM_Receiver_Context::_duplicate (ctx))
    , iterations_ (iterations)
    , keys_ (keys)
  {
  }

  RG_LateBinding_Receiver_impl::~RG_LateBinding_Receiver_impl ()
  {
    delete this->checker_;
  }

  ACE_Reactor*
  RG_LateBinding_Receiver_impl::reactor (void)
  {
    ACE_Reactor* reactor = 0;
    ::CORBA::Object_var ccm_object =
      this->ciao_context_->get_CCM_object();
    if (! ::CORBA::is_nil (ccm_object.in ()))
      {
        ::CORBA::ORB_var orb = ccm_object->_get_orb ();
        if (! ::CORBA::is_nil (orb.in ()))
          {
            reactor = orb->orb_core ()->reactor ();
          }
      }
    if (reactor == 0)
      {
        throw ::CORBA::INTERNAL ();
      }
    return reactor;
  }

  bool
  RG_LateBinding_Receiver_impl::check_last (void)
  {
    ::RG_LateBinding::RG_LateBindingTestConnector::Reader_var reader =
      this->ciao_context_->get_connection_info_read_data ();
    try
      {
        RG_LateBindingTest datum;
        ::CCM_DDS::ReadInfo readinfo;
        char key[10];
        ACE_OS::sprintf (key, "KEY_%d", this->keys_);
        datum.key = CORBA::string_dup (key);
        reader->read_one_last (
                datum,
                readinfo,
                ::DDS::HANDLE_NIL);
        ACE_DEBUG ((LM_DEBUG, "Receiver_exec_i::check_last - "
                              "last iteration <%02d> - <%02d>\n",
                               datum.iteration,
                               this->iterations_));
        return datum.iteration >= this->iterations_;
      }
    catch (const ::CCM_DDS::InternalError &)
      {
      }
    catch (const ::CCM_DDS::NonExistent &)
      {
      }
    catch (...)
      {
        ACE_ERROR ((LM_ERROR, "Receiver_exec_i::check_last: "
                              "ERROR: Unexpected exception caught\n"));
      }
    return false;
  }

  void
  RG_LateBinding_Receiver_impl::start (void)
  {
    this->test_exception ();
    this->set_topic_name_reader ();

    ACE_NEW_THROW_EX (this->checker_,
                      LastSampleChecker (*this,
                                         this->iterations_),
                      ::CORBA::NO_MEMORY ());
    if (this->reactor ()->schedule_timer (this->checker_,
                                          0,
                                          ACE_Time_Value (1, 0),
                                          ACE_Time_Value (1, 0)) == -1)
      {
        ACE_ERROR ((LM_ERROR, "Receiver_exec_i::schedule_timer - "
                  "ERROR: Error while starting LastSampleChecker\n"));
      }
  }

  void
  RG_LateBinding_Receiver_impl::start_read ()
  {
    if (this->checker_)
      {
        this->reactor ()->cancel_timer (this->checker_);
      }
    this->start_reading ();
    this->set_topic_name_getter ();
    this->start_getting ();
    this->set_topic_name_reader ();
    this->set_topic_name_getter ();
  }

  void
  RG_LateBinding_Receiver_impl::check_samples (
    const char * test,
    const RG_LateBindingTestSeq& samples,
    const ::CORBA::UShort& expected)
  {
    bool error = samples.length () != expected;
    if (ACE_OS::strcmp (test, "get") == 0)
      {
        error = !(samples.length () > 0);
      }
    if (error)
      {
        ACE_ERROR ((LM_ERROR, "RG_LateBinding_Receiver_impl::check_samples - "
                    "ERROR: Unexpected number of %C samples received: "
                    "expected <%d> - received <%u>\n",
                    test,
                    expected,
                    samples.length ()));
      }
    else
      {
        ACE_DEBUG ((LM_DEBUG, "RG_LateBinding_Receiver_impl::check_samples - "
                    "%C Samples found: <%u>\n",
                    test,
                    samples.length ()));
      }
    for (::CORBA::ULong i = 0;
         i < samples.length ();
         ++i)
      {
        ACE_DEBUG ((LM_DEBUG, "RG_LateBinding_Receiver_impl::check_samples - "
                    "Sample %C: sample <%d> - key <%C> - iteration <%d>\n",
                    test,
                    i,
                    samples[i].key.in (),
                    samples[i].iteration));
      }
  }

  void
  RG_LateBinding_Receiver_impl::test_exception (void)
  {
    try
      {
        if (! ::CORBA::is_nil (this->ciao_context_.in ()))
          {
            ::RG_LateBinding::RG_LateBindingTestConnector::Reader_var reader =
              this->ciao_context_->get_connection_info_read_data ();
            if (::CORBA::is_nil (reader.in ()))
              {
                ACE_ERROR ((LM_ERROR, "RG_LateBinding_Receiver_impl::test_exception - "
                            "ERROR: Unable to get writer interface from the "
                            "CIAO context\n"));
                return;
              }

            RG_LateBindingTest sample;
            ::CCM_DDS::ReadInfo readinfo;
            sample.key = CORBA::string_dup ("KEY_1");
            reader->read_one_last (sample,
                                   readinfo,
                                   ::DDS::HANDLE_NIL);
            ACE_ERROR ((LM_ERROR, "RG_LateBinding_Receiver_impl::test_exception - "
                        "ERROR: No exception caught before topic name has been set\n"));
          }
        else
          {
            ACE_ERROR ((LM_ERROR, "RG_LateBinding_Receiver_impl::test_exception - "
                        "ERROR: CIAO context seems to be NIL\n"));
          }
      }
    catch (const ::CORBA::BAD_INV_ORDER &)
      {
        ACE_DEBUG ((LM_DEBUG, "RG_LateBinding_Receiver_impl::test_exception - "
                    "Expected BAD_INV_ORDER thrown.\n"));
      }
    catch (const CORBA::Exception &e)
      {
        e._tao_print_exception("RG_LateBinding_Receiver_impl::test_exception - "
                               "ERROR: Unexpected exception");
      }
    catch (...)
      {
        ACE_ERROR ((LM_ERROR, "RG_LateBinding_Receiver_impl::test_exception - "
                    "ERROR: expected and unknown exception caught\n"));
      }
  }

  void
  RG_LateBinding_Receiver_impl::start_reading (void)
  {
    ACE_DEBUG ((LM_DEBUG, "RG_LateBinding_Receiver_impl::start_reading - "
                "Start reading\n"));
    ::RG_LateBinding::RG_LateBindingTestConnector::Reader_var reader =
      this->ciao_context_->get_connection_info_read_data ();
    try
      {
        RG_LateBindingTestSeq samples;
        ::CCM_DDS::ReadInfoSeq readinfo_seq;
        reader->read_all (samples, readinfo_seq);
        this->check_samples ("read", samples, this->iterations_ * this->keys_);
      }
    catch (const CORBA::Exception &e)
      {
        e._tao_print_exception("RG_LateBinding_Receiver_impl::start_reading - "
                               "ERROR: Unexpected exception");
      }
    catch (...)
      {
        ACE_ERROR ((LM_ERROR, "RG_LateBinding_Receiver_impl::start_reading - "
                    "ERROR: Caught unknow exception\n"));
      }
  }

  void
  RG_LateBinding_Receiver_impl::start_getting (void)
  {
    try
      {
        ::RG_LateBinding::RG_LateBindingTestConnector::Getter_var getter =
          this->ciao_context_->get_connection_info_get_fresh_data ();
        if (::CORBA::is_nil (getter.in ()))
          {
            ACE_DEBUG ((LM_DEBUG, "RG_LateBinding_Receiver_impl::start_getting - "
                        "ERROR: Unable to get writer interface from the "
                        "CIAO context\n"));
            return;
          }
        DDS::Duration_t to;
        to.sec = 1;
        to.nanosec = 0;
        getter->time_out (to);

        RG_LateBindingTestSeq samples;
        ::CCM_DDS::ReadInfoSeq readinfos;
        getter->get_many (samples, readinfos);
        this->check_samples ("get", samples);
      }
    catch (const CORBA::Exception &e)
      {
        e._tao_print_exception("RG_LateBinding_Receiver_impl::start_getting - "
                               "ERROR: Unexpected exception");
      }
    catch (...)
      {
        ACE_ERROR ((LM_ERROR, "RG_LateBinding_Receiver_impl::start_getting - "
                    "ERROR: expected and unknown exception caught\n"));
      }
  }

  void
  RG_LateBinding_Receiver_impl::set_topic_name_reader (void)
  {
    try
      {
        ACE_DEBUG ((LM_DEBUG, "RG_LateBinding_Receiver_impl::set_topic_name_reader - "
                    "Setting topic name\n"));
        ::RG_LateBinding::RG_LateBindingTestConnector::Reader_var reader =
          this->ciao_context_->get_connection_info_read_data ();
        if (::CORBA::is_nil (reader.in ()))
            {
              ACE_ERROR ((LM_ERROR, "ERROR: RG_LateBinding_Receiver_impl::set_topic_name_reader - "
                                    "Unable to get reader interface\n"));
              throw ::CORBA::INTERNAL ();
            }
        ::CORBA::Object_var cmp = reader->_get_component ();
        if (::CORBA::is_nil (cmp.in ()))
          {
            ACE_ERROR ((LM_ERROR, "ERROR: RG_LateBinding_Receiver_impl::set_topic_name_reader - "
                                  "Unable to get component interface\n"));
            throw ::CORBA::INTERNAL ();
          }
        ::RG_LateBinding::RG_LateBindingTestConnector::CCM_DDS_State_var conn =
          ::RG_LateBinding::RG_LateBindingTestConnector::CCM_DDS_State::_narrow (cmp.in ());
        if (::CORBA::is_nil (conn.in ()))
          {
            ACE_ERROR ((LM_ERROR, "ERROR: RG_LateBinding_Receiver_impl::set_topic_name_reader - "
                                  "Unable to narrow connector interface\n"));
            throw ::CORBA::INTERNAL ();
          }
        conn->topic_name ("LateBindingTopic");
      }
    catch (const ::CCM_DDS::NonChangeable &)
      {
        ACE_ERROR ((LM_ERROR, "ERROR: RG_LateBinding_Receiver_impl::set_topic_name_reader - "
                    "Caught NonChangeable exception.\n"));
      }
  }

  void
  RG_LateBinding_Receiver_impl::set_topic_name_getter (void)
  {
    try
      {
        ACE_DEBUG ((LM_DEBUG, "RG_LateBinding_Receiver_impl::set_topic_name_getter - "
                    "Setting topic name\n"));
        ::RG_LateBinding::RG_LateBindingTestConnector::Getter_var getter =
          this->ciao_context_->get_connection_info_get_fresh_data ();
        if (::CORBA::is_nil (getter.in ()))
            {
              ACE_ERROR ((LM_ERROR, "ERROR: RG_LateBinding_Receiver_impl::set_topic_name_getter - "
                                    "Unable to get getter interface\n"));
              throw ::CORBA::INTERNAL ();
            }
          ::CORBA::Object_var cmp = getter->_get_component ();
          if (::CORBA::is_nil (cmp.in ()))
            {
              ACE_ERROR ((LM_ERROR, "ERROR: RG_LateBinding_Receiver_impl::set_topic_name_getter - "
                                    "Unable to get component interface\n"));
              throw ::CORBA::INTERNAL ();
            }
        ::RG_LateBinding::RG_LateBindingTestConnector::CCM_DDS_Event_var conn =
          ::RG_LateBinding::RG_LateBindingTestConnector::CCM_DDS_Event::_narrow (cmp.in ());
        if (::CORBA::is_nil (conn.in ()))
          {
            ACE_ERROR ((LM_ERROR, "ERROR: RG_LateBinding_Receiver_impl::set_topic_name_getter - "
                                  "Unable to narrow connector interface\n"));
            throw ::CORBA::INTERNAL ();
          }
        conn->topic_name ("LateBindingTopic");
      }
    catch (const ::CCM_DDS::NonChangeable &)
      {
        ACE_ERROR ((LM_ERROR, "RG_LateBinding_Receiver_impl::set_topic_name_getter - "
                    "ERROR: Caught NonChangeable exception.\n"));
      }
  }
}