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