From 65001a7bbf6ad676834f86298a1e0cfede43bbef Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Mon, 10 Jun 2013 17:18:17 +0200 Subject: [PATCH] [XVSM]Some NPE fixes, GUI fixes --- .../sbc/valesriegler/group/GroupAgent.java | 18 ++-- .../group/gui/DeliveryOverviewModel.java | 27 +----- .../group/gui/GroupOverviewModel.java | 13 ++- .../valesriegler/pizzeria/PizzeriaAgent.java | 22 ++--- .../gui/tablemodels/DeliveryOrdersModel.java | 19 ++++ .../gui/tablemodels/GroupsOverviewModel.java | 19 ++++ .../tuwien/sbc/valesriegler/types/Order.java | 11 +-- .../sbc/valesriegler/xvsm/DriverXVSM.java | 96 ++++++++++--------- .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 10 +- 9 files changed, 128 insertions(+), 107 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 ae4b9ca..a087999 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 @@ -1,16 +1,5 @@ package at.ac.tuwien.sbc.valesriegler.group; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.swing.SwingUtilities; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel; import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest; @@ -22,6 +11,11 @@ import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.*; +import java.util.*; /** * The Main class of the Group component. @@ -67,7 +61,7 @@ public class GroupAgent { } groupAgent = new GroupAgent(); - if (!Util.runSimulation) { + if (! Util.runSimulation) { groupGui = new GroupGUI(); SwingUtilities.invokeLater(groupGui); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java index 53613e0..8bce9d4 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java @@ -53,34 +53,14 @@ public class DeliveryOverviewModel extends TableModel { } } - public void setOrdersTaken(List groups) { - for (DeliveryGroupData group : groups) { - changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED); - } - - fireTableDataChanged(); - } - - private void changeStateOfGroup(int groupId, DeliveryStatus state) { - items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state); - } - - public void setOrdersDelivered(List groups) { - for (DeliveryGroupData group : groups) { - changeStateOfGroup(group.getId(), group.getDeliveryStatus()); - } - - fireTableDataChanged(); - } - public void addDeliveries(List groups) { synchronized (items) { for (DeliveryGroupData group : groups) { final DeliveryGroup deliveryGroup = items.get(group.getId()); if(deliveryGroup==null) { - log.error("Delivery group not found!!!"); + log.error("Delivery group not found: {}", group.getId()); } - deliveryGroup.setDeliveryGroupData(group); + else deliveryGroup.setDeliveryGroupData(group); } } fireTableDataChanged(); @@ -118,6 +98,7 @@ public class DeliveryOverviewModel extends TableModel { } } } - return size == finished; + final boolean result = size == finished; + return result; } } 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 fcdfcc5..a321dff 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 @@ -47,7 +47,18 @@ public class GroupOverviewModel extends TableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { List values = new ArrayList<>(items.values()); - Group group = values.get(rowIndex); + // TODO make sure this is necessary... +// Collections.sort(values, new Comparator() { +// @Override +// public int compare(Group o1, Group o2) { +// final int o1Id = o1.getId(); +// final int o2Id = o2.getId(); +// if(o1Ido2Id) return 1; +// else return 0; +// } +// }); + Group group = values.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { case ID: 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 3e8c4d8..87a416c 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 @@ -1,27 +1,19 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria; -import java.util.List; - -import javax.swing.SwingUtilities; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC; import at.ac.tuwien.sbc.valesriegler.pizzeria.actions.TableNew; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.DeliveryDetailsModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.DeliveryOrdersModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.GroupsOverviewModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.PizzasOfOrderModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.TablesOverviewModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.WaitersOfOrderModel; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.*; import at.ac.tuwien.sbc.valesriegler.pizzeria.jms.JMSPizzeriaConnector; import at.ac.tuwien.sbc.valesriegler.pizzeria.jms.PizzeriaJMSNACMsgListener; import at.ac.tuwien.sbc.valesriegler.types.Table; import at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaAgentXVSM; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.*; +import java.util.List; /** * The Main class of the Pizzeria compoment. @@ -61,7 +53,7 @@ public class PizzeriaAgent { case "XVSM" : pizzeriaAgent.createModels(); pizzeriaAgent.initXVSM(args[1]); - if (!Util.runSimulation) { + if (! Util.runSimulation) { pizzeriaAgent.initGUI(); } Util.useJMS = false; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java index c6bed84..336bfd3 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/DeliveryOrdersModel.java @@ -6,6 +6,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; public class DeliveryOrdersModel extends AbstractOrdersModel { @@ -28,6 +30,23 @@ public class DeliveryOrdersModel extends AbstractOrdersModel @Override public Object getValueAt(int rowIndex, int columnIndex) { List values = new ArrayList<>(items.values()); + // TODO: make sure that is necessary + Collections.sort(values, new Comparator() { + @Override + public int compare(DeliveryGroupData o1, DeliveryGroupData o2) { + final int o1Id = o1.getOrder().getId(); + final int o2Id = o2.getOrder().getId(); + if(o1Id==0) { + if(o2Id>0) return 1; + } + if(o2Id==0) { + if(o1Id>0) return 1; + } + if (o1Id < o2Id) return -1; + else if (o1Id > o2Id) return 1; + else return 0; + } + }); DeliveryGroupData group = values.get(rowIndex); Order order = group.getOrder(); String wantedColumn = COLUMNS[columnIndex]; 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 26b2ef4..142e14f 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 @@ -7,6 +7,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; public class GroupsOverviewModel extends TableModel { @@ -26,6 +28,23 @@ public class GroupsOverviewModel extends TableModel { public Object getValueAt(int rowIndex, int columnIndex) { List values = new ArrayList<>(items.values()); + // TODO: make sure that is necessary + Collections.sort(values, new Comparator() { + @Override + public int compare(GroupData o1, GroupData o2) { + final int o1Id = o1.getOrder().getId(); + final int o2Id = o2.getOrder().getId(); + if (o1Id == 0) { + if (o2Id > 0) return 1; + } + if (o2Id == 0) { + if (o1Id > 0) return 1; + } + if (o1Id < o2Id) return -1; + else if (o1Id > o2Id) return 1; + else return 0; + } + }); GroupData group = values.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { 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 687d84b..a463957 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,14 +1,13 @@ package at.ac.tuwien.sbc.valesriegler.types; +import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import org.mozartspaces.capi3.Queryable; + import java.io.Serializable; import java.util.ArrayList; import java.util.List; -import org.mozartspaces.capi3.Queryable; - -import at.ac.tuwien.sbc.valesriegler.common.HasId; -import at.ac.tuwien.sbc.valesriegler.common.Util; - /** * Object denotes a Group's interest in pizzas and the state of the pizzas * themselves. @@ -79,7 +78,7 @@ public class Order implements Serializable, HasId { @Override public int getId() { - return id; + return Util.getIntSafe(id); } public List getOrderedPizzas() { 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 343b278..349b059 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 @@ -10,6 +10,7 @@ import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction; import org.mozartspaces.core.ContainerReference; import org.mozartspaces.core.MzsConstants; +import org.mozartspaces.core.MzsCoreException; import org.mozartspaces.core.TransactionReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,64 +43,68 @@ public class DriverXVSM extends AbstractXVSMConnector { @Override public void onEntriesWritten(List entries) throws Exception { - final List deliveries = castEntries(entries); + handleDeliveries(entries); - for (DeliveryGroupData delivery : deliveries) { - DeliveryGroupData template = new DeliveryGroupData(delivery.getId()); - template.setDeliveryStatus(DeliveryStatus.ORDERED); + } + }).createSpaceListenerImpl(); + } - final TransactionReference tx = capi.createTransaction( - 7000, - URI.create(String.format(Util.SERVER_ADDR, port))); - final String error = "Another driver wants to deliver this order!"; + private void handleDeliveries(List entries) throws MzsCoreException { + final List deliveries = castEntries(entries); - try { - // Get lock for delivering this delivery order - final DeliveryGroupData deliveryGroup = takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.DEFAULT, error); - final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.INFINITE, "Cannot get the delivery order!"); - group.setDriverId(driverId); - group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS); + for (DeliveryGroupData delivery : deliveries) { + DeliveryGroupData template = new DeliveryGroupData(delivery.getId()); + template.setDeliveryStatus(DeliveryStatus.ORDERED); - final List groups = Arrays.asList(group); -// sendItemsToContainer(groups, -// pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null); + final TransactionReference tx = capi.createTransaction( + 7000, + URI.create(String.format(Util.SERVER_ADDR, port))); + final String error = "Another driver wants to deliver this order!"; - if (!Util.runSimulation) { - Thread.sleep(3000); - } + try { + // Get lock for delivering this delivery order + final DeliveryGroupData deliveryGroup = takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.DEFAULT, error); + final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.INFINITE, "Cannot get the delivery order!"); + group.setDriverId(driverId); + group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS); - final String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT); - ContainerReference cref = null; - boolean success = false; - try { - cref = capi.lookupContainer(group.getAddress(), URI.create(address), MzsConstants.RequestTimeout.ZERO, null); - log.debug("Looked up container {} successfully!", address); - success = true; + final List groups = Arrays.asList(group); +// sendItemsToContainer(groups, +// pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null); - } catch (Exception ex) { - log.info("Could not find container {}", address); - } - if (success) { - group.setDeliveryStatus(DeliveryStatus.DELIVERED); - if (!Util.runSimulation) { - sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.DEFAULT, null); - } - } else { - group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED); - } - sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx); - capi.commitTransaction(tx); + if (!Util.runSimulation) { + Thread.sleep(3000); + } + final String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT); + ContainerReference cref = null; + boolean success = false; + try { + cref = capi.lookupContainer(group.getAddress(), URI.create(address), MzsConstants.RequestTimeout.ZERO, null); + log.debug("Looked up container {} successfully!", address); + success = true; - } catch (Exception e) { - log.info("DRiverXVSM exception"); - log.info(e.getMessage()); - e.printStackTrace(); + } catch (Exception ex) { + log.info("Could not find container {}", address); + } + if (success) { + group.setDeliveryStatus(DeliveryStatus.DELIVERED); + if (!Util.runSimulation) { + sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.DEFAULT, null); } + } else { + group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED); } + sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx); + capi.commitTransaction(tx); + + } catch (Exception e) { + log.info("DRiverXVSM exception"); + log.info(e.getMessage()); + e.printStackTrace(); } - }).createSpaceListenerImpl(); + } } public void listenForPreparedDeliveryPizzas() { @@ -143,6 +148,7 @@ public class DriverXVSM extends AbstractXVSMConnector { tx); capi.commitTransaction(tx); + handleDeliveries(groupsWithCompleteOrder); } else { log.info("Not yet all pizzas prepared! Order with id " + orderId + " has " + numberOfPizzas 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 c2a829f..c42b0a2 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 @@ -83,7 +83,7 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { boolean finished = GroupAgent.getInstance().getDeliveryModel().hasFinished(); if (finished) { final long now = new Date().getTime(); - final long difference = startTime.get() - now; + final long difference = now - startTime.get(); log.info("Finished after {} seconds", difference / 1000); } } @@ -126,20 +126,20 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { List pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA); List pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA); - final String pizzeria1 = "9875"; - final String pizzeria2 = "9874"; + final String pizzeria1 = "9874"; + final String pizzeria2 = "9875"; List groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4); List groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3); List groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2); final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel(); - deliveryModel.addItems(groups1); +// deliveryModel.addItems(groups1); deliveryModel.addItems(groups2); deliveryModel.addItems(groups3); GroupAgent.getInstance().onDeliveryGroupsCreated(groups2); GroupAgent.getInstance().onDeliveryGroupsCreated(groups3); - GroupAgent.getInstance().onDeliveryGroupsCreated(groups1); +// GroupAgent.getInstance().onDeliveryGroupsCreated(groups1); log.info("ATTENTION: Now let's wait 60 seconds!"); -- 2.43.0