/// <summary> /// Parse the given <b>IXmlElement</b> as a local <b>IPEndPoint</b>. /// </summary> /// <remarks> /// If the specified <b>IXmlElement</b> contains an empty address, /// <c>null</c> is returned. /// </remarks> /// <param name="xml"> /// The <b>IXmlElement</b> to parse. /// </param> /// <returns> /// A new <b>IPEndPoint</b> representing the contents of the given /// <b>XmlNode</b>. /// </returns> protected static IPEndPoint ParseLocalSocketAddress(IXmlElement xml) { IXmlElement xmlAddr = xml.GetElement("address"); IXmlElement xmlPort = xml.GetElement("port"); String sAddressFamiliy = xml.GetSafeElement("address-family").GetString( "InterNetwork"); if (xmlAddr == null && xmlPort == null) { return(null); } string addr = xmlAddr == null ? "localhost" : xmlAddr.GetString(); int port = xmlPort == null ? 0 : xmlPort.GetInt(); NetworkUtils.PreferredAddressFamily = (AddressFamily) Enum.Parse(typeof(AddressFamily), sAddressFamiliy); IPAddress ipAddress; try { ipAddress = addr.Equals("localhost") ? NetworkUtils.GetLocalHostAddress() : NetworkUtils.GetHostAddress(addr); } catch (Exception e) { throw new Exception("The \"" + xml.Name + "\" configuration " + "element contains an invalid \"address\" element", e); } try { return(new IPEndPoint(ipAddress, port)); } catch (Exception e) { throw new Exception("The \"" + xml.Name + "\" configuration " + "element contains an invalid \"port\" element", e); } }
/// <summary> /// Return a child <b>IXmlElement</b> of the given <b>IXmlElement</b> /// that can be used to create and configure a new /// <b>IConnectionInitiator</b>. /// </summary> /// <remarks> /// The given <b>IXmlElement</b> must have a child element with one /// of the following names: /// <list type="number"> /// <item> /// tcp-initiator: used to create and configure a new TcpInitiator /// </item> /// </list> /// </remarks> /// <param name="xml"> /// The parent <b>IXmlElement</b> of the <b>IXmlElement</b> used to /// create and configure a new <b>IConnectionInitiator</b>. /// </param> /// <returns> /// A child <b>IXmlElement</b> that can be used to create and /// configure a new <b>IConnectionInitiator</b> or <c>null</c> if no /// such <b>IXmlElement</b> exists. /// </returns> /// <exception cref="ArgumentException"> /// If the given <b>IXmlElement</b> does not have a valid /// <b>IConnectionInitiator</b> configuration child element. /// </exception> public static IXmlElement FindInitiatorConfig(IXmlElement xml) { IXmlElement xmlConfig = xml.Name.Equals("initiator-config") ? xml : xml.GetSafeElement("initiator-config"); if (xmlConfig.GetElement("tcp-initiator") != null) { return((IXmlElement)xmlConfig.Clone()); } throw new ArgumentException("the \"initiator-config\" element is either" + " missing, empty, or does not contain a valid transport-specific" + " child element:\n" + xml); }
/// <summary> /// Create the configured <b>IStreamProvider</b> used to provide /// a network stream. /// </summary> /// <param name="xml">An <b>IXmlElement</b> containing configuraiton for the StreamProviderFactory.</param> /// <returns>An <b>IStreamProvider</b>.</returns> public static IStreamProvider CreateProvider(IXmlElement xml) { IXmlElement xmlElement = xml.GetElement(SSL_NAME); if (xmlElement != null) { IStreamProvider streamProvider = new SslStreamProvider { Config = xmlElement }; return(streamProvider); } return(new SystemStreamProvider()); }
/// <summary> /// Factory method: create and configure a new /// <see cref="IConnectionInitiator"/> for the given configuration. /// </summary> /// <param name="xml"> /// The <b>IXmlElement</b> used to create and configure a new /// <b>IConnectionInitiator</b>. /// </param> /// <param name="ctx"> /// The <b>IOperationalContext</b> used to configure a new <b>Peer</b>. /// </param> /// <returns> /// A new <b>IConnectionInitiator</b>. /// </returns> /// <exception cref="ArgumentException"> /// If the given <b>IXmlElement</b> is not a valid /// <b>IConnectionInitiator</b> configuration element. /// </exception> /// <seealso cref="FindInitiatorConfig"/> public static IConnectionInitiator CreateInitiator(IXmlElement xml, IOperationalContext ctx) { IConnectionInitiator initiator; if (xml.GetElement("tcp-initiator") != null) { initiator = (IConnectionInitiator)Activator.CreateInstance(typeof(TcpInitiator)); } else { throw new ArgumentException("unsupported \"initiator-config\":\n" + xml); } initiator.OperationalContext = ctx; initiator.Configure(xml); return(initiator); }
/// <summary> /// Instantiate an <see cref="IAddressProvider"/> configured according /// to the specified XML. The passed XML has to conform to the /// following format: /// <pre> /// <!ELEMENT ... (socket-address+ | address-provider)> /// <!ELEMENT address-provider /// (class-name | (class-factory-name, method-name), init-params?> /// <!ELEMENT socket-address (address, port)> /// </pre> /// </summary> /// <returns> /// An instance of the corresponding <b>IAddressProvider</b> /// implementation. /// </returns> public IAddressProvider CreateAddressProvider() { IXmlElement config = Config; string elementName = config.Name; if (elementName.Equals("address-provider")) { IXmlElement xmlSocketAddress = config.GetElement("socket-address"); if (xmlSocketAddress != null) { return(new ConfigurableAddressProvider(config)); } return((IAddressProvider)XmlHelper.CreateInstance(config, null, typeof(IAddressProvider))); } return(new ConfigurableAddressProvider(config)); }
public void TestWithAddressProvider() { var initiator = new TcpInitiator { OperationalContext = new DefaultOperationalContext() }; Stream stream = GetType().Assembly.GetManifestResourceStream("Tangosol.Resources.s4hc-cache-config.xml"); IXmlDocument xmlConfig = XmlHelper.LoadXml(stream); IXmlElement remoteAddressNode = xmlConfig.FindElement( "caching-schemes/remote-cache-scheme/initiator-config/tcp-initiator/remote-addresses"); if (null != remoteAddressNode) { IXmlElement socketAddressNode = remoteAddressNode.GetElement("socket-address"); if (null != socketAddressNode) { XmlHelper.RemoveElement(remoteAddressNode, "socket-address"); } remoteAddressNode.AddElement("address-provider").AddElement("class-name").SetString( "Tangosol.Net.ConfigurableAddressProviderTests+LoopbackAddressProvider, Coherence.Tests"); } IXmlElement initConfig = xmlConfig.FindElement("caching-schemes/remote-cache-scheme/initiator-config"); initiator.Configure(initConfig); initiator.RegisterProtocol(CacheServiceProtocol.Instance); initiator.RegisterProtocol(NamedCacheProtocol.Instance); initiator.Start(); Assert.AreEqual(initiator.IsRunning, true); IConnection conn = initiator.EnsureConnection(); Assert.IsNotNull(conn); Assert.AreEqual(conn.IsOpen, true); conn.Close(); initiator.Stop(); }
/// <summary> /// Configure the controllable service. /// </summary> /// <remarks> /// <p/> /// This method can only be called before the controllable service /// is started. /// </remarks> /// <param name="xml"> /// An <see cref="IXmlElement"/> carrying configuration information /// specific to the IControllable object. /// </param> /// <exception cref="InvalidOperationException"> /// Thrown if the service is already running. /// </exception> /// <exception cref="ArgumentException"> /// Thrown if the configuration information is invalid. /// </exception> public override void Configure(IXmlElement xml) { lock (this) { base.Configure(xml); if (xml == null) { return; } // <tcp-initiator> IXmlElement xmlCat = xml.GetSafeElement("tcp-initiator"); // <stream-provider/> IXmlElement xmlSub = xmlCat.GetSafeElement("stream-provider"); StreamProvider = StreamProviderFactory.CreateProvider(xmlSub); // <local-address> xmlSub = xmlCat.GetSafeElement("local-address"); // <address> // <port> LocalAddress = ParseLocalSocketAddress(xmlSub); // <reusable> IsLocalAddressReusable = xmlSub.GetSafeElement("reusable"). GetBoolean(IsLocalAddressReusable); // <remote-addresses> bool isNameService = false; xmlSub = xmlCat.GetElement("name-service-addresses"); if (xmlSub == null) { xmlSub = xmlCat.GetSafeElement("remote-addresses"); } else { isNameService = true; } IAddressProviderFactory factory; IXmlElement xmlProvider = xmlSub.GetElement("address-provider"); bool missing = xmlProvider == null; bool empty = !missing && xmlProvider.IsEmpty; if (empty || missing) { ConfigurableAddressProviderFactory factoryImpl = new ConfigurableAddressProviderFactory(); factoryImpl.Config = missing ? xmlSub : xmlProvider; factory = factoryImpl; } else { String name = xmlProvider.GetString(); factory = (IAddressProviderFactory) OperationalContext.AddressProviderMap[name]; if (factory == null) { throw new ArgumentException("Address provider " + name + " not found."); } } RemoteAddressProvider = factory.CreateAddressProvider(); if (RemoteAddressProvider is ConfigurableAddressProvider && ConnectTimeout > 0) { ((ConfigurableAddressProvider)RemoteAddressProvider).RequestTimeout = ConnectTimeout; } IsNameService = isNameService; if (isNameService) { Subport = (int)WellKnownSubPorts.NameService; } else { Subport = -1; } // <reuse-address> IsLocalAddressReusable = xmlCat.GetSafeElement("reuse-address"). GetBoolean(IsLocalAddressReusable); // <keep-alive-enabled/> IsKeepAliveEnabled = xmlCat.GetSafeElement("keep-alive-enabled") . GetBoolean(IsKeepAliveEnabled); // <tcp-delay-enabled> IsTcpDelayEnabled = xmlCat.GetSafeElement("tcp-delay-enabled"). GetBoolean(IsTcpDelayEnabled); // <receive-buffer-size> ReceiveBufferSize = ParseMemorySize( xmlCat, "receive-buffer-size", ReceiveBufferSize); // <send-buffer-size> SendBufferSize = ParseMemorySize( xmlCat, "send-buffer-size", SendBufferSize); // <linger-timeout> LingerTimeout = ParseTime( xmlCat, "linger-timeout", LingerTimeout); } }
/// <summary> /// Parse test serializer config and set the members. /// </summary> private void ParseXml(IXmlElement xml) { m_pofcfg = xml.GetElement("pof-config").GetString(); m_testName = xml.GetElement("SerializerName").GetString(); }