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