From 7feab3b9cd4d0e058d445b861a731d378f46ace8 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 18:26:02 +0200 Subject: [PATCH] [JMS] PizzeriaGUI now displays DeliveryOrders. --- .../DeliveryOrdersToCook.java | 21 ++- .../CookedDeliveryOrders.java | 60 ++++--- .../sbc/valesriegler/group/DeliveryGroup.java | 6 +- .../gui/tablemodels/DeliveryOrdersModel.java | 151 +++++++++++------- .../pizzeria/jms/JMSPizzeriaConnector.java | 31 +++- 5 files changed, 180 insertions(+), 89 deletions(-) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java index dc26967..3495fd1 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java @@ -1,5 +1,18 @@ 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.actions.DeliveryOrderInfo; import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; @@ -7,11 +20,6 @@ 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; -import org.apache.activemq.ActiveMQConnectionFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.jms.*; /** * Cook the requested pizza. @@ -41,6 +49,7 @@ public class DeliveryOrdersToCook implements MessageListener { for (PizzaOrder pizzaorder : dor.getDeliveryGroupData().getOrder().getOrderedPizzas()) { pizzaorder.setStatus(PizzaOrderStatus.IN_PREPARATION); + pizzaorder.setCookId(cook.getId()); } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(cook.getCONNECTSTRING()); @@ -70,7 +79,7 @@ public class DeliveryOrdersToCook implements MessageListener { prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); prod.send(session.createObjectMessage(doi)); - // inform pizeria + // inform pizzeria prod = session.createProducer(session.createQueue("PizzeriaConnector")); prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // WTF: oi = new OrderInfo(orderrequest.getGroupdata(), cook.getId()); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java index 6c08250..060f876 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/CookedDeliveryOrders.java @@ -1,14 +1,23 @@ package at.ac.tuwien.sbc.valesriegler.driver.jms.messageListeners; -import at.ac.tuwien.sbc.valesriegler.common.Util; -import at.ac.tuwien.sbc.valesriegler.cook.actions.DeliveryOrderInfo; -import at.ac.tuwien.sbc.valesriegler.driver.actions.OrderDeliveredInfo; -import at.ac.tuwien.sbc.valesriegler.driver.jms.JMSDriver; +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 javax.jms.*; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.cook.actions.DeliveryOrderInfo; +import at.ac.tuwien.sbc.valesriegler.driver.actions.OrderDeliveredInfo; +import at.ac.tuwien.sbc.valesriegler.driver.jms.JMSDriver; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; /** * @@ -39,28 +48,39 @@ public class CookedDeliveryOrders implements MessageListener { // generate random delay Thread.sleep((long) (Math.random() * 10000)); String addr = doi.getDeliveryGroupData().getAddress(); + boolean delivered = false; + OrderDeliveredInfo odi = new OrderDeliveredInfo(doi.getDeliveryGroupData(), driver.getId()); // Deliver to destination broker - ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Util.JMS_DELIVERY_DESTINATION); - Connection connection = connectionFactory.createConnection(); - connection.start(); - log.debug("CookedDelivery:ActiveMQConnectionFactory::" + connection); + try { + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Util.JMS_DELIVERY_DESTINATION); + Connection connection = connectionFactory.createConnection(); + connection.start(); + log.debug("CookedDelivery:ActiveMQConnectionFactory::" + connection); - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer prod = session.createProducer(session.createQueue(addr)); - prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - OrderDeliveredInfo odi = new OrderDeliveredInfo(doi.getDeliveryGroupData(), driver.getId()); - prod.send(session.createObjectMessage(odi)); - session.close(); - connection.close(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer prod = session.createProducer(session.createQueue(addr)); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + odi.getDeliveryGroupData().setDeliveryStatus(DeliveryStatus.DELIVERED); + prod.send(session.createObjectMessage(odi)); + session.close(); + connection.close(); + delivered = true; + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + + if (!delivered) { + odi.getDeliveryGroupData().setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED); + } // Inform pizzeria - connectionFactory = new ActiveMQConnectionFactory(driver.getCONNECTSTRING()); - connection = connectionFactory.createConnection(); + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(driver.getCONNECTSTRING()); + Connection connection = connectionFactory.createConnection(); connection.start(); - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - prod = session.createProducer(session.createQueue("PizzeriaConnector")); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer prod = session.createProducer(session.createQueue("PizzeriaConnector")); prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); prod.send(session.createObjectMessage(odi)); session.close(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java index fd21703..66e447f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java @@ -1,12 +1,13 @@ package at.ac.tuwien.sbc.valesriegler.group; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class DeliveryGroup implements Runnable, HasId { private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); @@ -44,6 +45,7 @@ public class DeliveryGroup implements Runnable, HasId { case START : // should not happen. break; case ORDER_PENDING : + getDeliveryGroupData().getOrder().genId(); JMSGroupConnector conn = JMSGroupConnector.getConnector(deliveryGroupData.getPizzeriaId()); conn.send(new DeliveryOrderRequest(deliveryGroupData)); break; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java index 336bfd3..9335d60 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java @@ -1,74 +1,105 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; -import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; -import at.ac.tuwien.sbc.valesriegler.types.Order; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; +import at.ac.tuwien.sbc.valesriegler.types.Order; + public class DeliveryOrdersModel extends AbstractOrdersModel { - private static final Logger log = LoggerFactory.getLogger(DeliveryOrdersModel.class); + private static final Logger log = LoggerFactory.getLogger(DeliveryOrdersModel.class); - private static final String ID = "Delivery ID"; - private static final String ORDER_ID = "Order ID"; - private static final String DELIVERY_ADDRESS = "Address"; - private static final String STATUS = "Status"; - private static final String LOAD_BALANCER_ID = "Moved by"; - private static final String ORIGINAL_PIZZERIA = "Moved from"; + private static final String ID = "Delivery ID"; + private static final String ORDER_ID = "Order ID"; + private static final String DELIVERY_ADDRESS = "Address"; + private static final String STATUS = "Status"; + private static final String LOAD_BALANCER_ID = "Moved by"; + private static final String ORIGINAL_PIZZERIA = "Moved from"; - private static final String[] COLUMNS = new String[] { ORDER_ID, ID, DELIVERY_ADDRESS, STATUS, LOAD_BALANCER_ID, ORIGINAL_PIZZERIA }; + private static final String[] COLUMNS = new String[]{ORDER_ID, ID, DELIVERY_ADDRESS, STATUS, LOAD_BALANCER_ID, + ORIGINAL_PIZZERIA}; - @Override - protected String[] getColumns() { - return COLUMNS; - } + @Override + protected String[] getColumns() { + return COLUMNS; + } - @Override - public Object getValueAt(int rowIndex, int columnIndex) { - List values = new ArrayList<>(items.values()); - // TODO: make sure that is necessary - Collections.sort(values, new Comparator() { - @Override - public int compare(DeliveryGroupData o1, DeliveryGroupData o2) { - final int o1Id = o1.getOrder().getId(); - final int o2Id = o2.getOrder().getId(); - if(o1Id==0) { - if(o2Id>0) return 1; - } - if(o2Id==0) { - if(o1Id>0) return 1; - } - if (o1Id < o2Id) return -1; - else if (o1Id > o2Id) return 1; - else return 0; - } - }); - DeliveryGroupData group = values.get(rowIndex); - Order order = group.getOrder(); - String wantedColumn = COLUMNS[columnIndex]; - switch (wantedColumn) { - case ID: - return group.getId(); - case ORDER_ID: - return order.getId(); - case DELIVERY_ADDRESS: - return group.getAddress(); - case STATUS: - return group.getDeliveryStatus(); - case LOAD_BALANCER_ID: - return group.getLoadBalancerId(); - case ORIGINAL_PIZZERIA: - return group.getOriginalPizzeriaId(); - default: - throw new RuntimeException(UNHANDLEDCOLUMN); - } - } + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + List values = new ArrayList<>(items.values()); + // TODO: make sure that is necessary + Collections.sort(values, new Comparator() { + @Override + public int compare(DeliveryGroupData o1, DeliveryGroupData o2) { + final int o1Id = o1.getOrder().getId(); + final int o2Id = o2.getOrder().getId(); + if (o1Id == 0) { + if (o2Id > 0) + return 1; + } + if (o2Id == 0) { + if (o1Id > 0) + return 1; + } + if (o1Id < o2Id) + return -1; + else if (o1Id > o2Id) + return 1; + else + return 0; + } + }); + DeliveryGroupData group = values.get(rowIndex); + Order order = group.getOrder(); + String wantedColumn = COLUMNS[columnIndex]; + switch (wantedColumn) { + case ID : + return group.getId(); + case ORDER_ID : + return order.getId(); + case DELIVERY_ADDRESS : + return group.getAddress(); + case STATUS : + return group.getDeliveryStatus(); + case LOAD_BALANCER_ID : + return group.getLoadBalancerId(); + case ORIGINAL_PIZZERIA : + return group.getOriginalPizzeriaId(); + default : + throw new RuntimeException(UNHANDLEDCOLUMN); + } + } - public DeliveryGroupData getGroupOfRow(int rowIndex) { - return new ArrayList<>(items.values()).get(rowIndex); - } + public DeliveryGroupData getGroupOfRow(int rowIndex) { + List values = new ArrayList<>(items.values()); + // TODO: make sure that is necessary + Collections.sort(values, new Comparator() { + @Override + public int compare(DeliveryGroupData o1, DeliveryGroupData o2) { + final int o1Id = o1.getOrder().getId(); + final int o2Id = o2.getOrder().getId(); + if (o1Id == 0) { + if (o2Id > 0) + return 1; + } + if (o2Id == 0) { + if (o1Id > 0) + return 1; + } + if (o1Id < o2Id) + return -1; + else if (o1Id > o2Id) + return 1; + else + return 0; + } + }); + return values.get(rowIndex); + // return new ArrayList<>(items.values()).get(rowIndex); + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java index 2c4e44b..20b0942 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java @@ -17,7 +17,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; +import at.ac.tuwien.sbc.valesriegler.cook.actions.DeliveryOrderInfo; import at.ac.tuwien.sbc.valesriegler.cook.actions.OrderInfo; +import at.ac.tuwien.sbc.valesriegler.driver.actions.OrderDeliveredInfo; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderResponse; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; import at.ac.tuwien.sbc.valesriegler.group.actions.PayResponse; import at.ac.tuwien.sbc.valesriegler.group.actions.TableFree; @@ -123,6 +126,33 @@ public class JMSPizzeriaConnector extends AbstractPizzeriaConnector implements M t.setGroupId(-1); PizzeriaAgent.getInstance().getTablesModel().fireTableDataChanged(); return; + + } else if (obj instanceof DeliveryOrderResponse) { + DeliveryOrderResponse deliveryOrderResponse = (DeliveryOrderResponse) obj; + log.debug("Received: " + deliveryOrderResponse); + deliveryOrderResponse.getDeliveryGroupData().setWaiterIdOfOrder(deliveryOrderResponse.getWaiterId()); + ArrayList gd = new ArrayList(); + gd.add(deliveryOrderResponse.getDeliveryGroupData()); + PizzeriaAgent.getInstance().getDeliveryOrdersModel().addItems(gd); + return; + + } else if (obj instanceof DeliveryOrderInfo) { + DeliveryOrderInfo deliveryOrderInfo = (DeliveryOrderInfo) obj; + log.debug("Received: " + deliveryOrderInfo); + ArrayList gd = new ArrayList(); + gd.add(deliveryOrderInfo.getDeliveryGroupData()); + PizzeriaAgent.getInstance().getDeliveryOrdersModel().addItems(gd); + return; + + } else if (obj instanceof OrderDeliveredInfo) { + OrderDeliveredInfo orderDeliveredInfo = (OrderDeliveredInfo) obj; + log.debug("Received: " + orderDeliveredInfo); + orderDeliveredInfo.getDeliveryGroupData().setDriverId(orderDeliveredInfo.getDriverId()); + ArrayList gd = new ArrayList(); + gd.add(orderDeliveredInfo.getDeliveryGroupData()); + PizzeriaAgent.getInstance().getDeliveryOrdersModel().addItems(gd); + return; + } log.warn("Unknown message received!" + obj); } @@ -132,7 +162,6 @@ public class JMSPizzeriaConnector extends AbstractPizzeriaConnector implements M log.error("EXCEPTION!", e); } } - @Override public void send(AbstractAction request) { try { -- 2.43.0