1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.metricshub.wbem.sblim.slp.internal.msg;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 import org.metricshub.wbem.sblim.slp.ServiceLocationException;
46 import org.metricshub.wbem.sblim.slp.internal.SLPConfig;
47 import org.metricshub.wbem.sblim.slp.internal.SLPDefaults;
48
49
50
51
52
53 public abstract class SLPMessage implements FunctionIDs {
54
55 private final int iMaxDatagramSize = SLPConfig.getGlobalCfg().getMTU();
56
57 private MsgHeader iHeader;
58
59
60
61
62
63
64 public SLPMessage(int pFunctionID) {
65 this(pFunctionID, Util.getLangTag(SLPDefaults.LOCALE));
66 }
67
68
69
70
71
72
73
74 public SLPMessage(int pFunctionID, String pLangTag) {
75 this(new MsgHeader(MsgHeader.VERSION, pFunctionID, pLangTag, false, pFunctionID == SRV_REG, false, 0));
76 }
77
78
79
80
81
82
83 public SLPMessage(MsgHeader pHeader) {
84 this.iHeader = pHeader;
85 }
86
87
88
89
90
91
92 public MsgHeader getHeader() {
93 return this.iHeader;
94 }
95
96
97
98
99
100
101 public int getVersion() {
102 return this.iHeader.getVersion();
103 }
104
105
106
107
108
109
110 public int getFunctionID() {
111 return this.iHeader.getFunctionID();
112 }
113
114
115
116
117
118
119 public String getLangTag() {
120 return this.iHeader.getLangTag();
121 }
122
123
124
125
126
127
128 public boolean overflows() {
129 return this.iHeader.overflows();
130 }
131
132
133
134
135
136
137 public boolean fresh() {
138 return this.iHeader.fresh();
139 }
140
141
142
143
144
145
146 public boolean multicast() {
147 return this.iHeader.multicast();
148 }
149
150
151
152
153
154
155 public int getXID() {
156 return this.iHeader.getXID();
157 }
158
159
160
161
162
163 public void setXID(int pXID) {
164 this.iHeader.setXID(pXID);
165 }
166
167
168
169
170
171
172
173
174
175
176 public byte[] serialize(boolean pSetMulticastFlag, boolean pDatagramLimited, boolean pKeepXID)
177 throws ServiceLocationException {
178 return serialize(pSetMulticastFlag, pDatagramLimited, pKeepXID, null);
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193 public byte[] serialize(
194 boolean pSetMulticastFlag,
195 boolean pDatagramLimited,
196 boolean pKeepXID,
197 SerializeOption pOption
198 )
199 throws ServiceLocationException {
200 SLPOutputStream bodyOutStr = new SLPOutputStream(
201 pDatagramLimited ? this.iMaxDatagramSize - this.iHeader.getSize() : -1
202 );
203 boolean fit = serializeBody(bodyOutStr, pOption);
204 byte[] bodyBytes = bodyOutStr.toByteArray();
205 byte[] headerBytes = this.iHeader.serialize(bodyBytes.length, !fit, pSetMulticastFlag, pKeepXID);
206 byte[] bytes = new byte[headerBytes.length + bodyBytes.length];
207 System.arraycopy(headerBytes, 0, bytes, 0, headerBytes.length);
208 System.arraycopy(bodyBytes, 0, bytes, headerBytes.length, bodyBytes.length);
209 return bytes;
210 }
211
212
213
214
215
216 protected abstract boolean serializeBody(SLPOutputStream pOutStr, SerializeOption pOption)
217 throws ServiceLocationException;
218
219 @Override
220 public String toString() {
221 return super.toString() + " " + getXID();
222 }
223 }