summaryrefslogtreecommitdiff
path: root/CIAO/connectors/dds4ccm/tests/QueryCondition/DDS/DDS_Receiver/QC_Receiver.cpp
blob: 72620a0ce0fbcf3f5b5122742c9e0dc46924ad69 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <stdio.h>
#include <iostream>

using namespace std;

#include "Base.h"
#include "BaseSupport.h"

#include "ace/OS_main.h"
#include "ace/OS_NS_stdlib.h"

#define LIBRARY_NAME  "QueryCondition_Library"
#define PROFILE_NAME  "QueryConditionProfile"

bool shutdown_flag = false;
long num_samples = 0;
long received_samples = 0;
long expected_samples_run1 = 1 * 2;    //only sample 2 should be received of 2 keys
long expected_samples_run2 = 1 * 2;   //only sample 5 should be received o 2 keys
long expected_samples_run3 = (9 - 2) * 2; //all samples 1-9 except sample 2 and 5 of both keys.

DDSWaitSet* ws_ = new DDSWaitSet ();
DDS_Duration_t dur_ = {9, 0};

void sleep_now (int sleeptime)
{
#ifdef RTI_WIN32
        Sleep(sleeptime * 1000);
#else
        sleep(sleeptime);
#endif
}

void check_iter (const QueryConditionTest & sample,
                   const DDS_SampleInfo & readinfo,
                   const int & run)
{
  bool error = false;
  if (run == 1)
    {
      //only sample 2 should be received
      error = sample.iteration <= 1 && sample.iteration >= 3;
    }
  else if (run == 2)
    {
      //only samples 8 should be received
      error = sample.iteration <= 4 && sample.iteration >= 6;
    }
  else if (run == 3)
    {
      //all other unread samples.
      error = sample.iteration == 2;
      error |= sample.iteration ==5;
    }

  error
    ?
    cerr << "ERROR Received: key <"
        << sample.symbol
        << "> - iteration <"
        << sample.iteration
        << ">"
        << endl
    :
    cout << "Received: key <"
        << sample.symbol
        << "> - iteration <"
        << sample.iteration
        << "> - instance status <"
        << readinfo.instance_state
        << "> - view status <"
        << readinfo.view_state
        << "> - sample status <"
        << readinfo.sample_state
        << ">"
        << endl;
}


void read (DDSDataReader * dr,
           DDSQueryCondition * qc,
           DDSReadCondition * rc,
           const int & run)
{
  QueryConditionTestDataReader* typed_dr = QueryConditionTestDataReader::narrow (dr);

  DDSConditionSeq cond;

  if (ws_->wait (cond, dur_) == DDS_RETCODE_TIMEOUT)
    {
      cerr << "ERROR: wait timed out" << endl;
    }
  else
    {
      //wait a moment to allow DDS to write all samples we need here...
      QueryConditionTestSeq data;
      DDS_SampleInfoSeq     info_seq;
      sleep_now (3);
      for (DDS_Long i = 0; i < cond.length (); ++i)
        {
          if (run == 3)
            {
              sleep_now (10);
              if (cond[i] == rc)
                {
                  received_samples = 0;

                  //  **************  read all in one
                  cout << "CHECKING..... SAMPLES with read_w_condition with readcondition:" << endl;
                  typed_dr->read_w_condition (data,
                                              info_seq,
                                              DDS_LENGTH_UNLIMITED,
                                              rc);
                  for (DDS_Long y = 0; y < data.length (); ++y)
                    {
                      if (info_seq[y].valid_data)
                        {
                          check_iter (data[y], info_seq[y], run);
                          received_samples ++;
                        }
                    }
                  typed_dr->return_loan (data, info_seq);
                  if (received_samples !=  expected_samples_run3)
                    {
                      cerr << "ERROR: run  3 unexpected number of samples received : "
                           << "expected < "
                           <<  expected_samples_run3 << "> - received <"
                           << received_samples << ">" << endl;
                    }
                // end read all in one

                }
              else
                {
                  cerr << "ERROR: Should be woken up on ReadCondition" << endl;
                }
            }
          else // run 1 and 2
            {
              if (cond[i] == qc)
                {
                  received_samples = 0;
                  cout << "CHECKING..... SAMPLES with read_w_condition with querycondition:" << endl;
                  typed_dr->read_w_condition (data,
                                               info_seq,
                                              DDS_LENGTH_UNLIMITED,
                                              qc);
                  for (DDS_Long i = 0; i < data.length (); ++i)
                     {
                       if (info_seq[i].valid_data)
                         {
                           check_iter (data[i], info_seq[i], run);
                           received_samples ++;
                         }
                     }
                   typed_dr->return_loan (data, info_seq);

                  if (run == 1)
                    {
                      if (received_samples !=  expected_samples_run1)
                        {
                          cerr << "ERROR: run  1 unexpected number of samples received : "
                          << "expected < "
                          <<  expected_samples_run1 << "> - received <"
                          << received_samples << ">" << endl;
                        }
                    }
                  else
                    {
                      if (received_samples !=  expected_samples_run2)
                        {
                           cerr << "ERROR: run  2 unexpected number of samples received : "
                           << "expected < "
                           <<  expected_samples_run2 << "> - received <"
                           << received_samples << ">" << endl;
                        }
                    }
                 }
              else
                {
                  cerr << "ERROR: Should be woken up on QueryCondition" << endl;
                }
             }
        }
      if (run != 3)
        {
        }

    }

  //check if all samples are still available.
  if (run == 3)
    {
      QueryConditionTestSeq data;
      DDS_SampleInfoSeq     info_seq;
      sleep_now (3);
      typed_dr->read (data,
                      info_seq,
                      DDS_LENGTH_UNLIMITED);
      cout << "\nCHECKING..... TOTAL NUMBER OF SAMPLES IN DDS with a read : <"
             << data.length ()
             << ">"
             << endl;
      for (DDS_Long i = 0; i < data.length (); ++i)
        {
          cout << "Received: key <"
              << data[i].symbol
              << "> - iteration <"
              << data[i].iteration
              << "> - instance status <"
              << info_seq[i].instance_state
              << "> - view status <"
              << info_seq[i].view_state
              << "> - sample status <"
              << info_seq[i].sample_state
              << ">"
              << endl;
        }
      if (data.length () != 18)
        {
          cerr << "ERROR: unexpected number of samples received : "
          << "expected <18> - received <"
          << data.length () << ">" << endl;
        }
      typed_dr->return_loan (data, info_seq);
    }
    sleep_now (5);
}

