public void Consume(string queueName)
        {
            try
            {
                string[] routingKeys = File.ReadAllLines(topicsDir);
                var factory = new ConnectionFactory() { HostName = "localhost" };
                using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange: "audio_job", type: "topic");
                    channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
                    channel.BasicQos(0, 1, false);
                    //var queueName = channel.QueueDeclare().QueueName; //not good because it randomly generates queues
                    if (routingKeys.Length < 1)
                    {
                        Console.Error.WriteLine("Error no routing key set");
                        Console.WriteLine(" Press [enter] to exit.");
                        Console.ReadLine();
                        Environment.ExitCode = 1;
                        return;
                    }

                    foreach (var bindingKey in routingKeys)
                    {
                        channel.QueueBind(queue: queueName,
                                          exchange: "audio_job",
                                          routingKey: bindingKey);
                    }

                    Console.WriteLine(" [*] QUEUE NAME: " + queueName);
                    Console.WriteLine(" [*] Waiting for messages. To exit press CTRL+C");
                   // while (true)
                   // {
                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += (model, ea) =>
                        {
                            var body = ea.Body;
                            //var message = Encoding.UTF8.GetString(body);
                            var routingKey = ea.RoutingKey;
                            var props = ea.BasicProperties;
                            var tag = ea.DeliveryTag;
                            Console.WriteLine(" [x] Received '{0}':'{1}'",
                                              routingKey,
                                              "Audio Job");
                            object inputPropriety, inputLanguage, voiceSpeed, format;
                            props.Headers.TryGetValue("inputPropriety", out inputPropriety);
                            props.Headers.TryGetValue("inputLanguage", out inputLanguage);
                            props.Headers.TryGetValue("voiceSpeed", out voiceSpeed);
                            props.Headers.TryGetValue("format", out format);

                            //channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);

                            AudioJobProcessor ajp = new AudioJobProcessor();
                            byte[] response = ajp.SubmitWorkItem(body, Encoding.UTF8.GetString((byte[])inputPropriety), (int)inputLanguage, (int)voiceSpeed, (int)format);
                            bool worked = true;
                            if (response == null)
                            {
                                worked = false;
                            }
                            this.SendResponse(props, response, tag, worked);
                        };
                        channel.BasicConsume(queue: queueName,
                                             noAck: true,
                                             consumer: consumer);
                        Processor.CheckForAudioAgentInstances();
                        Console.ReadLine();
                        //Thread.Sleep(800);
                   // }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Application will now exit");
                Console.ReadLine();
            }
        }
