Пример #1
0
 public BillLine(ItemLine itemLine)
 {
     this.itemLine = itemLine;
     subtotal      = itemLine.getQuantiy() * itemLine.getUnitPrice();
 }
Пример #2
0
        public static void Main()
        {
            StockManager stockManager = new StockManager();

            var factory = new ConnectionFactory()
            {
                HostName = "localhost"
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "server_stock_queue", durable: false,
                                         exclusive: false, autoDelete: false, arguments: null);
                    channel.BasicQos(0, 1, false);
                    var consumer = new EventingBasicConsumer(channel);
                    channel.BasicConsume(queue: "server_stock_queue",
                                         autoAck: false, consumer: consumer);
                    Console.WriteLine(" [x] Awaiting RPC stock requests");


                    consumer.Received += (model, ea) =>
                    {
                        ItemLine response  = new ItemLine();
                        string   response2 = null;

                        var body       = ea.Body;
                        var props      = ea.BasicProperties;
                        var replyProps = channel.CreateBasicProperties();
                        replyProps.CorrelationId = props.CorrelationId;

                        try
                        {
                            var message = Encoding.UTF8.GetString(body.ToArray());

                            if (message.Equals("products"))
                            {
                                response2 = "PRODUCTS \n" + stockManager.affichageCurrentListe();
                            }
                            else
                            {
                                string[] numbers     = Regex.Split(message, @"\D+");
                                string   name        = stockManager.getNameByIndex(int.Parse(numbers[0]));
                                int      quantity    = int.Parse(numbers[1]);
                                int      maxQuantity = stockManager.quantityByName(name);
                                ItemLine reserved    = stockManager.ReserveItem(quantity, name);

                                if (quantity >= maxQuantity)
                                {
                                    response = new ItemLine(reserved.getName(), reserved.getUnitPrice(), maxQuantity);
                                }

                                else
                                {
                                    response = new ItemLine(reserved.getName(), reserved.getUnitPrice(), quantity);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(" [error] " + e.Message);
                            response = null;
                        }
                        finally
                        {
                            Console.WriteLine(" [....] " + " Trying to deliver " + response.getName());
                            if (response2 != null)
                            {
                                var responseBytes = Encoding.UTF8.GetBytes(response2);
                                channel.BasicPublish(exchange: "", routingKey: props.ReplyTo,
                                                     basicProperties: replyProps, body: responseBytes);
                                channel.BasicAck(deliveryTag: ea.DeliveryTag,
                                                 multiple: false);
                                Console.WriteLine(" [..OK...] " + " Liste products SENT  ");
                            }

                            else
                            {
                                var responseBytes = ToByteArray <ItemLine>(response);
                                channel.BasicPublish(exchange: "", routingKey: props.ReplyTo,
                                                     basicProperties: replyProps, body: responseBytes);
                                channel.BasicAck(deliveryTag: ea.DeliveryTag,
                                                 multiple: false);
                                Console.WriteLine(" [..OK...] " + " Item SENT ");
                            }
                        }
                    };

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
        }