From 498e615121765eb49fc1ac0eb35aac6abc863b9a Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Thu, 6 Jun 2013 11:29:58 +0200 Subject: [PATCH] [XVSM] Remove some containers of the Group Agent by introducing notifications of PIzzeria container changes for the Group Agent --- .../sbc/valesriegler/group/GroupAgent.java | 5 -- .../xvsm/AbstractXVSMConnector.java | 6 -- .../sbc/valesriegler/xvsm/DriverXVSM.java | 3 - .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 64 +++++++++++-------- .../sbc/valesriegler/xvsm/GroupXVSM.java | 2 - .../sbc/valesriegler/xvsm/WaiterXVSM.java | 16 ----- 6 files changed, 38 insertions(+), 58 deletions(-) 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 34f4b09..c912091 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 @@ -69,11 +69,6 @@ public class GroupAgent { xvsm = new GroupAgentXVSM(); xvsm.listenForNewPizzerias(); - xvsm.listenForTableAssigned(); - xvsm.listenForOrdersTaken(); - xvsm.listenForDeliveredOrders(); - xvsm.listenForPaymentRequest(); - xvsm.listenForPaymentDone(); } } 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 8c05433..e4bb0a7 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 @@ -29,27 +29,21 @@ public abstract class AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class); protected ContainerReference tableAssignedContainer; - protected ContainerReference groupAgentTableAssignedContainer; protected ContainerReference assignTableContainer; protected ContainerReference takeOrderContainer; protected ContainerReference orderTakenContainer; protected ContainerReference deliveryOrderTakenContainer; - protected ContainerReference groupAgentOrderTakenContainer; protected ContainerReference preparePizzasContainer; protected ContainerReference deliverPizzasContainer; protected ContainerReference deliverDeliveryPizzasContainer; protected ContainerReference paymentRequestContainer; - protected ContainerReference groupAgentPaymentRequestContainer; protected ContainerReference freeTablesContainer; protected ContainerReference pizzaInProgressContainer; protected ContainerReference orderCompleteContainer; - protected ContainerReference groupAgentOrderCompleteContainer; protected ContainerReference paymentDoneContainer; - protected ContainerReference groupAgentPaymentDoneContainer; protected ContainerReference pizzeriaInfoContainer; protected ContainerReference phoneCallsContainer; protected ContainerReference groupAgentInfoContainer; - protected ContainerReference groupAgentDeliveryOrderTakenContainer; protected Capi capi; protected NotificationManager notificationMgr; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java index 8adc7f9..00bb47a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java @@ -84,9 +84,6 @@ public class DriverXVSM extends AbstractXVSMConnector { sendItemsToContainer(groupsWithCompleteOrder, orderCompleteContainer, MzsConstants.RequestTimeout.DEFAULT, tx); - sendItemsToContainer(groupsWithCompleteOrder, - groupAgentOrderCompleteContainer, MzsConstants.RequestTimeout.DEFAULT, - null); capi.commitTransaction(tx); } else { 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 c22520e..b19a6bb 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 @@ -16,21 +16,17 @@ import javax.swing.*; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import java.util.Set; public class GroupAgentXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(GroupAgentXVSM.class); public GroupAgentXVSM() { super(Util.GROUP_AGENT_PORT); - groupAgentOrderCompleteContainer = useContainer(Util.GROUP_AGENT_ORDER_COMPLETE); - groupAgentPaymentRequestContainer = useContainer(Util.GROUP_AGENT_PAYMENT_REQUEST) ; - groupAgentPaymentDoneContainer = useContainer(Util.GROUP_AGENT_PAYMENT_DONE); - groupAgentTableAssignedContainer = useContainer(Util.GROUP_AGENT_TABLE_ASSIGNED); - groupAgentOrderTakenContainer = useContainer(Util.GROUP_AGENT_ORDER_TAKEN); groupAgentInfoContainer = useContainer(Util.GROUP_AGENT_INFO); } - public void listenForDeliveredOrders() { + public void listenForDeliveredOrders(int port) { SpaceListener deliveredOrders = new SpaceListenerImpl(capi, orderCompleteContainer, false) { @Override @@ -46,11 +42,11 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { }); } }; - createNotification(deliveredOrders, groupAgentOrderCompleteContainer); + createNotification(deliveredOrders, useContainerOfSpaceWithPort(Util.ORDER_COMPLETE, port)); } - public void listenForPaymentRequest() { + public void listenForPaymentRequest(int port) { SpaceListener paymentRequest = new SpaceListenerImpl(capi, paymentRequestContainer, false) { @Override void onEntriesWritten(List entries) throws Exception { @@ -67,10 +63,10 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { }; - createNotification(paymentRequest, groupAgentPaymentRequestContainer); + createNotification(paymentRequest, useContainerOfSpaceWithPort(Util.PAYMENT_REQUEST, port)); } - public void listenForPaymentDone() { + public void listenForPaymentDone(int port) { SpaceListener payment = new SpaceListenerImpl(capi, paymentDoneContainer, false) { @Override void onEntriesWritten(List entries) throws Exception { @@ -86,10 +82,10 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { } }; - createNotification(payment, groupAgentPaymentDoneContainer); + createNotification(payment, useContainerOfSpaceWithPort(Util.PAYMENT_DONE, port)); } - public void listenForTableAssigned() { + public void listenForTableAssigned(int port) { SpaceListener tableAssignmentListener = new SpaceListenerImpl(capi, tableAssignedContainer, false) { @Override @@ -105,11 +101,11 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { }); } }; - createNotification(tableAssignmentListener, groupAgentTableAssignedContainer); + createNotification(tableAssignmentListener, useContainerOfSpaceWithPort(Util.TABLE_ASSIGNED, port)); } - public void listenForOrdersTaken() { + public void listenForOrdersTaken(int port) { SpaceListener tableAssignmentListener = new SpaceListenerImpl(capi, orderTakenContainer, false) { @Override void onEntriesWritten(List entries) throws Exception { @@ -124,7 +120,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { }); } }; - createNotification(tableAssignmentListener, groupAgentOrderTakenContainer); + createNotification(tableAssignmentListener, useContainerOfSpaceWithPort(Util.ORDER_TAKEN, port)); } public void listenForNewPizzerias() { @@ -132,25 +128,41 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { @Override void onEntriesWritten(List entries) throws Exception { + final Set pizzeriaIdentifiers = GroupAgent.getInstance().getPizzeriaIdentifiers(); + + synchronized (pizzeriaIdentifiers) { + final List pizzeriaRegistrations = castEntries(entries); + final List pizzeriaIds = new ArrayList<>(); + for (PizzeriaRegistration registration : pizzeriaRegistrations) { + final String pizzeriaId = String.valueOf(registration.pizzeriaSpacePort); + if(! pizzeriaIdentifiers.contains(pizzeriaId)) { + pizzeriaIds.add(pizzeriaId); + listenToPizzeria(pizzeriaId); + } + } - final List pizzeriaRegistrations = castEntries(entries); - final List pizzeriaIds = new ArrayList<>(); - for (PizzeriaRegistration registration : pizzeriaRegistrations) { - pizzeriaIds.add(String.valueOf(registration.pizzeriaSpacePort)); + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + pizzeriaIdentifiers.addAll(pizzeriaIds); + } + }); } - - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - GroupAgent.getInstance().getPizzeriaIdentifiers().addAll(pizzeriaIds); - } - }); } }; createNotification(newPizzeriaListener, groupAgentInfoContainer); } + private void listenToPizzeria(String pizzeriaId) { + final int port = Integer.parseInt(pizzeriaId); + listenForDeliveredOrders(port); + listenForPaymentDone(port); + listenForPaymentRequest(port); + listenForTableAssigned(port); + listenForOrdersTaken(port); + } + public void sendNewGroupsToSpace(List groupData, int pizzeriaSpacePort) { sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.ASSIGN_TABLE, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); } 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 a2cbdff..e1f6001 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 @@ -28,7 +28,6 @@ public class GroupXVSM extends AbstractXVSMConnector { this.groupId = groupId; paymentRequestContainer = useContainerOfSpaceWithPort(Util.PAYMENT_REQUEST, port) ; - groupAgentPaymentRequestContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_PAYMENT_REQUEST, Util.GROUP_AGENT_PORT); orderCompleteContainer = useContainerOfSpaceWithPort(Util.ORDER_COMPLETE, port) ; // isEatingContainer = useContainer(Util.IS_EATING) ; // freeTablesContainer = useContainer(Util.FREE_TABLES) ; @@ -69,7 +68,6 @@ public class GroupXVSM extends AbstractXVSMConnector { } sendItemsToContainer(Arrays.asList(groupData), paymentRequestContainer, RequestTimeout.DEFAULT, null); - sendItemsToContainer(Arrays.asList(groupData), groupAgentPaymentRequestContainer, 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/WaiterXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java index 27369af..7184884 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 @@ -28,20 +28,14 @@ public class WaiterXVSM extends AbstractXVSMConnector { takeOrderContainer = useContainer(Util.TAKE_ORDER); orderTakenContainer = useContainer(Util.ORDER_TAKEN); deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN); - groupAgentOrderTakenContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_ORDER_TAKEN, Util.GROUP_AGENT_PORT); preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS); orderCompleteContainer = useContainer(Util.ORDER_COMPLETE); - groupAgentOrderCompleteContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_ORDER_COMPLETE, Util.GROUP_AGENT_PORT); deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS); paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST); - groupAgentPaymentRequestContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_PAYMENT_REQUEST, Util.GROUP_AGENT_PORT); paymentDoneContainer = useContainer(Util.PAYMENT_DONE); - groupAgentPaymentDoneContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_PAYMENT_DONE, Util.GROUP_AGENT_PORT); tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED); - groupAgentTableAssignedContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_TABLE_ASSIGNED, Util.GROUP_AGENT_PORT); pizzeriaInfoContainer = useContainer(Util.PIZZERIA_INFO); phoneCallsContainer = useContainer(Util.PHONE_CALLS); - groupAgentDeliveryOrderTakenContainer = useContainerOfSpaceWithPort(Util.GROUP_AGENT_DELIVERY_ORDER_TAKEN, Util.GROUP_AGENT_PORT); } public void listenForPhoneOrders() { @@ -71,8 +65,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { deliveryOrderTakenContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(order.getOrderedPizzas(), preparePizzasContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(groupsWhoHaveOrdered, - groupAgentDeliveryOrderTakenContainer, RequestTimeout.ZERO, null); capi.commitTransaction(tx); log.info("Waite has taken a phone delivery call!"); @@ -206,7 +198,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { final List groupsPayed = Arrays.asList(groupData); sendItemsToContainer(groupsPayed, paymentDoneContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(groupsPayed, groupAgentPaymentDoneContainer, RequestTimeout.ZERO, null); capi.commitTransaction(tx); } catch (Exception e) { @@ -256,8 +247,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { orderTakenContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(order.getOrderedPizzas(), preparePizzasContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(groupsWhoHaveOrdered, - groupAgentOrderTakenContainer, RequestTimeout.ZERO, null); capi.commitTransaction(tx); log.info("Waiter has taken order from group {}", @@ -327,9 +316,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { sendItemsToContainer(groupsWithCompleteOrder, orderCompleteContainer, RequestTimeout.DEFAULT, tx); - sendItemsToContainer(groupsWithCompleteOrder, - groupAgentOrderCompleteContainer, RequestTimeout.DEFAULT, - null); capi.commitTransaction(tx); } else { @@ -367,8 +353,6 @@ public class WaiterXVSM extends AbstractXVSMConnector { tableAssignedContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(Arrays.asList(lockedGroup), takeOrderContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(freeTables, - groupAgentTableAssignedContainer, RequestTimeout.ZERO, null); try { capi.commitTransaction(tx); -- 2.43.0