summaryrefslogtreecommitdiff
path: root/CIAO/connectors/dds4ccm/tests/QueryCondition/DDS_OneByOne/DDS_Sender/ObO_Sender.cpp
blob: b4fd5eae5e3dcf010ed72a4be7eac04d161d2d23 (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
#include <stdio.h>
#include <iostream>

using namespace std;

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

#include "Base_ObO.h"
#include "Base_ObOSupport.h"

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

const int number_of_iterations_ = 3;

const int number_of_keys_ = 2;

int last_iteration_ = 0;
int run_ = 0;

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

void write (DDSDataWriter * writer)
{
  QueryConditionTestDataWriter* typed_writer = 0;
  typed_writer = QueryConditionTestDataWriter::narrow (writer);
  for (int iter_key = 1; iter_key < number_of_keys_ + 1; ++iter_key)
    {
      char key[7];
      QueryConditionTest new_key;
      sprintf (key, "KEY_%d", iter_key);
      new_key.symbol = key;
      for (int iter = 1;
          iter < number_of_iterations_ + 1;
          ++iter)
        {
          new_key.iteration = iter + last_iteration_;
          DDS_ReturnCode_t retcode = typed_writer->write (new_key, DDS_HANDLE_NIL);
          if (retcode != DDS_RETCODE_OK)
            {
              cout << "ERROR writing symbol <"
                  << key
                  << "> - iteration <"
                  << new_key.iteration
                  << ">"
                  << endl;
            }
         }
      cout << "Sender: Written samples with symbol <"
            << key
            << "> and iteration <"
            << last_iteration_ + 1
            << "> through <"
            << new_key.iteration
            << ">"
            << endl;
    }
  ++run_;
  last_iteration_ = number_of_iterations_ * run_;
}

int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
    DDS_ReturnCode_t    retcode;
    DDSTopic*           topic = 0;
    DDSDataWriter*      data_writer = 0;
    DDSPublisher*       publisher = 0;
    int                 main_result = 1; /* error by default */

//     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);

    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 << "SENDER: Error creating participant" << endl;
      goto clean_exit;
    }

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

    if (retcode != DDS_RETCODE_OK) {
      cerr << "SENDER: Error registering type" << endl;
      goto clean_exit;
    }

    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 << "SENDER: Error creating topic" << endl;
      goto clean_exit;
    }

    publisher = participant->create_publisher_with_profile (LIBRARY_NAME,
                                                            PROFILE_NAME,
                                                            0,
                                                            0);
    if (!publisher) {
      cerr << "SENDER: Error creating publisher" << endl;
      goto clean_exit;
    }

    data_writer = publisher->create_datawriter_with_profile (
                                               topic,
                                               LIBRARY_NAME, /* QoS */
                                               PROFILE_NAME,
                                               0,            /* Listener */
                                               DDS_STATUS_MASK_NONE);
    if (!data_writer) {
      cerr << "SENDER: Error creating data writer" << endl;
      goto clean_exit;
    }

    write (data_writer);
    sleep_now (8);
    write (data_writer);
    sleep_now (8);
    write (data_writer);
    sleep_now (30);

    main_result = 0;
clean_exit:
    cout << "SENDER: 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;
        }
    }
    DDSDomainParticipantFactory::finalize_instance ();
    return main_result;
}