00001 /* 00002 * ConfigurableAddressProvider.hpp 00003 * 00004 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 00005 * 00006 * Oracle is a registered trademarks of Oracle Corporation and/or its 00007 * affiliates. 00008 * 00009 * This software is the confidential and proprietary information of Oracle 00010 * Corporation. You shall not disclose such confidential and proprietary 00011 * information and shall use it only in accordance with the terms of the 00012 * license agreement you entered into with Oracle. 00013 * 00014 * This notice may not be removed or altered. 00015 */ 00016 #ifndef COH_CONFIGURABLE_ADDRESS_PROVIDER_HPP 00017 #define COH_CONFIGURABLE_ADDRESS_PROVIDER_HPP 00018 00019 #include "coherence/lang.ns" 00020 00021 #include "coherence/net/AddressProvider.hpp" 00022 00023 #include "coherence/run/xml/XmlElement.hpp" 00024 00025 #include "coherence/util/Iterator.hpp" 00026 #include "coherence/util/List.hpp" 00027 00028 COH_OPEN_NAMESPACE2(coherence,net) 00029 00030 using coherence::run::xml::XmlElement; 00031 using coherence::util::Iterator; 00032 using coherence::util::List; 00033 00034 /** 00035 * ConfigurableAddressProvider is an implementation of the AddressProvider 00036 * interface based on a static list of addresses configured in an XML element 00037 * that contains one or more items in the following format: 00038 * <pre> 00039 * <socket-address> 00040 * <address>...</address> 00041 * <port>...</port> 00042 * </socket-address> 00043 * </pre> 00044 * The order of items in the configured list will be randomized to provide 00045 * basic load balancing. 00046 * This implementation is not thread safe. 00047 * 00048 * @author gg 2008-08-18 00049 * @author gm 2008-08-25 00050 * @since Coherence 3.4 00051 */ 00052 class COH_EXPORT ConfigurableAddressProvider 00053 : public class_spec<ConfigurableAddressProvider, 00054 extends<Object>, 00055 implements<AddressProvider> > 00056 { 00057 friend class factory<ConfigurableAddressProvider>; 00058 00059 // ----- constructors --------------------------------------------------- 00060 00061 protected: 00062 /** 00063 * Create a new ConfigurableAddressProvider instance. 00064 */ 00065 ConfigurableAddressProvider(); 00066 00067 /** 00068 * Construct an instance of ConfigurableAddressProvider based on the 00069 * specified XML element. 00070 * 00071 * @param vXml the XML element that contains the configuration info 00072 * @param fSafe true if the provider is skips unresolved addresses 00073 */ 00074 ConfigurableAddressProvider(XmlElement::View vXml, bool fSafe = true); 00075 00076 00077 // ----- AddressProvider interface -------------------------------------- 00078 00079 public: 00080 /** 00081 * {@inheritDoc} 00082 */ 00083 virtual InetSocketAddress::View getNextAddress(); 00084 00085 /** 00086 * {@inheritDoc} 00087 */ 00088 virtual void accept(); 00089 00090 /** 00091 * {@inheritDoc} 00092 */ 00093 virtual void reject(Exception::Holder oheCause); 00094 00095 00096 // ----- helpers -------------------------------------------------------- 00097 00098 protected: 00099 /** 00100 * Configure this ConfigurableAddressProvider based on the specified 00101 * XML. 00102 * 00103 * @param xmlConfig the XML element that contains the configuration 00104 * info 00105 */ 00106 virtual void configure(XmlElement::View vXml); 00107 00108 /** 00109 * Make all addresses iterable, starting at the first address. 00110 */ 00111 virtual void reset(); 00112 00113 /** 00114 * Make all addresses iterable, starting at the index after the specified 00115 * one. 00116 * 00117 * @param iLast the index of the last address returned 00118 */ 00119 virtual void reset(size32_t iLast); 00120 00121 /** 00122 * Resolve an address and port. 00123 * 00124 * @param vsHost the host 00125 * @param nPort the port 00126 * 00127 * @return an iterator over the resolved InetSocketAddresses 00128 */ 00129 virtual Iterator::Handle resolveAddress(String::View vsHost, int nPort) const; 00130 00131 /** 00132 * Sort the holders in the order to be returned by the getNextAddress 00133 * method. This implementation randomizes the holder lists for 00134 * simple load balancing. 00135 * 00136 * @param list the original list retrieved from the configuration 00137 * 00138 * @return the re-ordered list 00139 */ 00140 virtual List::Handle sortHolders(List::Handle hList); 00141 00142 00143 // ----- Object methods ------------------------------------------------- 00144 00145 public: 00146 /** 00147 * {@inheritDoc} 00148 */ 00149 virtual TypedHandle<const String> toString() const; 00150 00151 00152 // ----- inner class: AddressHolder ------------------------------------- 00153 00154 protected: 00155 /** 00156 * A stateful holder for an InetSocketAddress object. 00157 */ 00158 class COH_EXPORT AddressHolder 00159 : public class_spec<AddressHolder> 00160 { 00161 friend class factory<AddressHolder>; 00162 friend class ConfigurableAddressProvider; 00163 00164 // ----- constructors --------------------------------------- 00165 00166 protected: 00167 /** 00168 * Construct an AddressHolder for the specified 00169 * InetSocketAddress. 00170 * 00171 * @param sHost the hostname 00172 * @param nPort the port number 00173 */ 00174 AddressHolder(String::View vsHost, int32_t nPort); 00175 00176 /** 00177 * Create a new AddressHolder instance. 00178 */ 00179 AddressHolder(); 00180 00181 public: 00182 /** 00183 * Throw IllegalArgumentException if any values are invalid. 00184 * 00185 * @return this 00186 */ 00187 AddressHolder::Handle validate(); 00188 00189 // ----- accessors ------------------------------------------ 00190 00191 protected: 00192 /** 00193 * Check whether or not the underlying address has been accepted. 00194 * 00195 * @return true iff the underlying address has not yet been 00196 * accepted 00197 */ 00198 virtual bool isPending() const; 00199 00200 /** 00201 * Set or clear the "pending" flag. 00202 * 00203 * @param fPending the flag value 00204 */ 00205 virtual void setPending(bool fPending); 00206 00207 /** 00208 * Check whether or not the underlying address has been reported 00209 * as unresolveable. 00210 * 00211 * @return true iff the underlying address has been reported as 00212 * unresolveable 00213 */ 00214 virtual bool isReported() const; 00215 00216 /** 00217 * Set of clear the "reported" flag. 00218 * 00219 * @param fReported the flag value 00220 */ 00221 virtual void setReported(bool fReported); 00222 00223 public: 00224 /** 00225 * Return the host name. 00226 * 00227 * @return the host name 00228 */ 00229 virtual String::View getHost() const; 00230 00231 /** 00232 * Return the port number. 00233 * 00234 * @return the port number 00235 */ 00236 virtual uint16_t getPort() const; 00237 00238 // ----- data members ------------------------------------------- 00239 00240 protected: 00241 /** 00242 * The configured address, either hostname or IP address. 00243 */ 00244 FinalView<String> f_vsHost; 00245 00246 /** 00247 * The configured port. 00248 */ 00249 const uint16_t m_nPort; 00250 00251 /** 00252 * A flag indicating that the underlying address has been 00253 * provided to a client, but has not yet been accepted. 00254 */ 00255 bool m_fPending; 00256 00257 /** 00258 * Specifies if this address has already been reported as 00259 * unresolved. 00260 */ 00261 bool m_fReported; 00262 }; 00263 00264 00265 // ----- data members --------------------------------------------------- 00266 00267 protected: 00268 /** 00269 * A list of AddressHolder objects. 00270 */ 00271 FinalView<List> f_vListHolders; 00272 00273 /** 00274 * An address iterator for the previously resolved address. 00275 * 00276 * @since 12.2.1 00277 */ 00278 MemberHandle<Iterator> m_hIterAddr; 00279 00280 /** 00281 * Index of the last returned address. 00282 */ 00283 size32_t m_iLast; 00284 00285 /** 00286 * Specifies if the provider is only to return resolved addresses. 00287 */ 00288 bool m_fSafe; 00289 00290 00291 // ----- constants ------------------------------------------------------ 00292 00293 public: 00294 /** 00295 * The largest possible value of type size32_t. 00296 */ 00297 static const size32_t npos = size32_t(-1); 00298 }; 00299 00300 COH_CLOSE_NAMESPACE2 00301 00302 #endif // COH_CONFIGURABLE_ADDRESS_PROVIDER_HPP