int clean_up (DDSDomainParticipant * participant)
{
  DDS_ReturnCode_t    retcode = DDS_RETCODE_OK;
  int main_result = 0;

  cout << "RECEIVER: Exiting ..." << endl;
  if (participant) {
    retcode = participant->delete_contained_entities();
    if (retcode != DDS_RETCODE_OK) {
      main_result = 1;
    }
    retcode = DDSDomainParticipantFactory::get_instance()->
      delete_participant(participant);
    if (retcode != DDS_RETCODE_OK) {
      main_result = 1;
    }
  }
  delete ws_;
  DDSDomainParticipantFactory::finalize_instance ();
  return main_result;
}

int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  DDSTopic*           topic = 0;
  DDSDataReader*      data_reader = 0;
  DDSSubscriber*      sub = 0;
  DDS_ReturnCode_t    retcode = DDS_RETCODE_OK;
  QueryConditionTestDataReader* typed_dr = 0;
  DDSQueryCondition*  qc = 0;
  DDSReadCondition*   rc = 0;
  int run = 0;

//     NDDS_Config_LogVerbosity n_verbosity =
//       static_cast <NDDS_Config_LogVerbosity> (3);
//     NDDSConfigLogger::get_instance()->set_verbosity (n_verbosity);

  const ACE_TCHAR * env_domain_id = 0;
  if (argc > 1)
    {
      env_domain_id = argv[1];
    }
  if (!env_domain_id)
    {
      printf ("Environment variable DEFAULT_DOMAIN_ID not set "
              "=> setting it to 2\n");
      env_domain_id = "2";
    }
  else
    printf ("Domain ID set to %s\n", env_domain_id);

  const int domain_id = ACE_OS::atoi (env_domain_id);
  /* Create the domain participant on domain ID 0 */
  DDSDomainParticipant *participant = DDSDomainParticipantFactory::get_instance()->
      create_participant_with_profile (domain_id,    /* Domain ID */
                                       LIBRARY_NAME, /* QoS */
                                       PROFILE_NAME,
                                       0,            /* Listener */
                                       DDS_STATUS_MASK_NONE);
  if (!participant) {
    cerr << "RECEIVER: Error creating participant" << endl;
    return 1;
  }

  retcode = QueryConditionTestTypeSupport::register_type(
    participant, QueryConditionTestTypeSupport::get_type_name ());

  if (retcode != DDS_RETCODE_OK) {
    cerr << "RECEIVER: Error registering type" << endl;
    return clean_up (participant);
  }

  topic = participant->create_topic_with_profile (
                                    "QC",                   /* Topic name*/
                                    QueryConditionTestTypeSupport::get_type_name (), /* Type name */
                                    LIBRARY_NAME,       /* QoS */
                                    PROFILE_NAME,
                                    0,                  /* Listener  */
                                    DDS_STATUS_MASK_NONE);
  if (!topic) {
    cerr << "RECEIVER: Error creating topic" << endl;
    return clean_up (participant);
  }

  sub = participant->create_subscriber_with_profile (LIBRARY_NAME,
                                                     PROFILE_NAME,
                                                     0,
                                                     0);
  if (!sub) {
    cerr << "RECEIVER: Error creating subscriber" << endl;
    return clean_up (participant);
  }

  data_reader = sub->create_datareader_with_profile (
                                               topic,
                                               LIBRARY_NAME, /* QoS */
                                               PROFILE_NAME,
                                               0,            /* Listener */
                                               DDS_STATUS_MASK_NONE);
  if (!data_reader) {
    cerr << "RECEIVER: Error creating data reader" << endl;
    return clean_up (participant);
  }

  typed_dr = QueryConditionTestDataReader::narrow (data_reader);

  if (!typed_dr) {
    cerr << "RECEIVER: Unable to cast to a type specific data reader" << endl;
    return clean_up (participant);
  }

  const char* PARAMS_RUN_1[] = {"1", "3"};
  DDS_StringSeq parameters_run_1;
  parameters_run_1.from_array (PARAMS_RUN_1, 2);

  qc = typed_dr->create_querycondition (
                                DDS_NOT_READ_SAMPLE_STATE,
                                DDS_NEW_VIEW_STATE | DDS_NOT_NEW_VIEW_STATE,
                                DDS_ALIVE_INSTANCE_STATE | DDS_NOT_ALIVE_INSTANCE_STATE,
                                "iteration > %0 AND iteration < %1",
                                parameters_run_1);
  if (!qc) {
    cerr << "RECEIVER: Error creating query condition" << endl;
    return clean_up (participant);
  }
  else
    cout << "RECEIVER: query condition created : iteration > 1 AND iteration < 3" << endl;

  ws_->attach_condition (qc);

  cout << "RECEIVER: Expecting two samples (key_1 and key_2) with iteration 3." <<endl;

  read (data_reader, qc, rc, ++run);

  //Second run: change the parameters
  if (qc)
    {
      const char* PARAMS_RUN_2[] = {"4", "6"};
      DDS_StringSeq parameters_run_2;
      parameters_run_2.from_array (PARAMS_RUN_2, 2);
      if (qc->set_query_parameters (parameters_run_2) != DDS_RETCODE_OK)
        {
          cerr << "RECEIVER: Unable the set the new query parameters!!!" << endl;
          return clean_up (participant);
        }
    }
  cout << "RECEIVER: query condition changed : iteration > 4 AND iteration < 6" << endl;

  cout << "RECEIVER: Expecting two samples (key_1 and key_2) with iterations 5" <<endl;

  read (data_reader, qc, rc, ++run);
  // Third run: Detach querycondition and create read condition instead.
  // Remove the qc from the waitset and create a readcondition and start receiving
  // samples
  if (ws_->detach_condition (qc) != DDS_RETCODE_OK)
    {
      cerr << "RECEIVER: Error detaching query condition" << endl;
      return clean_up (participant);
    }
  cout << "RECEIVER: query condition deleted" << endl;
  cout << "RECEIVER: create read condition : DDS_NOT_READ_SAMPLE_STATE,"
       << "DDS_NEW_VIEW_STATE | DDS_NOT_NEW_VIEW_STATE,"
       << "DDS_ALIVE_INSTANCE_STATE | DDS_NOT_ALIVE_INSTANCE_STATE" << endl;

  // Delete the query condition from the data reader
  typed_dr->delete_readcondition (qc);
  sleep_now(10);

  rc = typed_dr->create_readcondition (
                            DDS_NOT_READ_SAMPLE_STATE,
                            DDS_NEW_VIEW_STATE | DDS_NOT_NEW_VIEW_STATE,
                            DDS_ALIVE_INSTANCE_STATE | DDS_NOT_ALIVE_INSTANCE_STATE);
  if (!rc) {
    cerr << "RECEIVER: Error creating read condition" << endl;
    return clean_up (participant);
  }

  if (ws_->attach_condition (rc) != DDS_RETCODE_OK)
    {
      cerr << "RECEIVER: Error attaching read condition" << endl;
      return clean_up (participant);
    }

  cout << "RECEIVER: Expecting ALL UNREAD samples (key_1 and key_2) with iterations between 1 and 9, except 2 and 5" <<endl;

  read (data_reader, qc, rc, ++run);

  return clean_up (participant);
}