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