-
Notifications
You must be signed in to change notification settings - Fork 1
/
Publisher.cpp
175 lines (143 loc) · 5.3 KB
/
Publisher.cpp
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
/*
*
*
* Distributed under the OpenDDS License.
* See: http://www.opendds.org/license.html
* a new comment
*/
#include <ace/Log_Msg.h>
#include <dds/DdsDcpsInfrastructureC.h>
#include <dds/DdsDcpsPublicationC.h>
#include <dds/DCPS/Marked_Default_Qos.h>
#include <dds/DCPS/Service_Participant.h>
#include <dds/DCPS/WaitSet.h>
#include "dds/DCPS/StaticIncludes.h"
#include "MessengerTypeSupportImpl.h"
#include <iostream>
int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
try {
// Initialize DomainParticipantFactory
DDS::DomainParticipantFactory_var dpf =
TheParticipantFactoryWithArgs(argc, argv);
// Create DomainParticipant
DDS::DomainParticipant_var participant =
dpf->create_participant(42,
PARTICIPANT_QOS_DEFAULT,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!participant) {
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("ERROR: %N:%l: main() -")
ACE_TEXT(" create_participant failed!\n")),
-1);
}
// Register TypeSupport (Messenger::Message)
Messenger::MessageTypeSupport_var ts =
new Messenger::MessageTypeSupportImpl;
if (ts->register_type(participant, "") != DDS::RETCODE_OK) {
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("ERROR: %N:%l: main() -")
ACE_TEXT(" register_type failed!\n")),
-1);
}
// Create Topic (Movie Discussion List)
CORBA::String_var type_name = ts->get_type_name();
DDS::Topic_var topic =
participant->create_topic("Movie Discussion List",
type_name,
TOPIC_QOS_DEFAULT,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!topic) {
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("ERROR: %N:%l: main() -")
ACE_TEXT(" create_topic failed!\n")),
-1);
}
// Create Publisher
DDS::Publisher_var publisher =
participant->create_publisher(PUBLISHER_QOS_DEFAULT,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!publisher) {
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("ERROR: %N:%l: main() -")
ACE_TEXT(" create_publisher failed!\n")),
-1);
}
// Create DataWriter
DDS::DataWriter_var writer =
publisher->create_datawriter(topic,
DATAWRITER_QOS_DEFAULT,
0,
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!writer) {
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("ERROR: %N:%l: main() -")
ACE_TEXT(" create_datawriter failed!\n")),
-1);
}
Messenger::MessageDataWriter_var message_writer =
Messenger::MessageDataWriter::_narrow(writer);
if (!message_writer) {
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("ERROR: %N:%l: main() -")
ACE_TEXT(" _narrow failed!\n")),
-1);
}
// // Block until Subscriber is available
// DDS::StatusCondition_var condition = writer->get_statuscondition();
// condition->set_enabled_statuses(DDS::PUBLICATION_MATCHED_STATUS);
// DDS::WaitSet_var ws = new DDS::WaitSet;
// ws->attach_condition(condition);
// while (true) {
// DDS::PublicationMatchedStatus matches;
// if (writer->get_publication_matched_status(matches) != ::DDS::RETCODE_OK) {
// ACE_ERROR_RETURN((LM_ERROR,
// ACE_TEXT("ERROR: %N:%l: main() -")
// ACE_TEXT(" get_publication_matched_status failed!\n")),
// -1);
// }
// if (matches.current_count >= 1) {
// break;
// }
// DDS::ConditionSeq conditions;
// DDS::Duration_t timeout = { 60, 0 };
// if (ws->wait(conditions, timeout) != DDS::RETCODE_OK) {
// ACE_ERROR_RETURN((LM_ERROR,
// ACE_TEXT("ERROR: %N:%l: main() -")
// ACE_TEXT(" wait failed!\n")),
// -1);
// }
// }
// ws->detach_condition(condition);
// Write samples
Messenger::Message message;
message.subject_id = 99;
message.from = "Comic Book Guy";
message.subject = "Review";
message.text = "Worst. Movie. Ever.";
message.count = 0;
for (;;) {
DDS::ReturnCode_t error = message_writer->write(message, DDS::HANDLE_NIL);
if (error != DDS::RETCODE_OK) {
ACE_ERROR((LM_ERROR,
ACE_TEXT("ERROR: %N:%l: main() -")
ACE_TEXT(" write returned %d!\n"), error));
}
std::cout << "Wrote message " << message.count << std::endl;
sleep (5);
++message.count;
}
// // Clean-up!
// participant->delete_contained_entities();
// dpf->delete_participant(participant);
// TheServiceParticipant->shutdown();
} catch (const CORBA::Exception& e) {
e._tao_print_exception("Exception caught in main():");
return -1;
}
return 0;
}