-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheProsimaClock.cpp
More file actions
347 lines (278 loc) · 9 KB
/
eProsimaClock.cpp
File metadata and controls
347 lines (278 loc) · 9 KB
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
// eProsimaClock.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "eProsimaClock.h"
#include "eProsimaClockDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CeProsimaClockApp
BEGIN_MESSAGE_MAP(CeProsimaClockApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
ON_BN_CLICKED(IDC_ENDPOINT_KIND, &CeProsimaClockApp::OnBnClickedEndpointType)
ON_UPDATE_COMMAND_UI_RANGE(IDC_PUBLISHER_NAME, IDC_VALUE, &CeProsimaClockApp::OnUpdateDisplays)
ON_UPDATE_COMMAND_UI(IDC_ENDPOINT_KIND, &CeProsimaClockApp::OnUpdateEndpointKind)
ON_UPDATE_COMMAND_UI(IDC_TIMER_STATE, &CeProsimaClockApp::OnUpdateTimerState)
END_MESSAGE_MAP()
// CeProsimaClockApp construction
CeProsimaClockApp::CeProsimaClockApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CeProsimaClockApp object
CeProsimaClockApp theApp;
// CeProsimaClockApp initialization
BOOL CeProsimaClockApp::InitInstance()
{
using namespace eprosima::fastrtps;
// fastrtps support, enable use of xml config files
ParticipantAttributes PParam;
Domain::getDefaultParticipantAttributes(PParam);
m_part.reset(Domain::createParticipant(PParam), &Domain::removeParticipant);
if(!m_part)
{
AfxMessageBox(_T("Unable to create a fastrtps participant!"), MB_ICONERROR | MB_OK);
return FALSE;
}
// register the type
if(!Domain::registerType(m_part.get(), &m_type))
{
AfxMessageBox(_T("Unable to register the idl type!"), MB_ICONERROR | MB_OK);
return FALSE;
}
// generate a subscriber by default
createSubscriber();
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
// Create the shell manager, in case the dialog contains
// any shell tree view or shell list view controls.
CShellManager *pShellManager = new CShellManager;
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
CeProsimaClockDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Delete the shell manager created above.
if (pShellManager != NULL)
{
delete pShellManager;
}
// Shutdown fastrpt operation is done before the dialog is closed to prevent
// callbacks from querying non-existent window (see OnCloseDialog)
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
void CeProsimaClockApp::OnCloseDialog()
{
using namespace eprosima::fastrtps;
// Free fastrtps resources
m_pub.reset();
m_sub.reset();
Domain::unregisterType(m_part.get(), m_type.getName());
m_part.reset();
Domain::stopAll();
}
void CeProsimaClockApp::OnUpdateDisplays(CCmdUI *pCmdUI)
{
using namespace std;
CTimeSpan ts;
CWnd * pE = pCmdUI->m_pOther;
if(pE->GetDlgCtrlID() == IDC_PUBLISHER_NAME)
{
ostringstream os;
if(m_pub)
{
// If publishing then use the publisher name
os << m_pub->getGuid();
}
else
{
auto it = m_publishers.begin();
if(it != m_publishers.end())
{
// If subscribing then use the first publisher
os << *it;
}
else
{
os << "No publishers discovered.";
}
}
pE->SetWindowText(CA2T(os.str().c_str()));
}
else
{
ts = counter;
pE->SetWindowText(ts.Format(_T("%H:%M:%S")));
}
}
void CeProsimaClockApp::OnUpdateEndpointKind(CCmdUI *pCmdUI)
{
CButton * pB = (CButton*)pCmdUI->m_pOther;
// Checked means publishing
CString caption, actual;
if(m_pub)
{
pB->SetCheck(BST_CHECKED);
caption = _T("Publishing...");
}
else
{
pB->SetCheck(BST_UNCHECKED);
caption = _T("Subscribing...");
}
pB->GetWindowText(actual);
if(caption != actual)
pB->SetWindowText(caption);
}
void CeProsimaClockApp::OnUpdateTimerState(CCmdUI *pCmdUI)
{
CButton * pB = (CButton*)pCmdUI->m_pOther;
// if subscribing disable this button
if(m_sub)
{
pB->EnableWindow(FALSE);
pB->SetWindowText(_T(""));
return;
}
// if publishing check the state to verify if a timer is working
pB->EnableWindow(TRUE);
int estado = pB->GetCheck();
CString caption, actual;
if(estado == BST_CHECKED)
{ // Parar
caption = _T("Stop");
}
else
{ // Arrancar
caption = _T("Start");
}
pB->GetWindowText(actual);
if(caption != actual)
pB->SetWindowText(caption);
}
void CeProsimaClockApp::OnBnClickedEndpointType()
{
// Flip-flop between Subscribing and Publishing
if(m_pub)
{
createSubscriber();
}
else
{
createPublisher();
}
// Update controls and channel commands through this
GetMainWnd()->UpdateDialogControls(this, TRUE);
}
// Update the publishers set
void CeProsimaClockApp::onSubscriptionMatched(eprosima::fastrtps::Subscriber* sub, eprosima::fastrtps::rtps::MatchingInfo& info) /*override*/
{
using STATUS = eprosima::fastrtps::rtps::MatchingStatus;
switch(info.status)
{
case STATUS::MATCHED_MATCHING:
m_publishers.insert(info.remoteEndpointGuid);
break;
case STATUS::REMOVED_MATCHING:
m_publishers.erase(info.remoteEndpointGuid);
break;
}
// MFC mechanism is serialized through message queues. Avoid taking any UI action
// from callbacks
GetMainWnd()->PostMessage(WM_COMMAND, MAKEWPARAM(IDC_LISTENER, 0));
}
// Update the dialog displays
void CeProsimaClockApp::onNewDataMessage(eprosima::fastrtps::Subscriber* sub) /*override*/
{
eProsimaClock val;
eprosima::fastrtps::SampleInfo_t si;
// Take the count value from the sub
if(sub->takeNextData((void*)&val, &si))
{
auto it = m_publishers.begin();
if(it != m_publishers.end())
{
// only rely information from the first publisher of the map
if(si.sample_identity.writer_guid() == *it)
{
counter = static_cast<__time64_t>(val.time());
// MFC mechanism is serialized through message queues. Avoid taking any UI action
// from callbacks
GetMainWnd()->PostMessage(WM_COMMAND, MAKEWPARAM(IDC_LISTENER,0));
}
}
}
}
// Create dialog endpoints
void CeProsimaClockApp::createPublisher()
{
using namespace eprosima::fastrtps;
using namespace eprosima::fastrtps::rtps;
// Use xml config file if exists
PublisherAttributes Wparam;
Domain::getDefaultPublisherAttributes(Wparam);
// Specify the topic
Wparam.topic.topicKind = NO_KEY;
Wparam.topic.topicDataType = "eProsimaClock";
Wparam.topic.topicName = "eProsimaTimer";
m_sub.reset();
m_pub.reset(Domain::createPublisher(m_part.get(), Wparam), &Domain::removePublisher);
// restart counting
reference = CTime::GetCurrentTime();
}
void CeProsimaClockApp::createSubscriber()
{
using namespace eprosima::fastrtps;
using namespace eprosima::fastrtps::rtps;
// Use xml config file if exists
SubscriberAttributes Rparam;
Domain::getDefaultSubscriberAttributes(Rparam);
// Specify the topic
Rparam.topic.topicKind = NO_KEY;
Rparam.topic.topicDataType = "eProsimaClock";
Rparam.topic.topicName = "eProsimaTimer";
m_pub.reset();
m_sub.reset(Domain::createSubscriber(m_part.get(), Rparam, this),&Domain::removeSubscriber);
}
void CeProsimaClockApp::OnTimer(UINT_PTR nIDEvent)
{
// Only makes sense if publishing
if(m_pub)
{
counter = CTime::GetCurrentTime()- reference;
eProsimaClock val;
val.time(static_cast<int64_t>(counter.GetTimeSpan()));
// publish the time
m_pub->write((void*)&val);
// Update the dialog controls, delegate the callbacks in the application object
// if not handled by the dialog itself
GetMainWnd()->UpdateDialogControls(this, TRUE);
}
}