From 3ab0eb9acc2cc09f0270100ace60d013ef4cc9c3 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Fri, 7 Jun 2013 17:28:03 +0200 Subject: [PATCH] [XVSM] Solve bug which made the GUI send customers to always the same Pizzeria --- README.md | 1 + .../gui/tablemodels/GroupsOverviewModel.java | 1 - .../xvsm/AbstractXVSMConnector.java | 15 +-- .../sbc/valesriegler/xvsm/CookXVSM.java | 4 +- .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 43 ++------ .../valesriegler/xvsm/PizzeriaAgentXVSM.java | 99 ++----------------- 6 files changed, 24 insertions(+), 139 deletions(-) diff --git a/README.md b/README.md index cbe352f..b57d2b4 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Space Based Computing SS 2013 ### How to start the Space Group GUI mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.group.GroupAgent" -Dexec.args="XVSM" +The Group GUI expects that a Space Server was started on 9876. ### How to start the Space Pizzeria GUI mvn exec:java -Dexec.mainClass="at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent" -Dexec.args="XVSM " 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 1a27ddc..26b2ef4 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 @@ -48,7 +48,6 @@ public class GroupsOverviewModel extends TableModel { synchronized (items) { for (Table table : tables) { final int groupId = table.getGroupId(); - log.info("222222222222222 Add group {} to a table", groupId); items.remove(groupId); } } 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 270c73f..783a56c 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 @@ -16,7 +16,10 @@ import org.slf4j.LoggerFactory; import java.io.Serializable; import java.net.URI; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; import java.util.concurrent.atomic.AtomicLong; public abstract class AbstractXVSMConnector { @@ -54,8 +57,6 @@ public abstract class AbstractXVSMConnector { protected NotificationManager notificationMgr; protected int port; - private HashMap containers = new HashMap(); - public AbstractXVSMConnector(int port) { this.port = port; initSpaceCommunication(port); @@ -79,15 +80,9 @@ public abstract class AbstractXVSMConnector { } protected ContainerReference useContainerOfSpaceWithPort(String containerName, int spacePort) { - String key = containerName + port; - ContainerReference container = containers.get(key); - if(container != null) return container; - try { final String address = String.format(Util.SERVER_ADDR, spacePort); - container = CapiUtil.lookupOrCreateContainer(containerName, URI.create(address), createCoordinators(new FifoCoordinator(), new LindaCoordinator(false)), null, capi); - containers.put(key, container); - return container; + return CapiUtil.lookupOrCreateContainer(containerName, URI.create(address), createCoordinators(new FifoCoordinator(), new LindaCoordinator(false)), null, capi); } catch (MzsCoreException e) { Util.handleSpaceErrorAndTerminate(e); } 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 cc712c8..743158b 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 @@ -37,7 +37,7 @@ public class CookXVSM extends AbstractXVSMConnector { } public void listenForPizzas() { - SpaceListener pizzasListener = getDefaultBuilder().setLookaround(true).setCref(preparePizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setLookaround(true).setCref(preparePizzasContainer).setTimeout(15000).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) @@ -103,7 +103,7 @@ public class CookXVSM extends AbstractXVSMConnector { /** * A cook may prepare a pizza if there is no current delivery order or if at least a pizza of the same order is - * in preparation! + * in preparation or has been done! */ private boolean mayPreparePizza(PizzaOrder pizzaOrder, TransactionReference tx) throws MzsCoreException { List deliveryPizzas = readMatchingEntities(new PizzaOrder(), prepareDeliveryPizzasContainer, tx, RequestTimeout.ZERO, "MAYPREPAREPIZZA: Cannot access prepareDeliveryPizzasContainer"); 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 8230aa7..28c8f94 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 @@ -6,21 +6,16 @@ import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.Table; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; -import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceListener; import org.mozartspaces.core.MzsConstants.RequestTimeout; -import org.mozartspaces.core.MzsCoreException; -import org.mozartspaces.core.TransactionReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; import java.io.Serializable; -import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.Set; -import static java.lang.String.format; import static java.lang.String.valueOf; public class GroupAgentXVSM extends AbstractXVSMConnector { @@ -32,7 +27,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void listenForDeliveredOrders(int port) { - SpaceListener deliveredOrders = getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.ORDER_COMPLETE, port)).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.ORDER_COMPLETE, port)).setSpaceAction(new SpaceAction() { @Override @@ -52,7 +47,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void listenForPaymentRequest(int port) { - SpaceListener paymentRequest = getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.PAYMENT_REQUEST, port)).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.PAYMENT_REQUEST, port)).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final GroupData group = getSingleGroup(entries); @@ -71,7 +66,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void listenForPaymentDone(int port) { - SpaceListener payment = getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.PAYMENT_DONE, port)).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.PAYMENT_DONE, port)).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -89,7 +84,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void listenForTableAssigned(int port) { - SpaceListener tableAssignmentListener = getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.TABLE_ASSIGNED, port)).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.TABLE_ASSIGNED, port)).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -107,7 +102,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void listenForOrdersTaken(int port) { - SpaceListener tableAssignmentListener = getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.ORDER_TAKEN, port)).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(useContainerOfSpaceWithPort(Util.ORDER_TAKEN, port)).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final GroupData group = getSingleGroup(entries); @@ -146,8 +141,6 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); - log.info("DElivered orders!"); - SwingUtilities.invokeLater(new Runnable() { @Override @@ -160,7 +153,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void listenForNewPizzerias() { - SpaceListener newPizzeriaListener = getDefaultBuilder().setCref(groupAgentInfoContainer).setLookaround(true).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(groupAgentInfoContainer).setLookaround(true).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -201,28 +194,12 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } public void sendNewGroupsToSpace(List groupData, int pizzeriaSpacePort) { - try { - TransactionReference tx = capi.createTransaction( - Util.SPACE_TRANSACTION_TIMEOUT, - URI.create(format(Util.SERVER_ADDR, pizzeriaSpacePort))); - sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.ASSIGN_TABLE, pizzeriaSpacePort), RequestTimeout.DEFAULT, tx); - sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PIZZERIA_GROUP, pizzeriaSpacePort), RequestTimeout.DEFAULT, tx); - capi.commitTransaction(tx); - } catch (MzsCoreException e) { - Util.handleSpaceErrorAndTerminate(e); - } + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PIZZERIA_GROUP, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.ASSIGN_TABLE, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); } public void sendNewDeliveriesToSpace(List groupData, int pizzeriaSpacePort) { - try { - TransactionReference tx = capi.createTransaction( - Util.SPACE_TRANSACTION_TIMEOUT, - URI.create(format(Util.SERVER_ADDR, pizzeriaSpacePort))); - sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PHONE_CALLS, pizzeriaSpacePort), RequestTimeout.DEFAULT, tx); - sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PIZZERIA_DELIVERY, pizzeriaSpacePort), RequestTimeout.DEFAULT, tx); - capi.commitTransaction(tx); - } catch (MzsCoreException e) { - Util.handleSpaceErrorAndTerminate(e); - } + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PIZZERIA_DELIVERY, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.PHONE_CALLS, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); } } 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 947b870..42b937d 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 @@ -5,7 +5,6 @@ import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.types.*; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; -import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceListener; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,31 +37,10 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } - public void listenForOccupiedTables() { - SpaceListener tablesListener = getDefaultBuilder().setCref(tableAssignedContainer).setSpaceAction(new SpaceAction() { - @Override - public void onEntriesWritten(List entries) throws Exception { - log.info("A new group was assigned to a table!"); - - final List tables = castEntries(entries); - log.info("occupied tables: " + tables.toString()); - - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getTablesModel().addItems(tables); - PizzeriaAgent.getInstance().getGroupModel().removeGroupsFromTables(tables); - } - }); - } - }).createSpaceListenerImpl(); - } - public void listenForTablesUpdates() { - SpaceListener tablesListener = getDefaultBuilder().setCref(pizzeriaTableContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(pizzeriaTableContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { - log.info("listenForTables!!!!!"); final List
tables = castEntries(entries); @@ -70,7 +48,6 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { for (Table table : tables) { if(! table.isFree()) occupiedTables.add(table); } - log.info("11111111111111111111 Number of occupied Tables: {}", occupiedTables.size()); SwingUtilities.invokeLater(new Runnable() { @Override @@ -84,8 +61,7 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } public void listenForGroupUpdates() { - - SpaceListener groupsListener = getDefaultBuilder().setCref(pizzeriaGroupContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(pizzeriaGroupContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); @@ -128,7 +104,7 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } public void listenForDeliveryUpdates() {//deliveryOrderTakenContainer - SpaceListener orderTakenListener = getDefaultBuilder().setCref(pizzeriaDeliveryContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(pizzeriaDeliveryContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List groups = castEntries(entries); @@ -144,27 +120,8 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } - -// public void listenForDeliveriesInProgress() { -// SpaceListener pizzasInProgress = getDefaultBuilder().setCref(deliveryInProgress).setSpaceAction(new SpaceAction() { -// -// @Override -// public void onEntriesWritten(List entries) throws Exception { -// final List groups = castEntries(entries); -// -// SwingUtilities.invokeLater(new Runnable() { -// @Override -// public void run() { -// PizzeriaAgent.getInstance().getDeliveryOrdersModel().addItems(groups); -// } -// }); -// -// } -// }).createSpaceListenerImpl(); -// } - public void listenForPizzasInPreparation() { - SpaceListener pizzasInProgress = getDefaultBuilder().setCref(pizzaInProgressContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(pizzaInProgressContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { @@ -187,26 +144,9 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } - public void listenForDeliveredOrders() { - SpaceListener deliveredOrders = getDefaultBuilder().setCref(orderDeliveredContainer).setSpaceAction(new SpaceAction() { - @Override - public void onEntriesWritten(List entries) throws Exception { - final List groups = castEntries(entries); - - log.info("{} order was delivered!", groups.size()); - - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getOrdersModel().updateStatusOfOrders(groups); - } - }); - } - }).createSpaceListenerImpl(); - } public void listenForPreparedPizzas() { - SpaceListener preparedPizzasListener = getDefaultBuilder().setCref(preparedPizzasContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(preparedPizzasContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List pizzas = castEntries(entries); @@ -223,7 +163,7 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { } public void listenForPreparedDeliveryPizzas() { - SpaceListener preparedPizzasListener = getDefaultBuilder().setCref(preparedDeliveryPizzasContainer).setSpaceAction(new SpaceAction() { + getDefaultBuilder().setCref(preparedDeliveryPizzasContainer).setSpaceAction(new SpaceAction() { @Override public void onEntriesWritten(List entries) throws Exception { final List pizzas = castEntries(entries); @@ -239,33 +179,6 @@ public class PizzeriaAgentXVSM extends AbstractXVSMConnector { }).createSpaceListenerImpl(); } - - public void listenForPayment() { - SpaceListener isGoneListener = getDefaultBuilder().setCref(paymentDoneContainer).setSpaceAction(new SpaceAction() { - @Override - public void onEntriesWritten(List entries) throws Exception { - final List groups = castEntries(entries); - - final GroupData group = groups.get(0); - log.info("Group {} is gone", group.getId()); - - final int tableId = PizzeriaAgent.getInstance().getTablesModel().getTableIdOfGroup(group); - SwingUtilities.invokeLater(new Runnable() { - - @Override - public void run() { - PizzeriaAgent.getInstance().getOrdersModel().updatePaymentWaiter(group); - PizzeriaAgent.getInstance().getTablesModel().makeTableFree(tableId); - } - }); - - Table table = new Table(tableId); - - sendItemsToContainer(Arrays.asList(table), freeTablesContainer, RequestTimeout.DEFAULT, null); - } - }).createSpaceListenerImpl(); - } - public void sendFreeTablesToContainer(List
tables) { sendItemsToContainer(tables, freeTablesContainer, RequestTimeout.DEFAULT, null); sendItemsToContainer(tables, pizzeriaTableContainer, RequestTimeout.DEFAULT, null); -- 2.43.0