From 26df8693db78522d877eef1621ff59311b8834ac Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 6 May 2013 01:27:40 +0200 Subject: [PATCH] the whole pizzeria workflow. EXCEPT cooking the pizzas. --- queues | 7 +- .../sbc/valesriegler/DEBUG_CreateTable.java | 2 +- .../valesriegler/cook/actions/PizzaInfo.java | 5 ++ .../messageListeners/CookRequestedPizza.java | 5 ++ .../tuwien/sbc/valesriegler/group/Group.java | 26 ++++--- .../group/actions/OrderRequest.java | 5 ++ .../group/actions/OrderResponse.java | 5 ++ .../group/actions/PayRequest.java | 17 +++++ .../group/actions/PayResponse.java | 24 +++++++ .../valesriegler/group/actions/TableFree.java | 17 +++++ .../group/actions/TableRequest.java | 5 ++ .../group/actions/TableResponse.java | 5 ++ .../group/jms/JMSGroupConnector.java | 48 +++++++++++-- .../sbc/valesriegler/types/GroupData.java | 10 +++ .../tuwien/sbc/valesriegler/types/Pizza.java | 2 +- .../sbc/valesriegler/waiter/Waiter.java | 6 ++ .../jms/messageListeners/WantToPay.java | 67 +++++++++++++++++++ 17 files changed, 237 insertions(+), 19 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/PayRequest.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/PayResponse.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableFree.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java diff --git a/queues b/queues index 3f463b9..511d112 100644 --- a/queues +++ b/queues @@ -23,7 +23,12 @@ Liste aller Messagequeues: OrderedPizzas Pizzen, die von Köchen gekocht werden sollen. - + + WantToPay + Sobald eine Gruppe fertig gegessen hat, will sie zahlen. + + WantToLeave + Alle, die gehen wollen, bzw gegangen sind. Nichts liest aus dieser Queue. ---------------------- Wartende Gruppen diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/DEBUG_CreateTable.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEBUG_CreateTable.java index 6e0feae..bfb941b 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/DEBUG_CreateTable.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEBUG_CreateTable.java @@ -39,7 +39,7 @@ public class DEBUG_CreateTable { tableprod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // Create Tables - for (int i = 0; i < 3; i++) { + for (int i = 0; i < 2; i++) { tableprod.send(session.createObjectMessage(new Table(i))); } 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 index 6248827..d8a5284 100644 --- 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 @@ -29,4 +29,9 @@ public class PizzaInfo extends AbstractAction implements Serializable { 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/CookRequestedPizza.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookRequestedPizza.java index a689b61..a6babf1 100644 --- 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 @@ -71,4 +71,9 @@ public class CookRequestedPizza implements MessageListener { 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 2642063..94adccc 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 @@ -4,6 +4,8 @@ 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.PayRequest; +import at.ac.tuwien.sbc.valesriegler.group.actions.TableFree; 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; @@ -46,17 +48,12 @@ public class Group implements Runnable { break; // do nothing. case ORDERED: break; // do nothing. - case EATING:// just wait some more and request bill. - // MessageProducer wantToPay = - // session.createProducer(session.createQueue("WantToPay")); - // wantToPay.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - // wantToPay.send(session.createObjectMessage(this)); + case EATING: // request bill. + GroupAgent.getInstance().getGroupcomm().send(new PayRequest(groupData)); break; case PAY:// leave table - // MessageProducer wantToLeave = - // session.createProducer(session.createQueue("LeftPizzaria")); - // wantToLeave.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - // wantToLeave.send(session.createObjectMessage(this)); + GroupAgent.getInstance().getGroupcomm().send(new TableFree(groupData)); + groupData.setState(GroupState.GONE); break; case GONE: break; // do nothing. @@ -95,6 +92,7 @@ public class Group implements Runnable { // 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.setOrderWaiter(waiter); new Thread(this).start(); log.debug("orderReceived(): " + this); @@ -104,8 +102,16 @@ public class Group implements Runnable { public void deliverPizzas(Order order, int waiter) { groupData.setState(GroupState.EATING); groupData.setOrder(order); - groupData.setPizzaWaiter(waiter); + groupData.setServingWaiter(waiter); new Thread(this).start(); log.debug("bringOrderedPizzas(): " + this); } + + // pay for the pizzas and leave + public void payForPizzas(int waiter) { + groupData.setState(GroupState.PAY); + groupData.setPayingWaiter(waiter); + new Thread(this).start(); + log.debug("payForPizzas(): " + this); + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderRequest.java index c5f9f21..532c604 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderRequest.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderRequest.java @@ -9,4 +9,9 @@ public class OrderRequest extends AbstractAction implements Serializable { super(groupdata); } + @Override + public String toString() { + return "OrderRequest []"; + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderResponse.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderResponse.java index 137c6f5..0a828af 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderResponse.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderResponse.java @@ -16,4 +16,9 @@ public class OrderResponse extends AbstractAction implements Serializable { return waiterId; } + @Override + public String toString() { + return "OrderResponse [waiterId=" + waiterId + "]"; + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/PayRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/PayRequest.java new file mode 100644 index 0000000..978db73 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/PayRequest.java @@ -0,0 +1,17 @@ +package at.ac.tuwien.sbc.valesriegler.group.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.types.GroupData; + +public class PayRequest extends AbstractAction implements Serializable { + public PayRequest(GroupData groupdata) { + super(groupdata); + } + + @Override + public String toString() { + return "PayRequest []"; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/PayResponse.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/PayResponse.java new file mode 100644 index 0000000..40e4534 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/PayResponse.java @@ -0,0 +1,24 @@ +package at.ac.tuwien.sbc.valesriegler.group.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.types.GroupData; + +public class PayResponse extends AbstractAction implements Serializable { + private final int waiterId; + + public PayResponse(GroupData groupdata, int waiterId) { + super(groupdata); + this.waiterId = waiterId; + } + + public int getWaiterId() { + return waiterId; + } + + @Override + public String toString() { + return "PayResponse [waiterId=" + waiterId + "]"; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableFree.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableFree.java new file mode 100644 index 0000000..3e3944e --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableFree.java @@ -0,0 +1,17 @@ +package at.ac.tuwien.sbc.valesriegler.group.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.types.GroupData; + +public class TableFree extends AbstractAction implements Serializable { + public TableFree(GroupData groupdata) { + super(groupdata); + } + + @Override + public String toString() { + return "TableFree []"; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableRequest.java index 10ac6a7..03195c6 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableRequest.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableRequest.java @@ -9,4 +9,9 @@ public class TableRequest extends AbstractAction implements Serializable { super(groupdata); } + @Override + public String toString() { + return "TableRequest []"; + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableResponse.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableResponse.java index e7a73a0..c665274 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableResponse.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableResponse.java @@ -23,4 +23,9 @@ public class TableResponse extends AbstractAction implements Serializable { return waiterId; } + @Override + public String toString() { + return "TableResponse [table=" + table + ", waiterId=" + waiterId + "]"; + } + } 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 ab6c479..17f359e 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,9 @@ import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.group.actions.AbstractAction; 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; +import at.ac.tuwien.sbc.valesriegler.group.actions.PayResponse; +import at.ac.tuwien.sbc.valesriegler.group.actions.TableFree; import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse; @@ -67,6 +70,12 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(orderresponse.getGroupdata().getId()); g.orderReceived(orderresponse.getWaiterId()); return; + } else if (obj instanceof PayResponse) { + PayResponse payresponse = (PayResponse) obj; + log.debug("Received: " + payresponse); + Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(payresponse.getGroupdata().getId()); + g.payForPizzas(payresponse.getWaiterId()); + return; } } @@ -85,22 +94,49 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToSitAtTable")); wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); wantToSitAtTable.send(session.createObjectMessage(request)); - session.close(); - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); informPizzariaGui.send(session.createObjectMessage(request)); session.close(); return; + } else if (request instanceof OrderRequest) { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToOrder")); - wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - wantToSitAtTable.send(session.createObjectMessage(request)); + MessageProducer wantToOrder = session.createProducer(session.createQueue("WantToOrder")); + wantToOrder.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToOrder.send(session.createObjectMessage(request)); + + MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzariaGui.send(session.createObjectMessage(request)); session.close(); + return; + + } else if (request instanceof PayRequest) { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer wantToPay = session.createProducer(session.createQueue("WantToPay")); + wantToPay.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToPay.send(session.createObjectMessage(request)); + + MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzariaGui.send(session.createObjectMessage(request)); + session.close(); + return; + + } else if (request instanceof TableFree) { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("TablesFree")); + wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToSitAtTable.send(session.createObjectMessage(request.getGroupdata().getTable())); + + request.getGroupdata().setTable(null); + + MessageProducer wantToLeave = session.createProducer(session.createQueue("WantToLeave")); + wantToLeave.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToLeave.send(session.createObjectMessage(request)); - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); informPizzariaGui.send(session.createObjectMessage(request)); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java index 3bb874b..e8082e4 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java @@ -22,7 +22,9 @@ public class GroupData implements Serializable { private int size; private Table table; + private int tableWaiter; + private Order order; private int orderWaiter; private int servingWaiter; @@ -47,6 +49,10 @@ public class GroupData implements Serializable { return orderWaiter; } + public int getPayingWaiter() { + return payingWaiter; + } + @Deprecated public List getPizzas() { return pizzas; @@ -86,6 +92,10 @@ public class GroupData implements Serializable { this.orderWaiter = orderWaiter; } + public void setPayingWaiter(int payingWaiter) { + this.payingWaiter = payingWaiter; + } + @Deprecated public void setPizzas(List pizzas) { this.pizzas = pizzas; 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 a7b8ad1..feb448d 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 @@ -25,7 +25,7 @@ public class Pizza implements Serializable { @Override public String toString() { - return "Pizza [type=" + type + ", id=" + id + ", cookId=" + cookId + ", deliveryAgent=" + deliveryAgent + "]"; + return "Pizza [id=" + id + ", type=" + type + ", cookId=" + cookId + ", deliveryAgent=" + deliveryAgent + "]"; } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java index 90788cb..95b961e 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java @@ -11,6 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToOrder; +import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToPay; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToSitAtTable; //TODO: needs a WaiterAgent and abstraction to support mzs + jms! @@ -41,6 +42,11 @@ public class Waiter implements Serializable { Session sessWantToOrder = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consWantToOrder = sessWantToOrder.createConsumer(sessWantToOrder.createQueue("WantToOrder")); consWantToOrder.setMessageListener(new WantToOrder(this)); + + Session sessWantToPay = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consWantToPay = sessWantToPay.createConsumer(sessWantToPay.createQueue("WantToPay")); + consWantToPay.setMessageListener(new WantToPay(this)); + } catch (Exception e) { log.error("Caught: ", e); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java new file mode 100644 index 0000000..e263b0a --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java @@ -0,0 +1,67 @@ +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.PayRequest; +import at.ac.tuwien.sbc.valesriegler.group.actions.PayResponse; +import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; + +public class WantToPay implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(WantToPay.class); + private final Waiter waiter; + + public WantToPay(Waiter waiter) { + this.waiter = waiter; + } + + @Override + public void onMessage(Message msg) { + try { + msg.acknowledge(); + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof PayRequest) { + PayRequest payrequest = (PayRequest) obj; + log.debug("Received: " + payrequest); + + // generate random delay + Thread.sleep((long) (Math.random() * 10000)); + + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + MessageProducer informGroup = session.createProducer(session.createQueue("GroupConnector")); + informGroup.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + PayResponse pr = new PayResponse(payrequest.getGroupdata(), waiter.getId()); + informGroup.send(session.createObjectMessage(pr)); + + 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); + } + } +} -- 2.43.0