Ejemplo n.º 1
0
        public void DeliverMail()
        {
            Console.WriteLine("Preparing mail delivery plan...");
            var transport = _transportFactory.CreateTransport(_mailItem);

            var deliveryPlan = transport.Deliver();

            Console.WriteLine($"Mail sent via {deliveryPlan}");
        }
Ejemplo n.º 2
0
        public IConnection CreateConnection(string userName, string password)
        {
            Connection connection = null;

            try
            {
                // Strip off the activemq prefix, if it exists.
                Uri uri = new Uri(URISupport.stripPrefix(brokerUri.OriginalString, "stomp:"));

                Tracer.InfoFormat("Connecting to: {0}", uri.ToString());

                ITransport transport = TransportFactory.CreateTransport(uri);

                connection = new Connection(uri, transport, this.ClientIdGenerator);

                connection.UserName = userName;
                connection.Password = password;

                ConfigureConnection(connection);

                if (this.clientId != null)
                {
                    connection.DefaultClientId = this.clientId;
                }

                connection.ITransport.Start();

                return(connection);
            }
            catch (NMSException e)
            {
                try
                {
                    connection.Close();
                }
                catch
                {
                }

                throw e;
            }
            catch (Exception e)
            {
                try
                {
                    connection.Close();
                }
                catch
                {
                }

                throw NMSExceptionSupport.Create("Could not connect to broker URL: " + this.brokerUri + ". Reason: " + e.Message, e);
            }
        }
Ejemplo n.º 3
0
        public IConnection CreateConnection(string userName, string password)
        {
            Connection connection = null;

            try
            {
                Tracer.InfoFormat("Connecting to: {0}", brokerUri.ToString());

                ITransport transport = TransportFactory.CreateTransport(brokerUri);

                connection = new Connection(brokerUri, transport, this.ClientIdGenerator);

                connection.UserName = userName;
                connection.Password = password;

                ConfigureConnection(connection);

                if (this.clientId != null)
                {
                    // Set the connection factory version as the default, the user can
                    // still override this via a call to Connection.ClientId = XXX
                    connection.DefaultClientId = this.clientId;
                }

                connection.ITransport.Start();

                return(connection);
            }
            catch (NMSException e)
            {
                try
                {
                    connection.Close();
                }
                catch
                {
                }

                throw e;
            }
            catch (Exception e)
            {
                try
                {
                    connection.Close();
                }
                catch
                {
                }

                throw NMSExceptionSupport.Create("Could not connect to broker URL: " + this.brokerUri + ". Reason: " + e.Message, e);
            }
        }
Ejemplo n.º 4
0
        protected virtual Connection CreateActiveMQConnection(string userName, string password)
        {
            Connection connection = null;

            try
            {
                Tracer.InfoFormat("Connecting to: {0}", brokerUri.ToString());

                ITransport transport = TransportFactory.CreateTransport(brokerUri);

                connection = CreateActiveMQConnection(transport);

                ConfigureConnection(connection);

                connection.UserName = userName;
                connection.Password = password;

                if (this.clientId != null)
                {
                    connection.DefaultClientId = this.clientId;
                }

                connection.ITransport.Start();

                return(connection);
            }
            catch (NMSException e)
            {
                try
                {
                    connection.Close();
                }
                catch
                {
                }

                throw e;
            }
            catch (Exception e)
            {
                try
                {
                    connection.Close();
                }
                catch
                {
                }

                throw NMSExceptionSupport.Create("Could not connect to broker URL: " + this.brokerUri + ". Reason: " + e.Message, e);
            }
        }
Ejemplo n.º 5
0
 public bool Display(string message)
 {
     try
     {
         var transport = TransportFactory.CreateTransport(transportType, this.Host, this.Args, 5000);
         var data      = new SendRam().GetBytes(message);
         transport.Send(data);
         transport.Dispose();
         return(true);
     }
     catch (Exception ex)
     {
         XTrace.WriteException(ex);
         return(false);
     }
 }
Ejemplo n.º 6
0
 public bool Display(List <Tuple <int, string> > messageList)
 {
     try
     {
         var transport = TransportFactory.CreateTransport(transportType, this.Host, this.Args, 5000);
         var data      =
             new SendRam().GetBytes(
                 messageList.OrderBy(item => item.Item1).Select(message => message.Item2).Join(" "));
         transport.Send(data);
         transport.Dispose();
         return(true);
     }
     catch (Exception ex)
     {
         XTrace.WriteException(ex);
         return(false);
     }
 }
