From b85af131629fba5caf79dcdb0c3c3b612a7b78ac Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Sat, 11 May 2013 15:37:08 +0200 Subject: [PATCH] cook pizza orders and let the waiters to deliver them. --- .../tuwien/sbc/valesriegler/common/Util.java | 43 ++++--- .../tuwien/sbc/valesriegler/cook/JMSCook.java | 5 +- .../valesriegler/cook/actions/OrderInfo.java | 22 +--- .../valesriegler/cook/actions/PizzaInfo.java | 43 ------- .../messageListeners/CookOrderedPizzas.java | 85 -------------- .../messageListeners/CookRequestedPizza.java | 85 -------------- .../jms/messageListeners/OrdersToCook.java | 106 ++++++++++++++++++ .../sbc/valesriegler/types/OrderStatus.java | 2 +- .../tuwien/sbc/valesriegler/types/Pizza.java | 33 +++--- 9 files changed, 152 insertions(+), 272 deletions(-) delete mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/PizzaInfo.java delete mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookOrderedPizzas.java delete mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookRequestedPizza.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java index 7a0a02f..25a2296 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java @@ -1,6 +1,5 @@ package at.ac.tuwien.sbc.valesriegler.common; -import java.io.Serializable; import java.net.URI; import java.util.Collections; import java.util.HashMap; @@ -19,15 +18,15 @@ import org.slf4j.LoggerFactory; public abstract class Util { private static final Logger log = LoggerFactory.getLogger(Util.class); - + // TODO: solve the switch between mom by command-line arguments - public static final boolean useJMS = false; - + public static final boolean useJMS = true; + public static final String TABLE_ASSIGNED = "tables"; public static final String GROUPS_CONTAINER = "groups"; public static final String ASSIGN_TABLE = "assignTable"; - public static final String TAKE_ORDER = "takeOrder"; - public static final String ORDER_TAKEN = "order"; + public static final String TAKE_ORDER = "takeOrder"; + public static final String ORDER_TAKEN = "order"; public static final String DELIVER_PIZZAS = "deliverPizzas"; public static final String PREPARE_PIZZAS = "preparePizzas"; public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress"; @@ -36,48 +35,48 @@ public abstract class Util { public static final String FREE_TABLES = "freeTables"; public static final String IS_EATING = "isEating"; public static final String PAYMENT_DONE = "hasPaid"; - + private static Random random = new Random(); - + public static final long SPACE_TRANSACTION_TIMEOUT = 1500; - + public static final String SERVER_ADDR = "xvsm://localhost:9876"; - - public static ContainerReference getOrCreateNamedContainer(final String spaceUri, final String containerName, final Capi capi, final List coordinators) - throws MzsCoreException { + + public static ContainerReference getOrCreateNamedContainer(final String spaceUri, final String containerName, + final Capi capi, final List coordinators) throws MzsCoreException { ContainerReference container; try { container = capi.lookupContainer(containerName, URI.create(spaceUri), RequestTimeout.DEFAULT, null); } catch (MzsCoreException e) { - container = capi.createContainer(containerName, URI.create(spaceUri), Container.UNBOUNDED, coordinators, null, null); + container = capi.createContainer(containerName, URI.create(spaceUri), Container.UNBOUNDED, coordinators, null, + null); } return container; } - + public static String getId(int id) { return (id != 0 && id != -1) ? String.valueOf(id) : ""; } - + public static int getIntSafe(Integer nr) { return nr == null ? 0 : nr; } - + public static Map intoMapById(List hasIds) { - if(hasIds == null) { + if (hasIds == null) { return Collections.emptyMap(); } - + Map myMap = new HashMap<>(); for (T hasId : hasIds) { myMap.put(hasId.getId(), hasId); } return myMap; } - - public static int getRandom(int min, int max){ - return random.nextInt(max - min + 1) + min; - } + public static int getRandom(int min, int max) { + return random.nextInt(max - min + 1) + min; + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java index 682916b..f5b7ea4 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java @@ -10,7 +10,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.HasId; -import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.CookRequestedPizza; +import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.OrdersToCook; /** * (JMS)Cook is far too primitive to require any abstraction. It would take more @@ -40,12 +40,13 @@ public class JMSCook implements HasId { Session sessWantToSit = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consWantToSit = sessWantToSit.createConsumer(sessWantToSit.createQueue("OrderedPizzas")); - consWantToSit.setMessageListener(new CookRequestedPizza(this)); + consWantToSit.setMessageListener(new OrdersToCook(this)); } catch (JMSException e) { log.error("EXCEPTION!", e); } } + @Override public int getId() { return id; } 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 index e14b586..3000fb1 100644 --- 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 @@ -3,41 +3,29 @@ 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; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; /** - * Inform Pizzaria of a Pizza-Event. + * response to the group's interest in pizza. * * @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; + public OrderInfo(GroupData groupdata, int cookId) { + super(groupdata); 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 + "]"; + return "OrderInfo [cookId=" + cookId + "]"; } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/PizzaInfo.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/PizzaInfo.java deleted file mode 100644 index 7ac85df..0000000 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/PizzaInfo.java +++ /dev/null @@ -1,43 +0,0 @@ -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 PizzaInfo extends AbstractAction implements Serializable { - private final int pizzaId; - private final PizzaOrderStatus status; - private final int cookId; - - public PizzaInfo(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 deleted file mode 100644 index a1e2a5e..0000000 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookOrderedPizzas.java +++ /dev/null @@ -1,85 +0,0 @@ -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/cook/jms/messageListeners/CookRequestedPizza.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookRequestedPizza.java deleted file mode 100644 index 72b7663..0000000 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookRequestedPizza.java +++ /dev/null @@ -1,85 +0,0 @@ -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 CookRequestedPizza implements MessageListener { - private static final Logger log = LoggerFactory.getLogger(CookRequestedPizza.class); - private final JMSCook cook; - - public CookRequestedPizza(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/cook/jms/messageListeners/OrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java new file mode 100644 index 0000000..2a6103a --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java @@ -0,0 +1,106 @@ +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.OrderInfo; +import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; +import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; +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 OrdersToCook implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(OrdersToCook.class); + private final JMSCook cook; + + public OrdersToCook(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 OrderRequest) { + OrderRequest orderrequest = (OrderRequest) obj; + log.debug("Received: " + orderrequest); + + for (PizzaOrder pizzaorder : orderrequest.getGroupdata().getOrder().getOrderedPizzas()) { + pizzaorder.setStatus(PizzaOrderStatus.IN_PREPARATION); + } + + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + + // inform pizzeria + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer prod = session.createProducer(session.createQueue("PizzeriaConnector")); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + OrderInfo oi = new OrderInfo(orderrequest.getGroupdata(), cook.getId()); + prod.send(session.createObjectMessage(oi)); + session.close(); + + // generate random delay + for (PizzaOrder po : orderrequest.getGroupdata().getOrder().getOrderedPizzas()) { + Thread.sleep(po.getPizzaType().duration * 1000); + po.setStatus(PizzaOrderStatus.DONE); + Pizza p = Pizza.createPizzaFromPizzaOrder(po, cook.getId()); + orderrequest.getGroupdata().getOrder().getCookedPizzas().add(p); + } + orderrequest.getGroupdata().getOrder().setStatus(OrderStatus.DELIVERY_PENDING); + + // let pizzas be delivered. + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + prod = session.createProducer(session.createQueue("CookedOrders")); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + prod.send(session.createObjectMessage(oi)); + + // inform pizeria + prod = session.createProducer(session.createQueue("PizzeriaConnector")); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + oi = new OrderInfo(orderrequest.getGroupdata(), cook.getId()); + prod.send(session.createObjectMessage(oi)); + 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/types/OrderStatus.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/OrderStatus.java index c895214..cce2da8 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/OrderStatus.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/OrderStatus.java @@ -9,5 +9,5 @@ import java.io.Serializable; * */ public enum OrderStatus implements Serializable { - NEW, ORDERED, DELIVERED, PAID + NEW, ORDERED, DELIVERY_PENDING, DELIVERED, PAID } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java index a318dc2..7da7d46 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java @@ -2,7 +2,6 @@ package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; -import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; @@ -14,12 +13,15 @@ import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; */ public class Pizza extends PizzaOrder implements Serializable { public Waiter deliveryAgent; - + /** - * Yes that's funny! the pizza has a field idOfOrder although PizzaOrder already has a field orderId! - * The reason: The space linda selection is a little bit limited. when I look for a template pizza with orderId set the - * space gives me ALL pizzas. The reason obviously is that it can't really include the field of the superclass in the search. - * So we must have an idOfOrder field in the pizza and not only in the PizzaOrder for the time being.. ^^ + * Yes that's funny! the pizza has a field idOfOrder although PizzaOrder + * already has a field orderId! The reason: The space linda selection is a + * little bit limited. when I look for a template pizza with orderId set the + * space gives me ALL pizzas. The reason obviously is that it can't really + * include the field of the superclass in the search. So we must have an + * idOfOrder field in the pizza and not only in the PizzaOrder for the time + * being.. ^^ */ private Integer idOfOrder; @@ -28,9 +30,10 @@ public class Pizza extends PizzaOrder implements Serializable { private Pizza(int id, PizzaType type, int cookId, int orderId) { super(id); this.id = id; - this.pizzaType = type; + pizzaType = type; this.cookId = cookId; - this.idOfOrder = orderId; + idOfOrder = orderId; + status = PizzaOrderStatus.DONE; } public Pizza() { @@ -43,19 +46,18 @@ public class Pizza extends PizzaOrder implements Serializable { @Override public String toString() { - return "Pizza [deliveryAgent=" + deliveryAgent + ", id=" + id - + ", orderId=" + orderId + ", pizzaType=" + pizzaType + return "Pizza [deliveryAgent=" + deliveryAgent + ", id=" + id + ", orderId=" + orderId + ", pizzaType=" + pizzaType + ", status=" + status + ", cookId=" + cookId + "]"; } - - @Override + + @Override public int getOrderId() { return getIdOfOrder(); }; - + @Override public void setOrderId(Integer orderId) { - this.idOfOrder = orderId; + idOfOrder = orderId; }; public int getIdOfOrder() { @@ -66,7 +68,4 @@ public class Pizza extends PizzaOrder implements Serializable { this.idOfOrder = idOfOrder; } - - - } -- 2.43.0