View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2007, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Endre Bak, IBM, ebak@de.ibm.com
12   * 
13   * Change History
14   * Flag       Date        Prog         Description
15   *------------------------------------------------------------------------------- 
16   * 1804402    2007-09-28  ebak         IPv6 ready SLP
17   * 1892103    2008-02-12  ebak         SLP improvements
18   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
19   * 2204488 	  2008-10-28  raman_arora  Fix code to remove compiler warnings
20   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
21   */
22  
23  package org.metricshub.wbem.sblim.slp.internal.msg;
24  
25  /*-
26   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
27   * WBEM Java Client
28   * ჻჻჻჻჻჻
29   * Copyright 2023 - 2025 MetricsHub
30   * ჻჻჻჻჻჻
31   * Licensed under the Apache License, Version 2.0 (the "License");
32   * you may not use this file except in compliance with the License.
33   * You may obtain a copy of the License at
34   *
35   *      http://www.apache.org/licenses/LICENSE-2.0
36   *
37   * Unless required by applicable law or agreed to in writing, software
38   * distributed under the License is distributed on an "AS IS" BASIS,
39   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40   * See the License for the specific language governing permissions and
41   * limitations under the License.
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   * SLPMessage
51   *
52   */
53  public abstract class SLPMessage implements FunctionIDs {
54  	// cache it for instance lifetime, do not parse always
55  	private final int iMaxDatagramSize = SLPConfig.getGlobalCfg().getMTU();
56  
57  	private MsgHeader iHeader;
58  
59  	/**
60  	 * Ctor.
61  	 *
62  	 * @param pFunctionID
63  	 */
64  	public SLPMessage(int pFunctionID) {
65  		this(pFunctionID, Util.getLangTag(SLPDefaults.LOCALE));
66  	}
67  
68  	/**
69  	 * Ctor.
70  	 *
71  	 * @param pFunctionID
72  	 * @param pLangTag
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  	 * Ctor.
80  	 *
81  	 * @param pHeader
82  	 */
83  	public SLPMessage(MsgHeader pHeader) {
84  		this.iHeader = pHeader;
85  	}
86  
87  	/**
88  	 * getHeader
89  	 *
90  	 * @return MsgHeader
91  	 */
92  	public MsgHeader getHeader() {
93  		return this.iHeader;
94  	}
95  
96  	/**
97  	 * getVersion
98  	 *
99  	 * @return int
100 	 */
101 	public int getVersion() {
102 		return this.iHeader.getVersion();
103 	}
104 
105 	/**
106 	 * getFunctionID
107 	 *
108 	 * @return int
109 	 */
110 	public int getFunctionID() {
111 		return this.iHeader.getFunctionID();
112 	}
113 
114 	/**
115 	 * getLangTag
116 	 *
117 	 * @return String
118 	 */
119 	public String getLangTag() {
120 		return this.iHeader.getLangTag();
121 	}
122 
123 	/**
124 	 * overflows
125 	 *
126 	 * @return boolean
127 	 */
128 	public boolean overflows() {
129 		return this.iHeader.overflows();
130 	}
131 
132 	/**
133 	 * fresh
134 	 *
135 	 * @return boolean
136 	 */
137 	public boolean fresh() {
138 		return this.iHeader.fresh();
139 	}
140 
141 	/**
142 	 * multicast
143 	 *
144 	 * @return boolean
145 	 */
146 	public boolean multicast() {
147 		return this.iHeader.multicast();
148 	}
149 
150 	/**
151 	 * getXID
152 	 *
153 	 * @return int
154 	 */
155 	public int getXID() {
156 		return this.iHeader.getXID();
157 	}
158 
159 	/**
160 	 * @see MsgHeader#setXID(int)
161 	 * @param pXID
162 	 */
163 	public void setXID(int pXID) {
164 		this.iHeader.setXID(pXID);
165 	}
166 
167 	/**
168 	 * serialize
169 	 *
170 	 * @param pSetMulticastFlag
171 	 * @param pDatagramLimited
172 	 * @param pKeepXID
173 	 * @return byte[]
174 	 * @throws ServiceLocationException
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 	 * serialize
183 	 *
184 	 * @param pSetMulticastFlag
185 	 * @param pDatagramLimited
186 	 * @param pKeepXID
187 	 * @param pOption
188 	 *            - passed to the serializeBody() method of the inherited
189 	 *            classes
190 	 * @return byte[]
191 	 * @throws ServiceLocationException
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 	 * @return true if all data is written into pOStr (all data fits into size
214 	 *         limit)
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 }