public ITransport CreateTransport(Uri location)
        {
            URISupport.CompositeData cd = URISupport.parseComposite(location);

            if (cd.Components.Length > 0)
            {
                agent.DiscoveryURI = cd.Components[0];
            }

            if (!agent.IsStarted)
            {
                agent.Start();
            }

            Uri hostUri = DiscoveredUri;

            if (null == hostUri)
            {
                // If a new broker is found the agent will fire an event which will result in discoveredUri being set.
                discoveredUriEvent.WaitOne(TIMEOUT_IN_SECONDS * 1000, true);
                hostUri = DiscoveredUri;
                if (null == hostUri)
                {
                    throw new NMSConnectionException(String.Format("Unable to find a connection to {0} before the timeout period expired.", location.ToString()));
                }
            }

            TcpTransportFactory tcpTransFactory = new TcpTransportFactory();

            return(tcpTransFactory.CompositeConnect(new Uri(hostUri + location.Query)));
        }
Example #2
0
        /// <summary>
        /// Create a transport factory for the scheme.  If we do not support the transport protocol,
        /// an NMSConnectionException will be thrown.
        /// </summary>
        /// <param name="location"></param>
        /// <returns></returns>
        private static ITransportFactory CreateTransportFactory(Uri location)
        {
            string scheme = location.Scheme;

            if (null == scheme || 0 == scheme.Length)
            {
                throw new NMSConnectionException(String.Format("Transport scheme invalid: [{0}]", location.ToString()));
            }

            ITransportFactory factory = null;

            try
            {
                switch (scheme.ToLower())
                {
                case "tcp":
                    factory = new TcpTransportFactory();
                    break;

                case "ssl":
                    factory = new SslTransportFactory();
                    break;

                case "discovery":
                    factory = new DiscoveryTransportFactory();
                    break;

                case "failover":
                    factory = new FailoverTransportFactory();
                    break;

                case "mock":
                    factory = new MockTransportFactory();
                    break;

                default:
                    throw new NMSConnectionException(String.Format("The transport {0} is not supported.", scheme));
                }
            }
            catch (NMSConnectionException)
            {
                throw;
            }
            catch
            {
                throw new NMSConnectionException("Error creating transport.");
            }

            if (null == factory)
            {
                throw new NMSConnectionException("Unable to create a transport.");
            }

            return(factory);
        }
Example #3
0
        protected override ITransport InitializeTransport(IEasyGelfLogger logger)
        {
            var configuration = new TcpTransportConfiguration
            {
                RemoteAddress = RemoteAddress,
                RemotePort    = RemotePort,
                Ssl           = Ssl,
                Timeout       = Timeout
            };

            return(TcpTransportFactory.Produce(configuration));
        }
        public IConnection CreateConnection(string userName, string password)
        {
            ConnectionInfo info = CreateConnectionInfo(userName, password);

            TcpTransportFactory tcpTransportFactory = new TcpTransportFactory();
            ITransport          transport           = tcpTransportFactory.CreateTransport(brokerUri);

            Connection connection = new Connection(transport, info);

            connection.ClientId = info.ClientId;

            // Set properties on connection using parameters prefixed with "jms."
            System.Collections.Specialized.StringDictionary map = URISupport.ParseQuery(brokerUri.Query);
            URISupport.SetProperties(connection, map, "jms.");

            return(connection);
        }
