From a4de1a71bfe0f2184d7e2e5d9ee813c204bc347e Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Sat, 11 May 2013 15:17:34 +0200 Subject: [PATCH] Waiter now sends the whole orderrequest to the Cook. --- .../valesriegler/cook/actions/OrderInfo.java | 43 ++++++++++ .../messageListeners/CookOrderedPizzas.java | 85 +++++++++++++++++++ .../tuwien/sbc/valesriegler/group/Group.java | 13 +-- .../jms/messageListeners/WantToOrder.java | 23 ++--- 4 files changed, 140 insertions(+), 24 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/OrderInfo.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookOrderedPizzas.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/OrderInfo.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/OrderInfo.java new file mode 100644 index 0000000..e14b586 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/OrderInfo.java @@ -0,0 +1,43 @@ +package at.ac.tuwien.sbc.valesriegler.cook.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; + +/** + * Inform Pizzaria of a Pizza-Event. + * + * @author jan + * + */ +public class OrderInfo extends AbstractAction implements Serializable { + private final int pizzaId; + private final PizzaOrderStatus status; + private final int cookId; + + public OrderInfo(int pizzaId, PizzaOrderStatus status, int cookId) { + super(null); + this.pizzaId = pizzaId; + this.status = status; + this.cookId = cookId; + } + + public int getPizzaId() { + return pizzaId; + } + + public PizzaOrderStatus getStatus() { + return status; + } + + public int getCookId() { + return cookId; + } + + @Override + public String toString() { + return "PizzaInfo [pizzaId=" + pizzaId + ", status=" + status + ", cookId=" + cookId + "]"; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookOrderedPizzas.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookOrderedPizzas.java new file mode 100644 index 0000000..a1e2a5e --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookOrderedPizzas.java @@ -0,0 +1,85 @@ +package at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners; + +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.cook.JMSCook; +import at.ac.tuwien.sbc.valesriegler.cook.actions.PizzaInfo; +import at.ac.tuwien.sbc.valesriegler.types.Pizza; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; + +/** + * Cook the requested pizza. + * + * @author jan + * + */ +public class CookOrderedPizzas implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(CookOrderedPizzas.class); + private final JMSCook cook; + + public CookOrderedPizzas(JMSCook cook) { + this.cook = cook; + } + + @Override + public void onMessage(Message msg) { + try { + msg.acknowledge(); + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof PizzaOrder) { + PizzaOrder pizzaorder = (PizzaOrder) obj; + log.debug("Received: " + pizzaorder); + + // generate random delay + Thread.sleep((long) (pizzaorder.getPizzaType().duration * 1000)); + pizzaorder.setStatus(PizzaOrderStatus.DONE); + + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer prodCP = session.createProducer(session.createQueue("CookedPizzas")); + prodCP.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + prodCP.send(session.createObjectMessage(Pizza.createPizzaFromPizzaOrder(pizzaorder, cook.getId()))); + + MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("PizzeriaConnector")); + wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + PizzaInfo pi = new PizzaInfo(pizzaorder.getId(), pizzaorder.getStatus(), cook.getId()); + wantToSitAtTable.send(session.createObjectMessage(pi)); + session.close(); + + connection.close(); + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } catch (InterruptedException e) { + log.error("EXCEPTION!", e); + } + } + + @Override + public String toString() { + return "CookRequestedPizza [cook=" + cook + "]"; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java index 81ca81c..143f6a2 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java @@ -1,7 +1,5 @@ package at.ac.tuwien.sbc.valesriegler.group; -import java.io.Serializable; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,6 +11,7 @@ import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupState; import at.ac.tuwien.sbc.valesriegler.types.Order; +import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; import at.ac.tuwien.sbc.valesriegler.types.Table; /** @@ -27,16 +26,13 @@ public class Group implements Runnable, HasId { private GroupData groupData; - public Group() { groupData = new GroupData(++idNext); } - + public Group(Integer id) { groupData = new GroupData(id); } - - @Override public void run() { @@ -76,7 +72,6 @@ public class Group implements Runnable, HasId { e.printStackTrace(); } } - public void setGroupData(GroupData groupData) { this.groupData = groupData; @@ -112,7 +107,7 @@ public class Group implements Runnable, HasId { // Waiter telling group he/she forwarded the order. public void orderReceived(int waiter) { groupData.setState(GroupState.ORDERED); - groupData.setState(GroupState.EATING); // TODO: skip pizza creation step + groupData.getOrder().setStatus(OrderStatus.ORDERED); groupData.setOrderWaiter(waiter); new Thread(this).start(); log.debug("orderReceived(): " + this); @@ -131,7 +126,7 @@ public class Group implements Runnable, HasId { public int getId() { return groupData.getId(); } - + // pay for the pizzas and leave public void payForPizzas(int waiter) { groupData.setState(GroupState.PAY); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java index a2d1f14..3456408 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java @@ -15,8 +15,6 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; -import at.ac.tuwien.sbc.valesriegler.types.Order; -import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; /** @@ -53,26 +51,21 @@ public class WantToOrder implements MessageListener { connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer prodOP = session.createProducer(session.createQueue("OrderedPizzas")); + // Make cooks do their work + MessageProducer prodOP = session.createProducer(session.createQueue("OrdersToCook")); prodOP.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + prodOP.send(session.createObjectMessage(orderrequest)); - // Create the queue for the cooks. - Order o = orderrequest.getGroupdata().getOrder(); - for (PizzaOrder po : o.getOrderedPizzas()) { - prodOP.send(session.createObjectMessage(po)); - } - - // Also put the order in the Pending order queue for the waiter to - // check. - MessageProducer prodPending = session.createProducer(session.createQueue("PendingOrders")); - prodPending.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - prodPending.send(session.createObjectMessage(o)); - + // inform Group + Pizzeria MessageProducer informGroup = session.createProducer(session.createQueue("GroupConnector")); informGroup.setDeliveryMode(DeliveryMode.NON_PERSISTENT); OrderResponse or = new OrderResponse(orderrequest.getGroupdata(), waiter.getId()); informGroup.send(session.createObjectMessage(or)); + MessageProducer informPizzeria = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzeria.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzeria.send(session.createObjectMessage(or)); + session.close(); connection.close(); } else { -- 2.43.0