From 692c49d943a34c6d69ca9b3bd92e102e05414f45 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Thu, 9 May 2013 22:00:56 +0200 Subject: [PATCH] Space cooks prepare pizzas and Pizzeria GUI shows that --- .../tuwien/sbc/valesriegler/common/Util.java | 24 ++++- .../tuwien/sbc/valesriegler/cook/JMSCook.java | 3 +- .../messageListeners/CookRequestedPizza.java | 2 +- .../tuwien/sbc/valesriegler/group/Group.java | 2 +- .../sbc/valesriegler/group/GroupAgent.java | 6 +- .../valesriegler/pizzeria/PizzeriaAgent.java | 10 +- .../gui/tablemodels/OrdersOverviewModel.java | 55 ++++++++++- .../gui/tablemodels/PizzasOfOrderModel.java | 12 +-- .../tuwien/sbc/valesriegler/types/Order.java | 15 ++- .../tuwien/sbc/valesriegler/types/Pizza.java | 22 +++-- .../sbc/valesriegler/types/PizzaOrder.java | 38 ++++---- .../sbc/valesriegler/types/PizzaType.java | 4 +- .../xvsm/AbstractXVSMConnector.java | 5 + .../sbc/valesriegler/xvsm/CookXVSM.java | 92 ++++++++++++------- .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 56 +++++++++++ .../sbc/valesriegler/xvsm/GroupXVSM.java | 50 +--------- ...zzeriaXVSM.java => PizzeriaAgentXVSM.java} | 47 ++++++++-- .../sbc/valesriegler/xvsm/WaiterXVSM.java | 8 +- 18 files changed, 308 insertions(+), 143 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java rename src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/{PizzeriaXVSM.java => PizzeriaAgentXVSM.java} (65%) 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 948d845..b33a264 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,7 +1,10 @@ package at.ac.tuwien.sbc.valesriegler.common; import java.net.URI; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.mozartspaces.capi3.Coordinator; import org.mozartspaces.core.Capi; @@ -9,8 +12,14 @@ import org.mozartspaces.core.ContainerReference; import org.mozartspaces.core.MzsConstants.Container; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCoreException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.xvsm.AbstractXVSMConnector; 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; @@ -21,6 +30,7 @@ public abstract class Util { public static final String ORDER = "order"; public static final String DELIVER_PIZZAS = "deliverPizzas"; public static final String PREPARE_PIZZAS = "preparePizzas"; + public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress"; public static final String PAYMENT = "payment"; public static final String FREE_TABLES = "freeTables"; @@ -39,11 +49,23 @@ public abstract class Util { } public static String getId(int id) { - return id != 0 ? String.valueOf(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) { + return Collections.emptyMap(); + } + + Map myMap = new HashMap<>(); + for (T hasId : hasIds) { + myMap.put(hasId.getId(), hasId); + } + return myMap; + } } 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 6e68569..682916b 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 @@ -9,6 +9,7 @@ import org.apache.activemq.ActiveMQConnectionFactory; 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; /** @@ -18,7 +19,7 @@ import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.CookRequestedPizz * @author jan * */ -public class JMSCook { +public class JMSCook implements HasId { private static final Logger log = LoggerFactory.getLogger(JMSCook.class); private static int nextID = 0; final private int id; 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 340486c..72b7663 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 @@ -56,7 +56,7 @@ public class CookRequestedPizza implements MessageListener { 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))); + prodCP.send(session.createObjectMessage(Pizza.createPizzaFromPizzaOrder(pizzaorder, cook.getId()))); MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("PizzeriaConnector")); wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 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 d078c82..81ca81c 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 @@ -23,7 +23,7 @@ import at.ac.tuwien.sbc.valesriegler.types.Table; */ public class Group implements Runnable, HasId { private static final Logger log = LoggerFactory.getLogger(Group.class); - private static int idNext = -1; // TODO: set to 0 after debugging! + private static int idNext = 0; // TODO: set to 0 after debugging! private GroupData groupData; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java index 6ae4181..5125a33 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java @@ -19,7 +19,7 @@ import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel; import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.xvsm.AbstractXVSMConnector; -import at.ac.tuwien.sbc.valesriegler.xvsm.GroupXVSM; +import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM; import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; /** @@ -36,7 +36,7 @@ public class GroupAgent { private static final Logger log = LoggerFactory.getLogger(GroupAgent.class); private GroupOverviewModel groupModel; private AbstractGroupConnector groupconn; - private GroupXVSM xvsm; + private GroupAgentXVSM xvsm; public static GroupAgent groupAgent; @@ -54,7 +54,7 @@ public class GroupAgent { groupconn.init(); } else { - xvsm = new GroupXVSM(); + xvsm = new GroupAgentXVSM(); xvsm.listenForGroupDataChanges(); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java index f6be717..8551062 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java @@ -22,7 +22,7 @@ import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.TablesOverviewMode import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.WaitersOfOrderModel; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.Table; -import at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaXVSM; +import at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaAgentXVSM; /** * The Main class of the Pizzeria compoment. @@ -45,7 +45,7 @@ public class PizzeriaAgent { private WaitersOfOrderModel waitersModel; private PizzasOfOrderModel pizzasModel; - private PizzeriaXVSM xvsm; + private PizzeriaAgentXVSM xvsm; public static void main(String[] args) {// TODO: remove hardcoding of JMS. // if (args.length != 1) { @@ -78,10 +78,12 @@ public class PizzeriaAgent { } private void initXVSM() { - xvsm = new PizzeriaXVSM(); + xvsm = new PizzeriaAgentXVSM(); xvsm.listenForTables(); xvsm.listenForGroups(); + xvsm.listenForOrders(); + xvsm.listenForPizzasInPreparation(); } private void initGUI() { @@ -101,8 +103,6 @@ public class PizzeriaAgent { xvsm.sendFreeTablesToSpace(tables); } } - - }); frame.start(); frame.pack(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java index 96e6729..14d4bb8 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java @@ -1,13 +1,25 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.TableModel; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.Order; +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 at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaAgentXVSM; public class OrdersOverviewModel extends TableModel { + private static final Logger log = LoggerFactory.getLogger(OrdersOverviewModel.class); + private static final String ID = "Order ID"; private static final String TABLE_ID = "Table ID"; private static final String GROUP_ID = "Group ID"; @@ -35,8 +47,22 @@ public class OrdersOverviewModel extends TableModel { } } + public Order getOrderById(int orderId) { + synchronized (items) { + List groups = new ArrayList(items.values()); + for (GroupData groupData : groups) { + if(groupData.getOrder().getId() == orderId) { + return groupData.getOrder(); + } + } + throw new RuntimeException(String.format("Order with orderId %d not found!", orderId)); + } + } + public GroupData getGroupOfRow(int rowIndex) { - return new ArrayList<>(items.values()).get(rowIndex); + synchronized (items) { + return new ArrayList<>(items.values()).get(rowIndex); + } } @Override @@ -44,4 +70,31 @@ public class OrdersOverviewModel extends TableModel { return COLUMNS; } + /** + * Traverse the GroupDatas and search for an order of which the orderId matches the orderId of one of the pizzas. Then set the status and the cookId of the respective pizza. + */ + public void updatePizzasInPreparation(List pizzas) { + synchronized(items) { + for (Pizza pizza : pizzas) { + List groups = new ArrayList(items.values()); + for (GroupData groupData : groups) { + Order order = groupData.getOrder(); + if(order.getId() == pizza.getOrderId()) { + + List orderedPizzas = order.getOrderedPizzas(); + Map orderedPizzasById = Util.intoMapById(orderedPizzas); + PizzaOrder pizzaOrder = orderedPizzasById.get(pizza.getId()); + if(pizzaOrder == null) { + throw new RuntimeException("A pizza which has never been ordered is in preparation... damn!"); + } + pizzaOrder.setStatus(PizzaOrderStatus.IN_PREPARATION); + pizzaOrder.setCookId(pizza.getCookId()); + } + } + } + } + + fireTableDataChanged(); + } } + diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/PizzasOfOrderModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/PizzasOfOrderModel.java index 297e480..2ab74a8 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/PizzasOfOrderModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/PizzasOfOrderModel.java @@ -1,12 +1,15 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; import java.util.List; +import java.util.Map; import at.ac.tuwien.sbc.valesriegler.common.TableModel; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.Order; +import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; -public class PizzasOfOrderModel extends TableModel { +public class PizzasOfOrderModel extends TableModel { private static final String TYPE = "Type"; private static final String STATUS = "Status"; private static final String COOK = "Cook"; @@ -23,12 +26,7 @@ public class PizzasOfOrderModel extends TableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { - if (rowIndex > 0) - return null; - List pizzaOrders = currentOrder.getOrderedPizzas(); - if (pizzaOrders == null) - return null; PizzaOrder pizzaOrder = pizzaOrders.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; @@ -51,7 +49,7 @@ public class PizzasOfOrderModel extends TableModel { @Override public int getRowCount() { - return currentOrder == null ? 0 : 1; + return currentOrder == null ? 0 : currentOrder.getOrderedPizzas().size(); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java index fa01a12..3d84368 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java @@ -1,9 +1,11 @@ package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; +import java.util.ArrayList; import java.util.List; import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.Group; /** @@ -20,7 +22,10 @@ public class Order implements Serializable, HasId { private OrderStatus status; private List orderedPizzas; - private List cookedPizzas; + private List cookedPizzas = new ArrayList<>(); + + // this is necessary so that i can make a xvsm linda selection on it + private Integer numberOfPizzas; public Order(int groupId, List orderedPizzas) { id = ++idNext; @@ -46,6 +51,14 @@ public class Order implements Serializable, HasId { return groupId; } + public int getNumberOfPizzas() { + return Util.getIntSafe(numberOfPizzas); + } + + public void setNumberOfPizzas(Integer numberOfPizzas) { + this.numberOfPizzas = numberOfPizzas; + } + public void setId(Integer id) { this.id = id; } 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 cd5f8b8..bbd1bfd 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,8 @@ package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; -import at.ac.tuwien.sbc.valesriegler.cook.JMSCook; +import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; /** @@ -11,27 +12,28 @@ import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; * @author jan * */ -public class Pizza implements Serializable { - final private int id; - final private PizzaType type; - final public int cookId; +public class Pizza extends PizzaOrder implements Serializable { public Waiter deliveryAgent; // private Person consumer; - private Pizza(int id, PizzaType type, int cookId) { + private Pizza(int id, PizzaType type, int cookId, int orderId) { + super(id); this.id = id; - this.type = type; + this.pizzaType = type; this.cookId = cookId; + this.orderId = orderId; } - public static Pizza createPizzaFromPizzaOrder(PizzaOrder pizzaorder, JMSCook producer) { - return new Pizza(pizzaorder.getId(), pizzaorder.getPizzaType(), producer.getId()); + public static Pizza createPizzaFromPizzaOrder(PizzaOrder pizzaorder, int cookId) { + return new Pizza(pizzaorder.getId(), pizzaorder.getPizzaType(), cookId, pizzaorder.getOrderId()); } + + @Override public String toString() { - return "Pizza [type=" + type + ", id=" + id + ", cookId=" + cookId + ", deliveryAgent=" + deliveryAgent + "]"; + return "Pizza [pizzaType=" + pizzaType + ", id=" + id + ", cookId=" + cookId + ", deliveryAgent=" + deliveryAgent + "]"; } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java index 8c68dde..f12742a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java @@ -3,6 +3,7 @@ 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; /** * Class denoting one Pizza in the future and its state. @@ -12,18 +13,22 @@ import at.ac.tuwien.sbc.valesriegler.common.HasId; */ public class PizzaOrder implements Serializable, HasId { private static int nextID = 0; - private Integer id; + protected Integer id; - private Integer orderId; - private PizzaType pizzaType; - private PizzaOrderStatus status; - private Integer cookId; + protected Integer orderId; + protected PizzaType pizzaType; + protected PizzaOrderStatus status; + protected int cookId; public PizzaOrder(PizzaType pizzaType) { id = ++nextID; this.pizzaType = pizzaType; status = PizzaOrderStatus.NEW; } + + public PizzaOrder(int id) { + this.id = id; + } public PizzaType getPizzaType() { return pizzaType; @@ -37,6 +42,14 @@ public class PizzaOrder implements Serializable, HasId { return id; } + public int getCookId() { + return cookId; + } + + public void setCookId(int cookId) { + this.cookId = cookId; + } + public void setStatus(PizzaOrderStatus status) { this.status = status; } @@ -49,27 +62,20 @@ public class PizzaOrder implements Serializable, HasId { this.pizzaType = pizzaType; } - public Integer getCookId() { - return cookId; - } - public Integer getOrderId() { - return orderId; + + public int getOrderId() { + return Util.getIntSafe(orderId); } public void setOrderId(Integer orderId) { this.orderId = orderId; } - public void setCookId(Integer cookId) { - this.cookId = cookId; - } - @Override public String toString() { return "PizzaOrder [id=" + id + ", orderId=" + orderId + ", pizzaType=" - + pizzaType + ", status=" + status + ", cookId=" + cookId + "]"; + + pizzaType + ", status=" + status + "]"; } - } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaType.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaType.java index 7873349..176fb6b 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaType.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaType.java @@ -12,9 +12,9 @@ public enum PizzaType implements Serializable { MARGHERITA(5, 3), SALAMI(5.5, 7), CARDINALE(6, 5); public final double price; - public final double duration; + public final long duration; - private PizzaType(double price, double duration) { + private PizzaType(double price, long duration) { this.price = price; this.duration = duration; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java index 27886b1..649819c 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java @@ -40,6 +40,7 @@ public abstract class AbstractXVSMConnector { protected ContainerReference deliverPizzasContainer; protected ContainerReference paymentContainer; protected ContainerReference freeTablesContainer; + protected ContainerReference pizzaInProgressContainer; protected Capi capi; protected NotificationManager notificationMgr; @@ -57,6 +58,10 @@ public abstract class AbstractXVSMConnector { handleSpaceErrorAndTerminate(e); } } + + public void usePizzaInProgressContainer() { + pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ; + } public void useTablesContainer() { tablesContainer = useContainer(Util.TABLES_CONTAINER, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))) ; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java index c9c6a3f..c085312 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java @@ -1,15 +1,24 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; import java.io.Serializable; +import java.net.URI; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +import org.mozartspaces.core.MzsConstants.RequestTimeout; +import org.mozartspaces.core.TransactionException; +import org.mozartspaces.core.TransactionReference; import org.mozartspaces.notifications.Notification; import org.mozartspaces.notifications.NotificationListener; import org.mozartspaces.notifications.Operation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; public class CookXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(CookXVSM.class); @@ -24,6 +33,7 @@ public class CookXVSM extends AbstractXVSMConnector { useOrdersContainer(); useDeliverPizzasContainer(); usePreparePizzasContainer(); + usePizzaInProgressContainer(); } public void listenForPizzas() { @@ -35,42 +45,47 @@ public class CookXVSM extends AbstractXVSMConnector { List pizzas = castEntries(entries); for (PizzaOrder pizzaOrder : pizzas) { + log.info("I must prepare this fucking pizza:"); log.info(pizzaOrder.toString()); } -// -// List groups = castEntries(entries); -// Collections.shuffle(groups); -// -// for (GroupData group : groups) { -// try { -// TransactionReference tx = capi.createTransaction(1500, URI.create(Util.SERVER_ADDR)); -// -// try { -// // Acquire a lock for the group in the AssignTableContainer -// String groupNotFound = String.format("Group with id %d was already assigned a table by another waiter!", group.getId()); -// -// GroupData lockedGroup = takeEntityByTemplateFromContainer(new GroupData(group.getId()), assignTableContainer, tx, 1000, groupNotFound); -// // Acquire a lock for one free table in the TablesContainer -// Table lockedFreeTable = takeEntityByTemplateFromContainer(new Table(null), freeTablesContainer, tx, RequestTimeout.DEFAULT, String.format("No free table for group with id %d could be found", group.getId())); -// -// assignGroupToTable(lockedGroup, lockedFreeTable, tx); -// -// } catch (IllegalArgumentException e) { -// log.info("IllegalArgumentException"); -// e.printStackTrace(); -// } catch (EntityNotFoundByTemplate e) { -// log.info(e.getMessage()); -// } catch (Exception e) { -// log.error("AN INNER EXCEPTION"); -// e.printStackTrace(); -// } -// } catch (TransactionException e) { -// log.info("An unimportant TransactionException has occurred"); -// } catch (Exception e) { -// log.error("OUTER EXCEPTION"); -// } -// -// } + Collections.shuffle(pizzas); + for (PizzaOrder pizzaOrder : pizzas) { + try { + try { + TransactionReference tx = capi.createTransaction(9000, URI.create(Util.SERVER_ADDR)); + String pizzaAlreadyCooked = String.format("Pizza with id %d has already been cooked by another cook", pizzaOrder.getId()); + + // Require the lock for preparing the pizza + PizzaOrder order = takeEntityByTemplateFromContainer(new PizzaOrder(pizzaOrder.getId()), preparePizzasContainer, tx, 1000, pizzaAlreadyCooked); + + // tell the space that you prepare the pizza -> without a transaction!! + Pizza pizzaInProgress = Pizza.createPizzaFromPizzaOrder(order, cookId); + pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION); + + log.info("I say that I now prepare this pizza:"); + log.info(pizzaInProgress.toString()); + sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, null); + + PizzaOrder pizza = createPizza(order); + sendItemsToContainer(Arrays.asList(pizza), deliverPizzasContainer, RequestTimeout.DEFAULT, tx); + + capi.commitTransaction(tx); + } catch (IllegalArgumentException e) { + log.info("IllegalArgumentException"); + e.printStackTrace(); + } catch (EntityNotFoundByTemplate e) { + log.info(e.getMessage()); + } catch (Exception e) { + log.error("AN INNER EXCEPTION"); + e.printStackTrace(); + } + } catch (TransactionException e) { + log.info("An unimportant TransactionException has occurred"); + } catch (Exception e) { + log.error("OUTER EXCEPTION"); + } + } + } }; try { @@ -81,4 +96,13 @@ public class CookXVSM extends AbstractXVSMConnector { } } + private PizzaOrder createPizza(PizzaOrder order) throws InterruptedException { + long duration = order.getPizzaType().duration; + Thread.sleep(duration * 1000); + + PizzaOrder pizza = Pizza.createPizzaFromPizzaOrder(order, cookId); + pizza.setStatus(PizzaOrderStatus.DONE); + return pizza; + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java new file mode 100644 index 0000000..0f476d8 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java @@ -0,0 +1,56 @@ +package at.ac.tuwien.sbc.valesriegler.xvsm; + +import java.io.Serializable; +import java.util.List; + +import javax.swing.SwingUtilities; + +import org.mozartspaces.core.MzsCoreException; +import org.mozartspaces.notifications.Notification; +import org.mozartspaces.notifications.NotificationListener; +import org.mozartspaces.notifications.Operation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; + +public class GroupAgentXVSM extends AbstractXVSMConnector { + private static final Logger log = LoggerFactory.getLogger(GroupAgentXVSM.class); + + public GroupAgentXVSM() { + super(); + useGroupsContainer(); + useAssignTableContainer(); + } + + public void listenForGroupDataChanges() { + NotificationListener groupDataListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + + log.info("{} groups have changed", entries.size()); + + final List groups = castEntries(entries); + + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + GroupAgent.getInstance().getGroupModel().addGroupData(groups); + } + }); + } + }; + try { + notificationMgr.createNotification(groupsContainer, groupDataListener, Operation.WRITE); + log.info("Created groupsContainer notification for a waiter"); + } catch (MzsCoreException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java index 634619b..2c92586 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java @@ -1,56 +1,8 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; -import java.io.Serializable; -import java.util.List; - -import javax.swing.SwingUtilities; - -import org.mozartspaces.core.MzsCoreException; -import org.mozartspaces.notifications.Notification; -import org.mozartspaces.notifications.NotificationListener; -import org.mozartspaces.notifications.Operation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; -import at.ac.tuwien.sbc.valesriegler.types.GroupData; - public class GroupXVSM extends AbstractXVSMConnector { - private static final Logger log = LoggerFactory.getLogger(GroupXVSM.class); - - public GroupXVSM() { - super(); - useGroupsContainer(); - useAssignTableContainer(); - } - public void listenForGroupDataChanges() { - NotificationListener groupDataListener = new NotificationListener() { - @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { - - log.info("{} groups have changed", entries.size()); - - final List groups = castEntries(entries); - - SwingUtilities.invokeLater(new Runnable() { - - @Override - public void run() { - GroupAgent.getInstance().getGroupModel().addGroupData(groups); - } - }); - } - }; - try { - notificationMgr.createNotification(groupsContainer, groupDataListener, Operation.WRITE); - log.info("Created groupsContainer notification for a waiter"); - } catch (MzsCoreException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - + public GroupXVSM() { } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java similarity index 65% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaXVSM.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java index 90a9df8..45a6854 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java @@ -6,7 +6,6 @@ import java.util.List; import javax.swing.SwingUtilities; -import org.mozartspaces.core.Entry; import org.mozartspaces.notifications.Notification; import org.mozartspaces.notifications.NotificationListener; import org.mozartspaces.notifications.Operation; @@ -16,16 +15,19 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupState; +import at.ac.tuwien.sbc.valesriegler.types.Pizza; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.Table; -public class PizzeriaXVSM extends AbstractXVSMConnector { - private static final Logger log = LoggerFactory.getLogger(PizzeriaXVSM.class); +public class PizzeriaAgentXVSM extends AbstractXVSMConnector { + private static final Logger log = LoggerFactory.getLogger(PizzeriaAgentXVSM.class); - public PizzeriaXVSM() { + public PizzeriaAgentXVSM() { super(); useTablesContainer(); useGroupsContainer(); useFreeTablesContainer(); + usePizzaInProgressContainer(); } public void listenForTables() { @@ -58,11 +60,11 @@ public class PizzeriaXVSM extends AbstractXVSMConnector { public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { final List groups = castEntries(entries); - log.info("{} group changes!", groups.size()); - for (GroupData groupData : groups) { - log.info(groupData.toString()); - - } +// log.info("{} group changes!", groups.size()); +// for (GroupData groupData : groups) { +// log.info(groupData.toString()); +// +// } SwingUtilities.invokeLater(new Runnable() { @Override @@ -91,5 +93,32 @@ public class PizzeriaXVSM extends AbstractXVSMConnector { public void listenForOrders() { } + public void listenForPizzasInPreparation() { + NotificationListener pizzasInProgress = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + final List pizzas = castEntries(entries); + + log.info("{} pizzas in progress changes!", pizzas.size()); + for (PizzaOrder pizza : pizzas) { + log.info(pizza.toString()); + } + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + PizzeriaAgent.getInstance().getOrdersModel().updatePizzasInPreparation(pizzas); + } + }); + + } + }; + try { + notificationMgr.createNotification(pizzaInProgressContainer, pizzasInProgress, Operation.WRITE); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); + } + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java index 69cb378..66c80cb 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java @@ -197,6 +197,10 @@ public class WaiterXVSM extends AbstractXVSMConnector { handleSpaceErrorAndTerminate(e); } } + + public void listenForPreparedPizzas() { + + } private void assignGroupToTable(GroupData lockedGroup, Table lockedFreeTable, TransactionReference tx) @@ -212,8 +216,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { sendItemsToContainer(Arrays.asList(lockedFreeTable), tablesContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(Arrays.asList(lockedGroup), takeOrderContainer, RequestTimeout.ZERO, tx); - - sendItemsToContainer(Arrays.asList(lockedGroup), groupsContainer, RequestTimeout.ZERO, tx); try { @@ -225,4 +227,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { } + + } -- 2.43.0