Ejemplo n.º 7
0
 public bool Display(List <Tuple <int, string> > messageList)
 {
     try
     {
         var transport = TransportFactory.CreateTransport(transportType, this.Host, this.Args, 5000);
         messageList.ForEach(item => {
             var data = new LedEntity().GetBytes(item.Item1, item.Item2); //根据实时数据窗口的位置发送实时数据
             data.ForEach(d => {
                 transport.Send(d);                                       //分包512字节发送
             });
         });
         transport.Dispose();
         return(true);
     }
     catch (Exception ex)
     {
         XTrace.WriteException(ex);
         return(false);
     }
 }
        public IConnection CreateConnection(string userName, string password)
        {
            // Strip off the activemq prefix, if it exists.
            Uri uri = new Uri(URISupport.stripPrefix(brokerUri.OriginalString, "activemq:"));

            Tracer.InfoFormat("Connecting to: {0}", uri.ToString());

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

            // Set properties on connection using parameters prefixed with "connection."
            // Since this could be a composite Uri, assume the connection-specific parameters
            // are associated with the outer-most specification of the composite Uri. What's nice
            // is that this works with simple Uri as well.
            URISupport.CompositeData c = URISupport.parseComposite(uri);
            URISupport.SetProperties(connection, c.Parameters, "connection.");

            connection.ITransport.Start();
            return(connection);
        }
Ejemplo n.º 9
0
        public IConnection CreateConnection()
        {
            ITransport transport;

            if (UseSsl)
            {
                var sslTransportFactory = new SslTransportFactory
                {
                    SslProtocol = "Tls"
                };

                transport = sslTransportFactory.CreateTransport(BrokerAddress);
            }
            else
            {
                transport = TransportFactory.CreateTransport(BrokerAddress);
            }

            return(new Connection(BrokerAddress, transport, new IdGenerator())
            {
                UserName = Username,
                Password = Password
            });
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Check and ensure that the connection objcet is connected.  If it is not
        /// connected or is closed, a ConnectionClosedException is thrown.
        /// </summary>
        internal void CheckConnected()
        {
            if (closed.Value)
            {
                throw new ConnectionClosedException();
            }

            if (!connected.Value)
            {
                DateTime timeoutTime = DateTime.Now + this.RequestTimeout;
                int      waitCount   = 1;

                while (true)
                {
                    if (Monitor.TryEnter(connectedLock))
                    {
                        try
                        {
                            if (closed.Value || closing.Value)
                            {
                                break;
                            }
                            else if (!connected.Value)
                            {
                                if (!this.userSpecifiedClientID)
                                {
                                    this.info.ClientId = this.clientIdGenerator.GenerateId();
                                }

                                try
                                {
                                    if (null != transport)
                                    {
                                        // Make sure the transport is started.
                                        if (!this.transport.IsStarted)
                                        {
                                            this.transport.Start();
                                        }

                                        // Send the connection and see if an ack/nak is returned.
                                        Response response = transport.Request(this.info, this.RequestTimeout);
                                        if (!(response is ExceptionResponse))
                                        {
                                            connected.Value = true;
                                        }
                                        else
                                        {
                                            ExceptionResponse error     = response as ExceptionResponse;
                                            NMSException      exception = CreateExceptionFromBrokerError(error.Exception);
                                            // This is non-recoverable.
                                            // Shutdown the transport connection, and re-create it, but don't start it.
                                            // It will be started if the connection is re-attempted.
                                            this.transport.Stop();
                                            ITransport newTransport = TransportFactory.CreateTransport(this.brokerUri);
                                            SetTransport(newTransport);
                                            throw exception;
                                        }
                                    }
                                }
                                catch
                                {
                                }
                            }
                        }
                        finally
                        {
                            Monitor.Exit(connectedLock);
                        }
                    }

                    if (connected.Value || closed.Value || closing.Value || DateTime.Now > timeoutTime)
                    {
                        break;
                    }

                    // Back off from being overly aggressive.  Having too many threads
                    // aggressively trying to connect to a down broker pegs the CPU.
                    Thread.Sleep(5 * (waitCount++));
                }

                if (!connected.Value)
                {
                    throw new ConnectionClosedException();
                }
            }
        }
Ejemplo n.º 11
0
 public IOtpTransport CreateTransport(string addr, int port) => TransportFactory.CreateTransport(addr, port);
Ejemplo n.º 12
0
 public IOtpTransport CreateTransport(IPEndPoint addr) => TransportFactory.CreateTransport(addr);