private void HandleRetailerRequest(RetailerRequest request) { WarehouseProgram.RecieveMessage(id, request.Order.ProductIds[0], country.ToString()); WarehouseReply reply = new WarehouseReply() { Order = request.Order }; Product p = products.FirstOrDefault(x => x.ProductId == request.Order.ProductIds[0]); if (p != null && p.ItemsInStock > 0) { reply.Order.IsAvailable = true; if (request.Order.CountryCode.Equals(country)) { reply.Order.DeliveryTime = 2; reply.Order.ShippingCharge = 5; } else { reply.Order.DeliveryTime = 4; reply.Order.ShippingCharge = 10; } } else { reply.Order.IsAvailable = false; } bus.Send <WarehouseReply>("warehouse.retailer", reply); WarehouseProgram.SendMessage(id, reply.Order.IsAvailable); }
private static void HandleWareHouseMessage(WarehouseReply warehouseMsg) { if (CustomerRequests.Find(x => x.CustomerId == warehouseMsg.Order.CustomerId) != null) { if (!HasPublished[warehouseMsg.Order.CustomerId]) { Console.WriteLine("Recieved message from a local warehouse."); if (warehouseMsg.Order.IsAvailable) { Console.WriteLine("Product is available..\nSending to client.."); RetailerReply msg = new RetailerReply() { ProductId = warehouseMsg.Order.ProductIds[0], IsAvailable = warehouseMsg.Order.IsAvailable }; Bus.Send <RetailerReply>($"retailer.client.{warehouseMsg.Order.CustomerId}", msg); CustomerRequests.RemoveAll(x => x.CustomerId == warehouseMsg.Order.CustomerId); WarehouseReplyLimiter.Remove(warehouseMsg.Order.CustomerId); HasPublished.Remove(warehouseMsg.Order.CustomerId); Console.WriteLine($"Response sent to customer with client id: {warehouseMsg.Order.CustomerId}"); } else { Console.WriteLine("Product not available.. Publishing to all warehouses.."); RetailerRequest req = new RetailerRequest() { Order = warehouseMsg.Order }; Bus.Publish <RetailerRequest>(req, "retailer.warehouses"); HasPublished[warehouseMsg.Order.CustomerId] = true; } } else { Console.WriteLine("Recieved message from one of all warehouses.."); if (warehouseMsg.Order.IsAvailable || ++WarehouseReplyLimiter[warehouseMsg.Order.CustomerId] >= MAX_WAREHOUSES) { RetailerReply reply = new RetailerReply() { ProductId = warehouseMsg.Order.ProductIds[0], IsAvailable = warehouseMsg.Order.IsAvailable }; Bus.Send <RetailerReply>($"retailer.client.{warehouseMsg.Order.CustomerId}", reply); CustomerRequests.RemoveAll(x => x.CustomerId == warehouseMsg.Order.CustomerId); WarehouseReplyLimiter.Remove(warehouseMsg.Order.CustomerId); HasPublished.Remove(warehouseMsg.Order.CustomerId); Console.WriteLine($"Response sent to customer with client id: {warehouseMsg.Order.CustomerId}"); } } } }