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   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
20   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2)
21   * 2763216    2009-04-14  blaschke-oss Code cleanup: visible spelling/grammar errors
22   */
23  
24  package org.metricshub.wbem.sblim.slp.internal.msg;
25  
26  /*-
27   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
28   * WBEM Java Client
29   * ჻჻჻჻჻჻
30   * Copyright 2023 - 2025 MetricsHub
31   * ჻჻჻჻჻჻
32   * Licensed under the Apache License, Version 2.0 (the "License");
33   * you may not use this file except in compliance with the License.
34   * You may obtain a copy of the License at
35   *
36   *      http://www.apache.org/licenses/LICENSE-2.0
37   *
38   * Unless required by applicable law or agreed to in writing, software
39   * distributed under the License is distributed on an "AS IS" BASIS,
40   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41   * See the License for the specific language governing permissions and
42   * limitations under the License.
43   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
44   */
45  
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.SortedSet;
49  import java.util.TreeSet;
50  import org.metricshub.wbem.sblim.slp.ServiceLocationException;
51  import org.metricshub.wbem.sblim.slp.internal.msg.SLPMessage;
52  
53  /**
54   * RequestMessage
55   *
56   */
57  public abstract class RequestMessage extends SLPMessage {
58  	private SortedSet<String> iPrevResponderSet;
59  
60  	private List<String> iScopeList;
61  
62  	/**
63  	 * Ctor.
64  	 *
65  	 * @param pFunctionID
66  	 * @param pPrevResponderSet
67  	 *            - set of address strings
68  	 * @param pScopeList
69  	 *            - list of scope strings
70  	 */
71  	public RequestMessage(int pFunctionID, SortedSet<String> pPrevResponderSet, List<String> pScopeList) {
72  		super(pFunctionID);
73  		init(pPrevResponderSet, pScopeList);
74  	}
75  
76  	/**
77  	 * Ctor.
78  	 *
79  	 * @param pFunctionID
80  	 * @param pLangTag
81  	 * @param pPrevResponderSet
82  	 *            - set of address strings
83  	 * @param pScopeList
84  	 *            - list of scope strings
85  	 */
86  	public RequestMessage(
87  		int pFunctionID,
88  		String pLangTag,
89  		SortedSet<String> pPrevResponderSet,
90  		List<String> pScopeList
91  	) {
92  		super(pFunctionID, pLangTag);
93  		init(pPrevResponderSet, pScopeList);
94  	}
95  
96  	/**
97  	 * Ctor.
98  	 *
99  	 * @param pHeader
100 	 * @param pPrevResponderSet
101 	 *            - set of address strings
102 	 * @param pScopeList
103 	 *            - list of scope strings
104 	 */
105 	public RequestMessage(MsgHeader pHeader, SortedSet<String> pPrevResponderSet, List<String> pScopeList) {
106 		super(pHeader);
107 		init(pPrevResponderSet, pScopeList);
108 	}
109 
110 	/**
111 	 * getPrevResponderSet
112 	 *
113 	 * @return SortedSet
114 	 */
115 	public SortedSet<String> getPrevResponderSet() {
116 		return this.iPrevResponderSet;
117 	}
118 
119 	/**
120 	 * getPrevRespondersItr
121 	 *
122 	 * @return Iterator
123 	 */
124 	public Iterator<String> getPrevRespondersItr() {
125 		return this.iPrevResponderSet == null ? null : this.iPrevResponderSet.iterator();
126 	}
127 
128 	/**
129 	 * updatePrevResponders
130 	 *
131 	 * @param pResponder
132 	 * @return boolean
133 	 */
134 	public boolean updatePrevResponders(String pResponder) {
135 		if (this.iPrevResponderSet == null) this.iPrevResponderSet = new TreeSet<String>();
136 		return this.iPrevResponderSet.add(pResponder);
137 	}
138 
139 	/**
140 	 * getScopeList
141 	 *
142 	 * @return List of scope strings
143 	 */
144 	public List<String> getScopeList() {
145 		return this.iScopeList;
146 	}
147 
148 	/**
149 	 * isAllowedResponseType
150 	 *
151 	 * @param pRspMsg
152 	 * @return boolean
153 	 */
154 	public boolean isAllowedResponseType(SLPMessage pRspMsg) {
155 		if (pRspMsg == null) return false;
156 		int id = pRspMsg.getFunctionID();
157 		int[] rspIDs = getAllowedResponseIDs();
158 		if (rspIDs == null) return true;
159 		for (int i = 0; i < rspIDs.length; i++) if (id == rspIDs[i]) return true;
160 		return false;
161 	}
162 
163 	/**
164 	 * serializeWithoutResponders
165 	 *
166 	 * @param pSetMulticastFlag
167 	 * @param pDatagramLimited
168 	 * @param pKeepXID
169 	 * @return byte[]
170 	 * @throws ServiceLocationException
171 	 */
172 	public byte[] serializeWithoutResponders(boolean pSetMulticastFlag, boolean pDatagramLimited, boolean pKeepXID)
173 		throws ServiceLocationException {
174 		return serialize(pSetMulticastFlag, pDatagramLimited, pKeepXID, new SkipResponders());
175 	}
176 
177 	@Override
178 	protected boolean serializeBody(SLPOutputStream pOutStr, SerializeOption pSkipResponders)
179 		throws ServiceLocationException {
180 		if (
181 			!pOutStr.writeStringList(pSkipResponders == null ? getPrevRespondersItr() : null)
182 		) throw new ServiceLocationException(
183 			ServiceLocationException.PREVIOUS_RESPONDER_OVERFLOW,
184 			"Previous responder list has overflowed!"
185 		);
186 		return serializeRequestBody(pOutStr);
187 	}
188 
189 	protected abstract boolean serializeRequestBody(SLPOutputStream pOutStr) throws ServiceLocationException;
190 
191 	protected abstract int[] getAllowedResponseIDs();
192 
193 	private void init(SortedSet<String> pPrevResponderSet, List<String> pScopeList) {
194 		this.iPrevResponderSet = pPrevResponderSet;
195 		this.iScopeList = pScopeList;
196 	}
197 
198 	class SkipResponders extends SerializeOption {
199 		/*
200 		 * non-null instance indicates that the PreviousResponderList
201 		 * serialization have to be skipped
202 		 */
203 	}
204 }