From 5f52e3235f587d003c54151b1e3a3e18ff9688e7 Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 10 Jun 2013 02:39:10 +0200 Subject: [PATCH] [JMS] NEW workflow outlined. implementation tomorrow. --- .../common/AbstractDeliveryAction.java | 27 +++++++ .../cook/actions/DeliveryOrderInfo.java | 31 ++++++++ .../DeliveryOrdersToCook.java | 64 +++++++++++++++ .../driver/actions/OrderDeliveredInfo.java | 24 ++++++ .../messageListeners/PendingDeliveries.java | 64 +++++++++++++++ .../sbc/valesriegler/group/DeliveryGroup.java | 78 +++++++++++++------ .../group/actions/DeliveryOrderRequest.java | 24 ++++++ .../group/jms/JMSGroupConnector.java | 8 +- .../jms/messageListeners/WantADelivery.java | 38 ++------- 9 files changed, 299 insertions(+), 59 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderRequest.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java new file mode 100644 index 0000000..0764975 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/AbstractDeliveryAction.java @@ -0,0 +1,27 @@ +package at.ac.tuwien.sbc.valesriegler.common; + +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; + +/** + * Abstract delivery action that can be performed in the DS. In almost all cases the + * DeliveryGroupData is relevant, it may be null in the few corner cases. + * + * @author jan + * + */ +public abstract class AbstractDeliveryAction extends AbstractAction { + private final DeliveryGroupData groupData; + + public AbstractDeliveryAction(DeliveryGroupData groupData) { + super(null); + this.groupData = groupData; + } + + public DeliveryGroupData getGroupData() { + return groupData; + } + + public DeliveryGroupData getDeliveryGroupData() { + return groupData; + } +} \ No newline at end of file diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java new file mode 100644 index 0000000..71e75e6 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/actions/DeliveryOrderInfo.java @@ -0,0 +1,31 @@ +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.GroupData; + +/** + * response to the group's interest in pizza. + * + * @author jan + * + */ +public class DeliveryOrderInfo extends AbstractAction implements Serializable { + private final int cookId; + + public DeliveryOrderInfo(GroupData groupdata, int cookId) { + super(groupdata); + this.cookId = cookId; + } + + public int getCookId() { + return cookId; + } + + @Override + public String toString() { + return "OrderInfo [cookId=" + cookId + "]"; + } + +} 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 new file mode 100644 index 0000000..049add1 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/DeliveryOrdersToCook.java @@ -0,0 +1,64 @@ +package at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.ObjectMessage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; + +/** + * Cook the requested pizza. + * + * @author jan + * + */ +public class DeliveryOrdersToCook implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(DeliveryOrdersToCook.class); + private final JMSCook cook; + + public DeliveryOrdersToCook(JMSCook cook) { + this.cook = cook; + } + + @Override + public void onMessage(Message msg) { + try { + synchronized (cook) { + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof DeliveryOrderRequest) { + DeliveryOrderRequest dor = (DeliveryOrderRequest) obj; + log.debug("Received: " + dor); + + // generate random delay + Thread.sleep((long) (Math.random() * 10000)); + + // TODO: + + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + msg.acknowledge(); + } + } 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/driver/actions/OrderDeliveredInfo.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java new file mode 100644 index 0000000..539ec5e --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/actions/OrderDeliveredInfo.java @@ -0,0 +1,24 @@ +package at.ac.tuwien.sbc.valesriegler.driver.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; + +/** + * response to the group's interest in pizza. + * + * @author jan + * + */ +public class OrderDeliveredInfo extends AbstractAction implements Serializable { + private final int cookId; + + public OrderDeliveredInfo(GroupData groupdata, int cookId) { + super(groupdata); + this.cookId = cookId; + } + + // TODO + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java new file mode 100644 index 0000000..75f0308 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/driver/jms/messageListeners/PendingDeliveries.java @@ -0,0 +1,64 @@ +package at.ac.tuwien.sbc.valesriegler.driver.jms.messageListeners; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.ObjectMessage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.driver.jms.JMSDriver; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; + +/** + * + * + * @author jan + * + */ +public class PendingDeliveries implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(PendingDeliveries.class); + private final JMSDriver driver; + + public PendingDeliveries(JMSDriver driver) { + this.driver = driver; + } + + @Override + public void onMessage(Message msg) { + try { + synchronized (driver) { + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof DeliveryOrderRequest) { + DeliveryOrderRequest dor = (DeliveryOrderRequest) obj; + log.debug("Received: " + dor); + + // generate random delay + Thread.sleep((long) (Math.random() * 10000)); + + // TODO: + + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + msg.acknowledge(); + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } catch (InterruptedException e) { + log.error("EXCEPTION!", e); + } + } + + @Override + public String toString() { + return "PendingDeliveries [driver=" + driver + "]"; + } +} 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 2b78a76..9832e2f 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,40 +1,68 @@ 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 org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class DeliveryGroup implements Runnable, HasId { - private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); - private static int idNext = 0; + private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); + private static int idNext = 0; + + private DeliveryGroupData deliveryGroupData; - private DeliveryGroupData deliveryGroupData; + public DeliveryGroup() { + deliveryGroupData = new DeliveryGroupData(++idNext); + } - public DeliveryGroup() { - this.deliveryGroupData = new DeliveryGroupData(++idNext); - } + public DeliveryGroup(int id) { + deliveryGroupData = new DeliveryGroupData(id); + } - public DeliveryGroup(int id) { - this.deliveryGroupData = new DeliveryGroupData(id); - } + public DeliveryGroupData getDeliveryGroupData() { + return deliveryGroupData; + } - public DeliveryGroupData getDeliveryGroupData() { - return deliveryGroupData; - } + @Override + public int getId() { + return deliveryGroupData.getId(); + } - @Override - public int getId() { - return deliveryGroupData.getId(); - } + public void setDeliveryGroupData(DeliveryGroupData deliveryGroupData) { + this.deliveryGroupData = deliveryGroupData; + } - public void setDeliveryGroupData(DeliveryGroupData deliveryGroupData) { - this.deliveryGroupData = deliveryGroupData; - } + @Override + public void run() { + log.debug("Thread started for:" + this); + try { + Thread.sleep((long) (Math.random() * 10000)); + switch (deliveryGroupData.getDeliveryStatus()) { + case START : + JMSGroupConnector conn = JMSGroupConnector.getConnector(deliveryGroupData.getPizzeriaId()); + conn.send(new DeliveryOrderRequest(deliveryGroupData)); + break; + case ORDERED : + break; + case IN_PROGRESS :// TODO + break; + case DELIVERED : + break; + case PAID : + break; + case IS_DELIVERED : + break; + case DELIVERY_FAILED : + break; + default : + break; + } - @Override - public void run() { - //@jan you can use that like in at.ac.tuwien.sbc.valesriegler.group.Group if you need to... - } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderRequest.java new file mode 100644 index 0000000..5779166 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/DeliveryOrderRequest.java @@ -0,0 +1,24 @@ +package at.ac.tuwien.sbc.valesriegler.group.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.common.AbstractDeliveryAction; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; + +/** + * This is the phonecall to a pizzeria. + * + * @author jan + * + */ +public class DeliveryOrderRequest extends AbstractDeliveryAction implements Serializable { + public DeliveryOrderRequest(DeliveryGroupData deliveryGroupData) { + super(deliveryGroupData); + } + + @Override + public String toString() { + return "DeliveryOrderRequest [getDeliveryGroupData()=" + getDeliveryGroupData() + "]"; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java index 360b8b1..82dff0c 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java @@ -20,6 +20,7 @@ import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; import at.ac.tuwien.sbc.valesriegler.group.AbstractGroupConnector; import at.ac.tuwien.sbc.valesriegler.group.Group; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; import at.ac.tuwien.sbc.valesriegler.group.actions.PayRequest; @@ -147,15 +148,20 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message informPizzariaGui.send(session.createObjectMessage(request)); session.close(); return; + + } else if (request instanceof DeliveryOrderRequest) { + // TODO: + return; } } catch (JMSException e) { log.error("EXCEPTION!", e); } } - public static HashMap getConnectors() { + public static HashMap getConnectors() { return connectors; } + public static JMSGroupConnector getConnector(String key) { return connectors.get(key); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java index 92ae50d..86ffdd0 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantADelivery.java @@ -1,20 +1,14 @@ package at.ac.tuwien.sbc.valesriegler.waiter.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.group.actions.OrderRequest; -import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; +import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; /** @@ -40,37 +34,15 @@ public class WantADelivery implements MessageListener { ObjectMessage objMsg = (ObjectMessage) msg; Object obj = objMsg.getObject(); - if (obj instanceof OrderRequest) { - OrderRequest orderrequest = (OrderRequest) obj; - log.debug("Received: " + orderrequest); + if (obj instanceof DeliveryOrderRequest) { + DeliveryOrderRequest dor = (DeliveryOrderRequest) obj; + log.debug("Received: " + dor); // generate random delay Thread.sleep((long) (Math.random() * 10000)); - ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(waiter.getCONNECTSTRING()); - Connection connection = connectionFactory.createConnection(); - connection.start(); - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - OrderResponse or = new OrderResponse(orderrequest.getGroupdata(), waiter.getId()); + // TODO - // Make cooks do their work - MessageProducer prodOP = session.createProducer(session.createQueue("OrdersToCook")); - prodOP.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - // TODO: hack - orderrequest.getGroupdata().setOrderWaiter(waiter.getId()); - prodOP.send(session.createObjectMessage(orderrequest)); - - // inform Group + Pizzeria - MessageProducer informGroup = session.createProducer(session.createQueue("GroupConnector")); - informGroup.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - 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 { log.warn("Received unknown Object: " + obj); } -- 2.43.0