-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNtpV4Impl.java
More file actions
141 lines (122 loc) · 3.71 KB
/
Copy pathNtpV4Impl.java
File metadata and controls
141 lines (122 loc) · 3.71 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
package nts;
import java.util.ArrayList;
import java.util.List;
import nts.NTSExtensionFields.FieldType;
import nts.NTSExtensionFields.NTSExtensionField;
/**
* Implements {@link NtpV4Packet} to convert Java objects to and from the Network Time Protocol
* (NTP) data message header format described in RFC-1305.
*/
public class NtpV4Impl extends NtpV3Impl implements NtpV4Packet {
private static final int EF_INDEX = 48;
public List<NTSExtensionField> extensionFields = new ArrayList<>();
/** Creates a new instance of NtpV4Impl */
public NtpV4Impl() {
setVersion(4); // NtpV4
}
@Override
public void addExtensionField(NTSExtensionField field) {
int idx = buf.length;
int len = field.getFieldLength();
byte[] new_buf = new byte[idx + len];
extensionFields.add(field);
byte[] bb = field.toByteArray();
System.arraycopy(buf, 0, new_buf, 0, idx);
System.arraycopy(bb, 0, new_buf, idx, field.getFieldLength());
buf = new_buf;
}
@Override
public void addExtensionFields(List<NTSExtensionField> fields) {
int extLen = 0;
for (NTSExtensionField ef : extensionFields) {
extLen += ef.getFieldLength();
}
if (extLen == 0) {
return;
}
byte[] new_buf = new byte[buf.length + extLen];
System.arraycopy(buf, 0, new_buf, 0, buf.length);
int offset = buf.length;
for (NTSExtensionField ef : extensionFields) {
byte[] fieldBytes = ef.toByteArray();
System.arraycopy(fieldBytes, 0, new_buf, offset, fieldBytes.length);
offset += fieldBytes.length;
}
buf = new_buf;
}
private void extractExtensionFields() {
int idx = EF_INDEX;
while (idx < this.buf.length) {
NTSExtensionField ef = NTSExtensionField.fromBytes(this.buf, idx);
idx += ef.getFieldLength();
extensionFields.add(ef);
}
}
@Override
public List<NTSExtensionField> getExtensionFields() {
if (extensionFields.size() == 0 && buf.length > EF_INDEX) {
extractExtensionFields();
}
return extensionFields;
}
@Override
public List<NTSExtensionField> getExtensionFields(final FieldType type) {
List<NTSExtensionField> res = new ArrayList<>();
for (NTSExtensionField ef : getExtensionFields()) {
if (ef.fieldType == type) {
res.add(ef);
}
}
return res;
}
/**
* Compares this object against the specified object. The result is {@code true} if and only if
* the argument is not {@code null} and is a <code>NtpV4Impl</code> object that contains the same
* values as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same; {@code false} otherwise.
* @since 3.4
*/
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final NtpV4Impl other = (NtpV4Impl) obj;
return java.util.Arrays.equals(buf, other.buf);
}
/**
* Returns details of NTP packet as a string.
*
* @return details of NTP packet as a string.
*/
@Override
public String toString() {
String res =
"["
+ "version:"
+ getVersion()
+ ", mode:"
+ getMode()
+ ", poll:"
+ getPoll()
+ ", precision:"
+ getPrecision()
+ ", delay:"
+ getRootDelay()
+ ", dispersion(ms):"
+ getRootDispersionInMillisDouble()
+ ", id:"
+ getReferenceIdString()
+ ", xmitTime:"
+ getTransmitTimeStamp().toDateString(); // + " ]";
for (NTSExtensionField fld : extensionFields) {
res += ", " + fld;
}
return res + " ]";
}
}