Esempio n. 2
0
        public void run(string[] args)
        {
            string queueName = "rba16";

            if (File.Exists(workingDir + "\\agent.config"))
            {
                queueName = File.ReadAllText(workingDir + "\\agent.config");
            }
            bool   skip  = false;
            string input = "";

            if (args.Length > 0)
            {
                input = args[0];
                if (input.Equals("startup", StringComparison.InvariantCultureIgnoreCase))
                {
                    skip = true;
                }
            }
            if (File.Exists(workingDir + "\\topics.config"))
            {
                oldTopics = File.ReadAllLines(workingDir + "\\topics.config");
                if (oldTopics.Length > 0)
                {
                    topics = oldTopics;
                }
            }
            string res = "";

            foreach (var topic in topics)
            {
                res += topic + " ";
            }
            Console.WriteLine("Current queue is: " + queueName);
            Console.WriteLine("Current binding keys are: " + res);
            if (!skip)
            {
                Console.WriteLine(consoleMessage);
                ConsoleKeyInfo ck   = Console.ReadKey();
                bool           loop = true;
                do
                {
                    if (ConsoleKey.P.Equals(ck.Key))
                    {
                        AudioJobProcessor.WriteAllInstalledVoicesToFile();
                        Console.WriteLine(consoleMessage);
                        ck = Console.ReadKey();
                    }
                    if (ConsoleKey.N.Equals(ck.Key))
                    {
                        AudioJobProcessor.WriteVoiceNames();
                        Console.WriteLine(consoleMessage);
                        ck = Console.ReadKey();
                    }
                    if (ConsoleKey.C.Equals(ck.Key))
                    {
                        Console.WriteLine("Current binding keys are: " + res);
                        Console.WriteLine("Write each key separated by commas (,):");
                        string keys = Console.ReadLine();
                        topics = keys.Split(',');
                        Console.WriteLine(consoleMessage);
                        ck = Console.ReadKey();
                    }
                    if (ConsoleKey.Q.Equals(ck.Key))
                    {
                        Console.WriteLine("Current queue is: " + queueName + " Write new queue name: ");
                        queueName = Console.ReadLine();
                        Console.WriteLine(consoleMessage);
                        ck = Console.ReadKey();
                    }
                    if (ConsoleKey.Enter.Equals(ck.Key))
                    {
                        loop = false;
                        //ck = Console.ReadKey();
                    }
                } while (loop);
                res = "";
                foreach (var topic in topics)
                {
                    res += topic + " ";
                }
                if (!File.Exists(workingDir + "\\agent.config"))
                {
                    File.Create(workingDir + "\\agent.config").Close();
                }
                File.WriteAllText(workingDir + "\\agent.config", queueName);

                if (!File.Exists(workingDir + "\\topics.config"))
                {
                    File.Create(workingDir + "\\topics.config").Close();
                }
                File.WriteAllLines(workingDir + "\\topics.config", topics);
            }
            Console.WriteLine("Audio Conversion Agent started listening on topics: " + res);
            QueueConsumer qc = new QueueConsumer();

            qc.Consume(queueName.Trim());
        }
        public void Consume(string queueName)
        {
            try
            {
                string[] routingKeys = File.ReadAllLines(topicsDir);
                var      factory     = new ConnectionFactory()
                {
                    HostName = "localhost"
                };
                using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        channel.ExchangeDeclare(exchange: "audio_job", type: "topic");
                        channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
                        channel.BasicQos(0, 1, false);
                        //var queueName = channel.QueueDeclare().QueueName; //not good here because it randomly generates queues
                        if (routingKeys.Length < 1)
                        {
                            Console.Error.WriteLine("Error no routing key set");
                            Console.WriteLine(" Press [enter] to exit.");
                            Console.ReadLine();
                            Environment.ExitCode = 1;
                            return;
                        }

                        foreach (var bindingKey in routingKeys)
                        {
                            channel.QueueBind(queue: queueName,
                                              exchange: "audio_job",
                                              routingKey: bindingKey);
                        }

                        Console.WriteLine(" [*] QUEUE NAME: " + queueName);
                        Console.WriteLine(" [*] Waiting for messages. To exit press CTRL+C");
                        // while (true)
                        // {
                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += (model, ea) =>
                        {
                            var body = ea.Body;
                            //var message = Encoding.UTF8.GetString(body);
                            var routingKey = ea.RoutingKey;
                            var props      = ea.BasicProperties;
                            var tag        = ea.DeliveryTag;
                            Console.WriteLine(" [x] Received '{0}' Time: '{1}'",
                                              routingKey,
                                              DateTime.Now);
                            props.Headers.TryGetValue("inputPropriety", out object inputPropriety);
                            props.Headers.TryGetValue("inputLanguage", out object inputLanguage);
                            props.Headers.TryGetValue("voiceSpeed", out object voiceSpeed);
                            props.Headers.TryGetValue("format", out object format);

                            //channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);

                            AudioJobProcessor ajp      = new AudioJobProcessor();
                            byte[]            response = ajp.SubmitWorkItem(body, Encoding.UTF8.GetString((byte[])inputPropriety), (int)inputLanguage, (int)voiceSpeed, (int)format);
                            bool worked = true;
                            if (response == null)
                            {
                                worked = false;
                            }
                            this.SendResponse(props, response, tag, worked);
                        };
                        channel.BasicConsume(queue: queueName,
                                             noAck: true,
                                             consumer: consumer);
                        Processor.CheckForAudioAgentInstances();
                        Console.ReadLine();
                        //Thread.Sleep(800);
                        // }
                    }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Application will now exit");
                Console.ReadLine();
            }
        }