From 2286204657e6b47c6e3feb8f2a20ece104ff9743 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Fri, 10 May 2013 17:09:16 +0200 Subject: [PATCH] Some space refactoring. Doing away with unnecessary containers. Auto-Reloading of Pizza and WaiterModels of pizzeriagui when pizza status changes --- .../tuwien/sbc/valesriegler/common/Util.java | 8 +- .../sbc/valesriegler/group/GroupAgent.java | 6 +- .../group/gui/GroupOverviewModel.java | 33 +++++- .../valesriegler/pizzeria/PizzeriaAgent.java | 6 +- .../pizzeria/gui/PizzeriaFrame.java | 11 +- .../gui/tablemodels/GroupsOverviewModel.java | 30 ++--- .../gui/tablemodels/OrdersOverviewModel.java | 14 ++- .../gui/tablemodels/PizzasOfOrderModel.java | 2 +- .../tuwien/sbc/valesriegler/types/Table.java | 6 +- .../xvsm/AbstractXVSMConnector.java | 18 ++- .../sbc/valesriegler/xvsm/CookXVSM.java | 3 +- .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 108 +++++++++++------- .../sbc/valesriegler/xvsm/GroupXVSM.java | 19 +-- .../valesriegler/xvsm/PizzeriaAgentXVSM.java | 88 +++++++++++--- .../sbc/valesriegler/xvsm/WaiterXVSM.java | 31 +++-- .../sbc/valesriegler/xvsm/waiter/Waiter.java | 9 +- 16 files changed, 240 insertions(+), 152 deletions(-) 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 8c5efed..e16ddc4 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 @@ -23,19 +23,19 @@ public abstract class Util { // TODO: solve the switch between mom by command-line arguments public static final boolean useJMS = false; - public static final String TABLES_CONTAINER = "tables"; + 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 = "order"; + 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"; public static final String ORDER_COMPLETE = "orderComplete"; - public static final String PAYMENT = "payment"; + public static final String PAYMENT_REQUEST = "payment"; public static final String FREE_TABLES = "freeTables"; public static final String IS_EATING = "isEating"; - public static final String HAS_PAID = "hasPaid"; + public static final String PAYMENT_DONE = "hasPaid"; public static final String SERVER_ADDR = "xvsm://localhost:9876"; 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 e03c41d..beed127 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 @@ -48,10 +48,12 @@ public class GroupAgent { else { xvsm = new GroupAgentXVSM(); - xvsm.listenForGroupDataChanges(); + xvsm.listenForTableAssigned(); + xvsm.listenForOrdersTaken(); +// xvsm.listenForGroupDataChanges(); xvsm.listenForDeliveredOrders(); xvsm.listenForPaymentRequest(); - xvsm.listenForPayment(); + xvsm.listenForPaymentDone(); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java index e4ca48c..2d3da5c 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java @@ -16,6 +16,7 @@ import at.ac.tuwien.sbc.valesriegler.types.GroupState; import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; +import at.ac.tuwien.sbc.valesriegler.types.Table; /** * Might not the nicest way to store global data, but this is an elemental @@ -120,6 +121,7 @@ public class GroupOverviewModel extends TableModel { * * @param newItems the group data of the corresponding groups */ + @Deprecated public void addGroupData(List newItems) { List groups = new ArrayList<>(); for (GroupData groupData : newItems) { @@ -130,14 +132,18 @@ public class GroupOverviewModel extends TableModel { super.addItems(groups); } - public void updateOrderDone(int groupId) { - if(stateIs(groupId, GroupState.GONE) || stateIs(groupId, GroupState.PAY)) return; + public void setGroupEating(int groupId) { +// if(stateIs(groupId, GroupState.GONE) || stateIs(groupId, GroupState.PAY)) return; changeStateOfGroup(groupId, GroupState.EATING); + + fireTableDataChanged(); } - public void updateGroupPaymentPending(int groupId) { - if(stateIs(groupId, GroupState.GONE)) return; + public void setGroupWantsToPay(int groupId) { +// if(stateIs(groupId, GroupState.GONE)) return; changeStateOfGroup(groupId, GroupState.PAY); + + fireTableDataChanged(); } private boolean stateIs(int groupId, GroupState state) { @@ -145,13 +151,28 @@ public class GroupOverviewModel extends TableModel { } - public void updateGroupHasPaid(int groupId) { - + public void setGroupHasPaid(int groupId) { changeStateOfGroup(groupId, GroupState.GONE); + + fireTableDataChanged(); } private void changeStateOfGroup(int groupId, GroupState state) { items.get(groupId).getGroupData().setState(state); + } + + public void setGroupsSitting(List tables) { + synchronized (items) { + for (Table table : tables) { + changeStateOfGroup(table.getGroupId(), GroupState.SITTING); + } + } + + fireTableDataChanged(); + } + + public void setOrderTaken(GroupData group) { + changeStateOfGroup(group.getId(), GroupState.ORDERED); fireTableDataChanged(); } 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 06973c5..496e31f 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 @@ -80,12 +80,14 @@ public class PizzeriaAgent { private void initXVSM() { xvsm = new PizzeriaAgentXVSM(); - xvsm.listenForTables(); - xvsm.listenForGroups(); + xvsm.listenForFreeTables(); + xvsm.listenForTakenOrders(); + xvsm.listenForWaitingGroups(); xvsm.listenForPizzasInPreparation(); xvsm.listenForDeliveredOrders(); xvsm.listenForPreparedPizzas(); xvsm.listenForPayment(); + xvsm.listenForOccupiedTables(); } private void initGUI() { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java index 53a4bc0..4541afa 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java @@ -139,6 +139,16 @@ public class PizzeriaFrame extends JFrame { pizzasPanel.setVisible(true); } }); + + /* The PizzaModel and WaitersModel and their tables only show the selected entry in the orders table + When the table model of the orders model changes the PizzaModel and WaitersModel have to update, too */ + PizzeriaAgent.getInstance().getOrdersModel().addTableModelListener(new TableModelListener() { + @Override + public void tableChanged(TableModelEvent e) { + PizzeriaAgent.getInstance().getPizzasModel().fireTableDataChanged(); + PizzeriaAgent.getInstance().getWaitersModel().fireTableDataChanged(); + } + }); detailWrapper.add(waitersPanel); detailWrapper.add(pizzasPanel); @@ -180,7 +190,6 @@ public class PizzeriaFrame extends JFrame { numberOfFreeTables); freeTablesLabel.setText(numberOfFreeTables); - } }); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java index b4c1482..594ed32 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java @@ -19,33 +19,11 @@ public class GroupsOverviewModel extends TableModel { return COLUMNS; } - @Override - public int getRowCount() { - List values = new ArrayList<>(items.values()); - Iterator it = values.iterator(); - while (it.hasNext()) { - GroupData groupData = it.next(); - // Show only the waiting groups! - // TODO what is the GroupState.NEW? - if(!(groupData.getState() == GroupState.NEW || groupData.getState() == GroupState.WAITING)) { - it.remove(); - } - } - - return values.size(); - } @Override public Object getValueAt(int rowIndex, int columnIndex) { List values = new ArrayList<>(items.values()); - Iterator it = values.iterator(); - while (it.hasNext()) { - GroupData groupData = it.next(); - // Show only the waiting groups! - if(groupData.getState() == GroupState.NEW || groupData.getState() == GroupState.WAITING) { - it.remove(); - } - } + GroupData group = values.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { @@ -58,4 +36,10 @@ public class GroupsOverviewModel extends TableModel { } } + public void removeGroup(int id) { + items.remove(id); + + fireTableDataChanged(); + } + } 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 e6547e8..ed55d3a 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 @@ -110,7 +110,6 @@ public class OrdersOverviewModel extends TableModel { for (PizzaOrder pizzaOrder : orderedPizzas) { if(pizzaOrder.getId() == pizza.getId()) { pizzaOrder.setStatus(PizzaOrderStatus.DONE); - log.info("Status of pizza was set to DONE"); } } } @@ -127,5 +126,18 @@ public class OrdersOverviewModel extends TableModel { } throw new RuntimeException(String.format("Order with orderId %d not found!", orderId)); } + + + public void updatePaymentWaiter(GroupData group) { + synchronized(items) { + List groups = new ArrayList(items.values()); + for (GroupData groupData : groups) { + if(groupData.getId() == group.getId()) { + groupData.setPayingWaiter(group.getPayingWaiter()); + } + } + } + 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 2ab74a8..161c0e2 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 @@ -36,7 +36,7 @@ public class PizzasOfOrderModel extends TableModel { case STATUS: return pizzaOrder.getStatus(); case COOK: - return pizzaOrder.getCookId(); + return Util.getId(pizzaOrder.getCookId()); default: throw new RuntimeException(UNHANDLEDCOLUMN); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java index a931ea3..7d357f4 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java @@ -47,11 +47,13 @@ public class Table implements Serializable, HasId { return false; } + + @Override public String toString() { - return "Table [id=" + id + "]"; + return "Table [id=" + id + ", groupId=" + groupId + "]"; } - + @Override public int hashCode() { final int prime = 31; 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 89fff67..c385445 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 @@ -33,19 +33,18 @@ import at.ac.tuwien.sbc.valesriegler.types.Table; public abstract class AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class); - protected ContainerReference tablesContainer; - protected ContainerReference groupsContainer; + protected ContainerReference tableAssignedContainer; protected ContainerReference assignTableContainer; protected ContainerReference takeOrderContainer; - protected ContainerReference ordersContainer; + protected ContainerReference orderTakenContainer; protected ContainerReference preparePizzasContainer; protected ContainerReference deliverPizzasContainer; - protected ContainerReference paymentContainer; + protected ContainerReference paymentRequestContainer; protected ContainerReference freeTablesContainer; protected ContainerReference pizzaInProgressContainer; protected ContainerReference orderCompleteContainer; protected ContainerReference isEatingContainer; - protected ContainerReference hasPaidContainer; + protected ContainerReference paymentDoneContainer; protected Capi capi; protected NotificationManager notificationMgr; @@ -149,17 +148,16 @@ public abstract class AbstractXVSMConnector { return newList; } - public void sendTablesToSpace(List
tables) { - sendItemsToContainer(tables, tablesContainer, RequestTimeout.DEFAULT, null); - } +// public void sendTablesToSpace(List
tables) { +// sendItemsToContainer(tables, tableOccupiedContainer, RequestTimeout.DEFAULT, null); +// } public void sendFreeTablesToSpace(List
tables) { sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null); - sendTablesToSpace(tables); } public void sendNewGroupsToSpace(List newGroups) { - sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null); +// sendItemsToContainer(newGroups, groupsContainer, RequestTimeout.DEFAULT, null); sendItemsToContainer(newGroups, assignTableContainer, RequestTimeout.DEFAULT, null); } } \ No newline at end of file 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 3ad7327..76d665f 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 @@ -31,8 +31,7 @@ public class CookXVSM extends AbstractXVSMConnector { super(); this.cookId = id; - groupsContainer = useContainer(Util.GROUPS_CONTAINER) ; - ordersContainer = useContainer(Util.ORDER) ; + orderTakenContainer = useContainer(Util.ORDER_TAKEN) ; deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS); preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS) ; pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS) ; 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 index 3baada2..aa0a435 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java @@ -5,9 +5,6 @@ import java.util.List; import javax.swing.SwingUtilities; -import org.mozartspaces.capi3.AnyCoordinator; -import org.mozartspaces.capi3.LindaCoordinator; -import org.mozartspaces.core.MzsCoreException; import org.mozartspaces.notifications.Notification; import org.mozartspaces.notifications.NotificationListener; import org.mozartspaces.notifications.Operation; @@ -16,21 +13,20 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; -import at.ac.tuwien.sbc.valesriegler.group.SpaceGroup; -import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.types.GroupData; -import at.ac.tuwien.sbc.valesriegler.types.Order; +import at.ac.tuwien.sbc.valesriegler.types.Table; public class GroupAgentXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(GroupAgentXVSM.class); public GroupAgentXVSM() { super(); - groupsContainer = useContainer(Util.GROUPS_CONTAINER) ; assignTableContainer = useContainer(Util.ASSIGN_TABLE) ; orderCompleteContainer = useContainer(Util.ORDER_COMPLETE) ; - paymentContainer = useContainer(Util.PAYMENT) ; - hasPaidContainer = useContainer(Util.HAS_PAID) ; + paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST) ; + paymentDoneContainer = useContainer(Util.PAYMENT_DONE); + tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED); + orderTakenContainer = useContainer(Util.ORDER_TAKEN); } public void listenForDeliveredOrders() { @@ -48,7 +44,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - GroupAgent.getInstance().getGroupModel().updateOrderDone(groupId); + GroupAgent.getInstance().getGroupModel().setGroupEating(groupId); } }); } @@ -60,33 +56,6 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } } - 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 (Exception e) { - handleSpaceErrorAndTerminate(e); - } - - } - public void listenForPaymentRequest() { NotificationListener paymentRequest = new NotificationListener() { @Override @@ -103,20 +72,20 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - GroupAgent.getInstance().getGroupModel().updateGroupPaymentPending(group.getId()); + GroupAgent.getInstance().getGroupModel().setGroupWantsToPay(group.getId()); } }); } }; try { - notificationMgr.createNotification(paymentContainer, paymentRequest, Operation.WRITE); + notificationMgr.createNotification(paymentRequestContainer, paymentRequest, Operation.WRITE); } catch (Exception e) { handleSpaceErrorAndTerminate(e); } } - public void listenForPayment() { + public void listenForPaymentDone() { NotificationListener payment = new NotificationListener() { @Override public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { @@ -132,17 +101,72 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - GroupAgent.getInstance().getGroupModel().updateGroupHasPaid(group.getId()); + GroupAgent.getInstance().getGroupModel().setGroupHasPaid(group.getId()); } }); } }; try { - notificationMgr.createNotification(hasPaidContainer, payment, Operation.WRITE); + notificationMgr.createNotification(paymentDoneContainer, payment, Operation.WRITE); } catch (Exception e) { handleSpaceErrorAndTerminate(e); } } + public void listenForTableAssigned() { + NotificationListener tableAssignmentListener = new NotificationListener() { + + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + log.info("A table was assigned to a group, heureka!"); + + final List
tables = castEntries(entries); + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + GroupAgent.getInstance().getGroupModel().setGroupsSitting(tables); + } + }); + } + }; + try { + notificationMgr.createNotification(tableAssignedContainer, tableAssignmentListener, Operation.WRITE); + log.info("Created tableOccupiedContainer notification for the group agent"); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); + } + } + + public void listenForOrdersTaken() { + NotificationListener orderTakenListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + + + final List groups = castEntries(entries); + + if(groups.size() != 1) { + throw new RuntimeException("A group always orders as a whole!"); + } + final GroupData group = groups.get(0); + + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + GroupAgent.getInstance().getGroupModel().setOrderTaken(group); + } + }); + } + }; + try { + notificationMgr.createNotification(orderTakenContainer, orderTakenListener, Operation.WRITE); + log.info("Created orderTakenContainer notification for the group agent"); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); + } + } + } 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 aa7dacd..3d32ef7 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 @@ -32,7 +32,7 @@ public class GroupXVSM extends AbstractXVSMConnector { super(); this.groupId = groupId; - paymentContainer = useContainer(Util.PAYMENT) ; + paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST) ; orderCompleteContainer = useContainer(Util.ORDER_COMPLETE) ; isEatingContainer = useContainer(Util.IS_EATING) ; freeTablesContainer = useContainer(Util.FREE_TABLES) ; @@ -46,17 +46,10 @@ public class GroupXVSM extends AbstractXVSMConnector { final List groups = castEntries(entries); final Order order = groups.get(0).getOrder(); - - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - - if(order.getGroupId() == groupId) { - eatAndThenPay(); - } - } - }); - + + if(order.getGroupId() == groupId) { + eatAndThenPay(); + } } }; try { @@ -79,7 +72,7 @@ public class GroupXVSM extends AbstractXVSMConnector { e.printStackTrace(); } - sendItemsToContainer(Arrays.asList(groupData), paymentContainer, RequestTimeout.DEFAULT, null); + sendItemsToContainer(Arrays.asList(groupData), paymentRequestContainer, RequestTimeout.DEFAULT, null); log.info("I sent my payment request to the space! GroupId: {}", groupId); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java index 0688823..8945536 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java @@ -33,20 +33,51 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { public PizzeriaAgentXVSM() { super(); - tablesContainer = useContainer(Util.TABLES_CONTAINER) ; - groupsContainer = useContainer(Util.GROUPS_CONTAINER) ; + tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED) ; freeTablesContainer = useContainer(Util.FREE_TABLES); pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS) ; orderCompleteContainer = useContainer(Util.ORDER_COMPLETE) ; deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS); - hasPaidContainer = useContainer(Util.HAS_PAID) ; + paymentDoneContainer = useContainer(Util.PAYMENT_DONE) ; + orderTakenContainer = useContainer(Util.ORDER_TAKEN); + assignTableContainer = useContainer(Util.ASSIGN_TABLE); } - public void listenForTables() { + public void listenForOccupiedTables() { NotificationListener tablesListener = new NotificationListener() { @Override public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { - log.info("Tables Change notified"); + log.info("A new group was assigned to a table!"); + + final List
tables = castEntries(entries); + + if(tables.size() != 1) { + throw new RuntimeException("Only one table can get free at once!"); + } + final Table table = tables.get(0); + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + PizzeriaAgent.getInstance().getTablesModel().addItems(Arrays.asList(table)); + PizzeriaAgent.getInstance().getGroupModel().removeGroup(table.getGroupId()); + } + }); + } + }; + try { + notificationMgr.createNotification(tableAssignedContainer, tablesListener, Operation.WRITE); + log.info("Created tableAssigned notification for pizzeria!"); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); + } + } + + public void listenForFreeTables() { + NotificationListener tablesListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + log.info("A table has become free"); final List
tables = castEntries(entries); @@ -59,13 +90,13 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } }; try { - notificationMgr.createNotification(tablesContainer, tablesListener, Operation.WRITE); + notificationMgr.createNotification(freeTablesContainer, tablesListener, Operation.WRITE); } catch (Exception e) { handleSpaceErrorAndTerminate(e); } } - public void listenForGroups() { + public void listenForWaitingGroups() { NotificationListener groupsListener = new NotificationListener() { @Override @@ -76,21 +107,41 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { @Override public void run() { PizzeriaAgent.getInstance().getGroupModel().addItems(groups); - - List groupsWhoHaveOrdered = new ArrayList<>(); - for (GroupData groupData : groups) { - if(groupData.getState() != GroupState.NEW && groupData.getState() != GroupState.WAITING) { - groupsWhoHaveOrdered.add(groupData); - } - } - PizzeriaAgent.getInstance().getOrdersModel().addItems(groupsWhoHaveOrdered); } }); } }; try { - notificationMgr.createNotification(groupsContainer, groupsListener, Operation.WRITE); + notificationMgr.createNotification(assignTableContainer, groupsListener, Operation.WRITE); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); + } + } + +public void listenForTakenOrders() { + + NotificationListener orderTakenListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + final List groups = castEntries(entries); + + if(groups.size() != 1) { + throw new RuntimeException("Only one order per table!"); + } + final GroupData group = groups.get(0); + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + PizzeriaAgent.getInstance().getOrdersModel().addItems(Arrays.asList(group)); + } + }); + + } + }; + try { + notificationMgr.createNotification(orderTakenContainer, orderTakenListener, Operation.WRITE); } catch (Exception e) { handleSpaceErrorAndTerminate(e); } @@ -193,6 +244,7 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { @Override public void run() { + PizzeriaAgent.getInstance().getOrdersModel().updatePaymentWaiter(group); PizzeriaAgent.getInstance().getTablesModel().makeTableFree(tableId); } }); @@ -203,11 +255,13 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } }; try { - notificationMgr.createNotification(hasPaidContainer, isGoneListener, Operation.WRITE); + notificationMgr.createNotification(paymentDoneContainer, isGoneListener, 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 ff1f7a9..c387ab8 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 @@ -42,15 +42,14 @@ public class WaiterXVSM extends AbstractXVSMConnector { freeTablesContainer = useContainer(Util.FREE_TABLES); assignTableContainer = useContainer(Util.ASSIGN_TABLE) ; - tablesContainer = useContainer(Util.TABLES_CONTAINER) ; takeOrderContainer = useContainer(Util.TAKE_ORDER) ; - ordersContainer = useContainer(Util.ORDER) ; + orderTakenContainer = useContainer(Util.ORDER_TAKEN) ; preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS) ; - groupsContainer = useContainer(Util.GROUPS_CONTAINER) ; orderCompleteContainer = useContainer(Util.ORDER_COMPLETE) ; deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS); - paymentContainer = useContainer(Util.PAYMENT) ; - hasPaidContainer = useContainer(Util.HAS_PAID) ; + paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST) ; + paymentDoneContainer = useContainer(Util.PAYMENT_DONE); + tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED); } public void listenForFreeTable() { @@ -64,7 +63,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { log.info("{} tables have become free", entries.size()); List
tables = castEntries(entries); - Collections.shuffle(tables); + Collections.rotate(tables, waiterId); for (Table table : tables) { try { @@ -135,7 +134,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { log.info("New guest groups have arrived"); List groups = castEntries(entries); - Collections.shuffle(groups); + Collections.rotate(groups, waiterId); for (GroupData group : groups) { try { @@ -215,7 +214,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { // thing! takeMatchingEntity( entity, - paymentContainer, + paymentRequestContainer, tx, RequestTimeout.DEFAULT, String.format( @@ -226,7 +225,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { groupData.setPayingWaiter(waiterId); sendItemsToContainer(Arrays.asList(groupData), - hasPaidContainer, RequestTimeout.ZERO, tx); + paymentDoneContainer, RequestTimeout.ZERO, tx); capi.commitTransaction(tx); @@ -238,7 +237,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { }; try { - notificationMgr.createNotification(paymentContainer, + notificationMgr.createNotification(paymentRequestContainer, paymentListener, Operation.WRITE); log.info("Created payment notification for a waiter"); } catch (Exception e) { @@ -246,7 +245,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { } } - public void listenForOrders() { + public void listenForOrderRequests() { NotificationListener ordersListener = new NotificationListener() { @Override public void entryOperationFinished(final Notification notification, @@ -287,9 +286,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { order.setStatus(OrderStatus.ORDERED); sendItemsToContainer(Arrays.asList(groupData), - ordersContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(Arrays.asList(groupData), - groupsContainer, RequestTimeout.ZERO, tx); + orderTakenContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(order.getOrderedPizzas(), preparePizzasContainer, RequestTimeout.ZERO, tx); capi.commitTransaction(tx); @@ -346,7 +343,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { entity.setOrder(order); GroupData groupData = takeMatchingEntity(entity, - ordersContainer, tx, RequestTimeout.DEFAULT, + orderTakenContainer, tx, RequestTimeout.DEFAULT, "Another waiter just checks if the order is complete"); int groupId = groupData.getId(); int numberOfPizzas = groupData.getOrder() @@ -417,12 +414,10 @@ public class WaiterXVSM extends AbstractXVSMConnector { lockedGroup.setTable(lockedFreeTable); lockedGroup.setTableWaiter(waiterId); - sendItemsToContainer(Arrays.asList(lockedFreeTable), tablesContainer, + sendItemsToContainer(Arrays.asList(lockedFreeTable), tableAssignedContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(Arrays.asList(lockedGroup), takeOrderContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(Arrays.asList(lockedGroup), groupsContainer, - RequestTimeout.ZERO, tx); try { capi.commitTransaction(tx); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java index 77ca842..71a867e 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java @@ -40,7 +40,7 @@ public class Waiter implements Serializable { private void start() { xvsm = new WaiterXVSM(id); - xvsm.listenForOrders(); + xvsm.listenForOrderRequests(); xvsm.listenForPaymentRequest(); @@ -51,13 +51,6 @@ public class Waiter implements Serializable { // when tables get free the waiter should have a look if there are waiting guests and assign the new table to them xvsm.listenForFreeTable(); - - - - - - - } -- 2.43.0