1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.metricshub.wbem.sblim.slp.internal.msg;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.net.DatagramPacket;
46 import java.net.Socket;
47 import org.metricshub.wbem.sblim.slp.ServiceLocationException;
48 import org.metricshub.wbem.sblim.slp.internal.msg.FunctionIDs;
49
50
51
52
53
54 public class MsgFactory implements FunctionIDs {
55
56
57
58
59
60 private interface FactoryEntry {
61
62
63
64
65
66
67
68
69
70 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException;
71 }
72
73 private static FactoryEntry[] cFactoryArray;
74
75 private static void placeFactory(int pFnID, FactoryEntry pEntry) {
76 cFactoryArray[pFnID - FIRST_ID] = pEntry;
77 }
78
79 private static synchronized void createFactoryArray() {
80 if (cFactoryArray != null) return;
81 cFactoryArray = new FactoryEntry[LAST_ID - FIRST_ID + 1];
82 for (int i = 0; i < cFactoryArray.length; i++) cFactoryArray[i] = null;
83 placeFactory(
84 ATTR_RPLY,
85 new FactoryEntry() {
86
87 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
88 return AttributeReply.parse(pHdr, pInStr);
89 }
90 }
91 );
92 placeFactory(
93 ATTR_RQST,
94 new FactoryEntry() {
95
96 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
97 return AttributeRequest.parse(pHdr, pInStr);
98 }
99 }
100 );
101 placeFactory(
102 DA_ADVERT,
103 new FactoryEntry() {
104
105 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
106 return DAAdvert.parse(pHdr, pInStr);
107 }
108 }
109 );
110 placeFactory(
111 SA_ADVERT,
112 new FactoryEntry() {
113
114 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
115 return SAAdvert.parse(pHdr, pInStr);
116 }
117 }
118 );
119 placeFactory(
120 SRV_ACK,
121 new FactoryEntry() {
122
123 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
124 return ServiceAcknowledgment.parse(pHdr, pInStr);
125 }
126 }
127 );
128 placeFactory(
129 SRV_DEREG,
130 new FactoryEntry() {
131
132 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
133 return ServiceDeregistration.parse(pHdr, pInStr);
134 }
135 }
136 );
137 placeFactory(
138 SRV_REG,
139 new FactoryEntry() {
140
141 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
142 return ServiceRegistration.parse(pHdr, pInStr);
143 }
144 }
145 );
146 placeFactory(
147 SRV_RPLY,
148 new FactoryEntry() {
149
150 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
151 return ServiceReply.parse(pHdr, pInStr);
152 }
153 }
154 );
155 placeFactory(
156 SRV_RQST,
157 new FactoryEntry() {
158
159 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
160 return ServiceRequest.parse(pHdr, pInStr);
161 }
162 }
163 );
164
165 placeFactory(
166 SRV_TYPE_RPLY,
167 new FactoryEntry() {
168
169 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
170 return ServiceTypeReply.parse(pHdr, pInStr);
171 }
172 }
173 );
174 placeFactory(
175 SRV_TYPE_RQST,
176 new FactoryEntry() {
177
178 public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
179 return ServiceTypeRequest.parse(pHdr, pInStr);
180 }
181 }
182 );
183 }
184
185 private static FactoryEntry getFactory(int pFnID) {
186 createFactoryArray();
187 return cFactoryArray[pFnID - FIRST_ID];
188 }
189
190
191
192
193
194
195
196
197
198 public static SLPMessage parse(Socket pSock) throws ServiceLocationException, IOException {
199 return parse(pSock.getInputStream());
200 }
201
202
203
204
205
206
207
208
209
210 public static SLPMessage parse(InputStream pInStr) throws ServiceLocationException, IOException {
211 return parse(new SLPInputStream(pInStr));
212 }
213
214
215
216
217
218
219
220
221
222 public static SLPMessage parse(DatagramPacket pPacket) throws ServiceLocationException, IOException {
223 return parse(new SLPInputStream(pPacket));
224 }
225
226
227
228
229
230
231
232
233
234 public static SLPMessage parse(SLPInputStream pInStr) throws ServiceLocationException, IOException {
235 MsgHeader hdr = MsgHeader.parse(pInStr);
236 FactoryEntry factory = getFactory(hdr.getFunctionID());
237 if (factory == null) throw new ServiceLocationException(
238 ServiceLocationException.NOT_IMPLEMENTED,
239 "FunctionID=" + hdr.getFunctionID() + " is not implemented!"
240 );
241 return factory.parse(hdr, pInStr);
242 }
243 }