-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeSyncClient.cpp
168 lines (168 loc) · 3.98 KB
/
TimeSyncClient.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
//
////rd timer
//#include <chrono>
//#include <thread>
//
//#include <stdlib.h> //atol
//#include <cmath> //lround
//#include <iostream> //cout
//
//
//#include "TimeSync.h"
//#include "DataWriter_Aux2Strings.h"
//
//using std::cerr;
//using std::cout;
//using std::endl;
//using std::string;
//
//
//long timestamp_master;
//
//long stepsCounter = 0;
//long delaySum = 0;
//
//void TimestampThread()
//{
// // master timestamp
// //increase every 10 ms
//
// timestamp_master = 0;
// std::chrono::time_point<std::chrono::system_clock> t = std::chrono::system_clock::now();
//
// while (true)
// {
// timestamp_master += 1;
// t += std::chrono::milliseconds(10);
// std::this_thread::sleep_until(t);
// }
//
//
//}
//
//
//
//void AddNextDelay(Mri::Aux2Strings auxMessage, long timestampNow) {
//
// long substraction = timestampNow - atol(auxMessage.str1);
// delaySum += substraction;
// stepsCounter++;
// //std::cout << "Add next delay Steps: " << stepsCounter << std::endl << std::endl;
//
// if (stepsCounter>4)
// { //after 5 times do
// //synchronization
// long average_delay = std::lround(delaySum / 10); // from 5 samples, then take a half delay time (1 way)
// long timestamp_perf = atol(auxMessage.str2) + average_delay;
// //std::cout << "Average delay: " << average_delay << std::endl;
// std::cout << "Average delay: " << average_delay << endl<< "Previous timestamp: " << GetTimestamp() <<endl << "Current timestamp: " << timestamp_perf << std::endl;
//
// SetTimestamp(timestamp_perf);
//
// std::cout << "TIMESTAMP SYNCHRONIZED" << " " << std::endl << std::endl;
//
//
//
// //clean-up
// delaySum = 0;
// stepsCounter = 0;
// }
//
//}
//
//
//
//long GetTimestamp() {
// return timestamp_master;
//}
//
//void SetTimestamp(long timestamp_perfect) {
//
// timestamp_master = timestamp_perfect;
//}
//
//bool SendSyncMessage() {
// Mri::Aux2Strings auxMessage;
//
// std::string s = std::to_string(GetTimestamp());
// /*char* p = new char[s.length() + 1];
// memcpy(p, s.c_str(), s.length() + 1);*/
//
//
// auxMessage.receiverId = SERVER_ID;
// auxMessage.senderId = THIS_APP_ID;
// auxMessage.tag ="timesync";
// auxMessage.str1 = s.c_str();
// auxMessage.str2 = "";
//
// int success = writer_global_aux2strings->write(auxMessage, DDS::HANDLE_NIL);
// if (success != DDS::RETCODE_OK) {
// //ACE_ERROR((LM_ERROR, ACE_TEXT("(%P|%t) ERROR: TimeSync send message write returned %d.\n"), success));
// throw std::string("ERROR: SendSyncMessage failed");
// }
// //sender.sendMessage(auxMessage);
//
// //std::cout << "Send timesync message at: " << GetTimestamp() << std::endl << std::endl;
// return true;
//}
//
//bool SynchronizeTime() {
//
// //reset variables
// stepsCounter = 0;
// delaySum = 0;
//
//
// for (size_t i = 0; i < 6; i++)
// {
// //send 5 + 1 sync messages
// // we need just 5 messages, we add one more just in case
//
// SendSyncMessage();
//
// Sleep(300);
//
// }
//
//
// return true;
//}
//
//bool ParseAux2Strings(Mri::Aux2Strings aux_message)
//{
//
// if (aux_message.receiverId!=THIS_APP_ID)
// {
// return true;
// }
//
//
// if (aux_message.senderId==SERVER_ID && strcmp(aux_message.tag, TAG_TIME_SYNC.c_str()) == 0)
// {// time synchronization message
//
// AddNextDelay(aux_message, GetTimestamp());
// //std::cout << "Timestamp: " << GetTimestamp() << std::endl << std::endl;
//
// }
//
// return true;
//}
//
//
//
//void TimeSynchronization(DDS::DomainParticipant_var m_participant,DDS::Subscriber_var m_subscriber, DDS::Publisher_var m_publisher) {
//
// DataReader_Aux2Strings reader(m_participant, m_subscriber, "Mri_Control");
// DataWriter_Aux2Strings sender(m_participant, m_publisher, "Mri_Control");
//
// //we have to repeat SynchrinizeTime. It seems it loses a few packets at the very beginning !
// std::cout << "Timestamp synchronization..." << std::endl;
// SynchronizeTime();
// Sleep(100);
// SynchronizeTime();
//
// //wait 2 sec to finish synchronization process and then dispose reader and sender
// Sleep(2000);
//
// cout << "END Timestamp Synchronization" << endl;
//}