Example #5
0
        /// <summary>
        ///     Create a transport factory for the scheme.
        ///     If we do not support the transport protocol, an StompConnectionException will be thrown.
        /// </summary>
        /// <param name="location">An URI.</param>
        /// <returns>Returns a <see cref="ITransportFactory" />.</returns>
        private ITransportFactory CreateTransportFactory(Uri location)
        {
            if (location.Scheme.IsEmpty())
            {
                throw new StompConnectionException($"Transport scheme invalid: [{location}]");
            }

            ITransportFactory factory;

            try
            {
                switch (location.Scheme.ToLower())
                {
                case "tcp":
                    factory = new TcpTransportFactory(_stompConnectionSettings);
                    break;

                case "ssl":
                    factory = new SslTransportFactory(_stompConnectionSettings);
                    break;

                default:
                    throw new StompConnectionException($"The transport {location.Scheme} is not supported.");
                }
            }
            catch (StompConnectionException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new StompConnectionException("Error creating transport.", ex);
            }

            if (null == factory)
            {
                throw new StompConnectionException("Unable to create a transport.");
            }

            return(factory);
        }
        public IConnection CreateConnection(string userName, string password)
        {
            Uri uri = brokerUri;

            // Do we need to strip off the activemq prefix??
            if ("activemq".Equals(brokerUri.Scheme))
            {
                uri = new Uri(brokerUri.AbsolutePath + brokerUri.Query);
            }

            ConnectionInfo    info = CreateConnectionInfo(userName, password);
            ITransportFactory tcpTransportFactory = new TcpTransportFactory();
            ITransport        transport           = tcpTransportFactory.CreateTransport(uri);
            Connection        connection          = new Connection(uri, transport, info);

            // Set properties on connection using parameters prefixed with "connection."
            System.Collections.Specialized.StringDictionary map = URISupport.ParseQuery(brokerUri.Query);
            URISupport.SetProperties(connection, map, "connection.");

            return(connection);
        }
Example #7
0
        private bool TryToConnectSocket()
        {
            bool hasSucceeded = false;

            try
            {
                multicastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                endPoint        = new IPEndPoint(IPAddress.Any, discoveryUri.Port);

                //We have to allow reuse in the multicast socket. Otherwise, we would be unable to use multiple clients on the same machine.
                multicastSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                multicastSocket.Bind(endPoint);

                IPAddress ipaddress;

                if (!TcpTransportFactory.TryParseIPAddress(discoveryUri.Host, out ipaddress))
                {
                    ipaddress = TcpTransportFactory.GetIPAddress(discoveryUri.Host, AddressFamily.InterNetwork);
                    if (null == ipaddress)
                    {
                        throw new NMSConnectionException("Invalid host address.");
                    }
                }

                multicastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                                new MulticastOption(ipaddress, IPAddress.Any));
#if !NETCF
                multicastSocket.ReceiveTimeout = SOCKET_TIMEOUT_MILLISECONDS;
#endif
                hasSucceeded = true;
            }
            catch (SocketException)
            {
            }

            return(hasSucceeded);
        }
Example #8
0
        private bool TryToConnectSocket(string targetHost, int targetPort)
        {
            bool hasSucceeded = false;

            try
            {
                multicastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                endPoint        = new IPEndPoint(IPAddress.Any, targetPort);

                // We have to allow reuse in the multicast socket. Otherwise, we would be unable to
                // use multiple clients on the same machine.
                multicastSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                multicastSocket.Bind(endPoint);

                IPAddress ipaddress;

                if (!TcpTransportFactory.TryParseIPAddress(targetHost, out ipaddress))
                {
                    ipaddress = TcpTransportFactory.GetIPAddress(targetHost, AddressFamily.InterNetwork);
                    if (null == ipaddress)
                    {
                        throw new NMSConnectionException("Invalid host address.");
                    }
                }

                if (LoopBackMode)
                {
                    multicastSocket.MulticastLoopback = true;
                }
                if (TimeToLive != 0)
                {
                    multicastSocket.SetSocketOption(SocketOptionLevel.IP,
                                                    SocketOptionName.MulticastTimeToLive, timeToLive);
                }
                if (!String.IsNullOrEmpty(mcJoinNetworkInterface))
                {
                    // TODO figure out how to set this.
                    throw new NotSupportedException("McJoinNetworkInterface not yet implemented.");
                }
                else
                {
                    multicastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                                    new MulticastOption(ipaddress, IPAddress.Any));
                }

                if (!String.IsNullOrEmpty(mcNetworkInterface))
                {
                    // TODO figure out how to set this.
                    throw new NotSupportedException("McNetworkInterface not yet implemented.");
                }

                multicastSocket.ReceiveTimeout = (int)keepAliveInterval;

                hasSucceeded = true;
            }
            catch (SocketException)
            {
            }

            return(hasSucceeded);
        }