From bdfce63e733b5fee4a22c7349e2a526c76a72dcf Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Mon, 6 May 2013 01:50:20 +0200 Subject: [PATCH] Support for space notifications for pizzeria and group agents. Change table model items type to a hash map. Replace primitive types in space class types to wrapper types. --- pom.xml | 16 ++-- .../sbc/valesriegler/DEP_GroupDataMin.java | 4 +- .../ac/tuwien/sbc/valesriegler/DEP_Order.java | 3 +- .../ac/tuwien/sbc/valesriegler/DEP_Table.java | 10 ++- .../tuwien/sbc/valesriegler/common/HasId.java | 5 ++ .../sbc/valesriegler/common/TableModel.java | 12 +-- .../common/{SpaceUtil.java => Util.java} | 5 +- .../tuwien/sbc/valesriegler/group/Group.java | 8 +- .../sbc/valesriegler/group/GroupAgent.java | 38 +++++--- .../valesriegler/group/gui/GroupFrame.java | 9 +- .../group/gui/GroupOverviewModel.java | 19 ++-- .../valesriegler/pizzeria/PizzeriaAgent.java | 51 +++++++++-- .../valesriegler/pizzeria/gui/DEP_Table.java | 39 --------- .../pizzeria/gui/PizzeriaFrame.java | 68 ++++++++------- .../gui/tablemodels/GroupsOverviewModel.java | 7 +- .../gui/tablemodels/OrdersOverviewModel.java | 6 +- .../gui/tablemodels/TablesOverviewModel.java | 14 ++- .../sbc/valesriegler/types/GroupData.java | 2 + .../tuwien/sbc/valesriegler/types/Order.java | 3 + .../sbc/valesriegler/types/PizzaOrder.java | 4 +- .../tuwien/sbc/valesriegler/types/Table.java | 3 + .../sbc/valesriegler/xvsm/XVSMConnector.java | 86 +++++++++++++++---- .../sbc/valesriegler/xvsm/cook/Cook.java | 4 - 23 files changed, 266 insertions(+), 150 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/common/HasId.java rename src/main/java/at/ac/tuwien/sbc/valesriegler/common/{SpaceUtil.java => Util.java} (88%) delete mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java diff --git a/pom.xml b/pom.xml index fe96bd4..2ad4a6c 100644 --- a/pom.xml +++ b/pom.xml @@ -42,16 +42,18 @@ + + org.mozartspaces + mozartspaces-notifications + 2.2-SNAPSHOT + - + @@ -111,7 +113,7 @@ - + pizzeria @@ -126,7 +128,7 @@ -classpath at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent - + diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_GroupDataMin.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_GroupDataMin.java index c4e3f6b..ecbc960 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_GroupDataMin.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_GroupDataMin.java @@ -2,13 +2,15 @@ package at.ac.tuwien.sbc.valesriegler; import java.io.Serializable; +import at.ac.tuwien.sbc.valesriegler.common.HasId; + /** * A group bean which only has an id and a size. * * @author Gregor Riegler */ @Deprecated -public class DEP_GroupDataMin implements Serializable { +public class DEP_GroupDataMin implements Serializable, HasId { private int id; private int size; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_Order.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_Order.java index c91789f..9f5a77f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_Order.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_Order.java @@ -2,10 +2,11 @@ package at.ac.tuwien.sbc.valesriegler; import java.util.List; +import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; @Deprecated -public class DEP_Order { +public class DEP_Order implements HasId { private int id; private int tableId; private int groupId; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_Table.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_Table.java index 4de9c66..8ca0ce5 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_Table.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_Table.java @@ -2,12 +2,14 @@ package at.ac.tuwien.sbc.valesriegler; import java.io.Serializable; +import at.ac.tuwien.sbc.valesriegler.common.HasId; + @Deprecated -public class DEP_Table implements Serializable { +public class DEP_Table implements Serializable, HasId { - private int id; + private Integer id; private TableStatus status; - private int groupId; + private Integer groupId; public int getId() { return id; @@ -25,7 +27,7 @@ public class DEP_Table implements Serializable { this.status = status; } - public int getGroupId() { + public Integer getGroupId() { return groupId; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/HasId.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/HasId.java new file mode 100644 index 0000000..7a685db --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/HasId.java @@ -0,0 +1,5 @@ +package at.ac.tuwien.sbc.valesriegler.common; + +public interface HasId { + public int getId(); +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/TableModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/TableModel.java index f03350a..f58b8ba 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/TableModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/TableModel.java @@ -1,8 +1,8 @@ package at.ac.tuwien.sbc.valesriegler.common; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentSkipListMap; import javax.swing.table.AbstractTableModel; @@ -12,13 +12,15 @@ import javax.swing.table.AbstractTableModel; * @author Gregor Riegler * */ -public abstract class TableModel extends AbstractTableModel { +public abstract class TableModel extends AbstractTableModel { protected static final String UNHANDLEDCOLUMN = "Unhandled column"; - protected final List items = Collections.synchronizedList(new ArrayList()); + protected final Map items = new ConcurrentSkipListMap(); public void addItems(List newItems) { - items.addAll(newItems); + for (Item item : newItems) { + items.put(item.getId(), item); + } fireTableDataChanged(); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/SpaceUtil.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java similarity index 88% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/common/SpaceUtil.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java index 64a1439..8f214ec 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/SpaceUtil.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java @@ -10,7 +10,10 @@ import org.mozartspaces.core.MzsConstants.Container; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCoreException; -public abstract class SpaceUtil { +public abstract class Util { + // TODO: solve the switch between mom by command-line arguments + public static final boolean useJMS = true; + public static final String TABLES_CONTAINER = "tables"; public static final String GROUPS_CONTAINER = "groups"; public static final String SERVER_ADDR = "xvsm://localhost:9876"; 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 2642063..64d46c3 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 @@ -3,6 +3,7 @@ package at.ac.tuwien.sbc.valesriegler.group; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; import at.ac.tuwien.sbc.valesriegler.types.GroupData; @@ -11,7 +12,7 @@ import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.Table; // class doing all the Group related work as required. -public class Group implements Runnable { +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! @@ -108,4 +109,9 @@ public class Group implements Runnable { new Thread(this).start(); log.debug("bringOrderedPizzas(): " + this); } + + @Override + public int getId() { + return groupData.getId(); + } } 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 b09cdc9..2502529 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,10 +1,18 @@ package at.ac.tuwien.sbc.valesriegler.group; +import java.io.Serializable; +import java.rmi.server.Operation; import java.util.ArrayList; import java.util.List; +import javax.management.Notification; +import javax.management.NotificationListener; import javax.swing.SwingUtilities; +import org.mozartspaces.core.CapiUtil; +import org.mozartspaces.core.Entry; + +import at.ac.tuwien.sbc.valesriegler.common.Util; 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; @@ -20,6 +28,8 @@ import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; * */ public class GroupAgent { + + public static GroupAgent groupAgent; private GroupOverviewModel groupModel; private AbstractGroupConnector groupconn; @@ -34,12 +44,16 @@ public class GroupAgent { public GroupAgent(JMSGroupConnector groupconn) { groupModel = new GroupOverviewModel(); - this.groupconn = groupconn; - groupconn.init(); - -// xvsm = new XVSMConnector(); -// xvsm.initSpaceCommunication(); -// xvsm.useGroupsContainer(); + if(Util.useJMS) { + this.groupconn = groupconn; + groupconn.init(); + } + else { + xvsm = new XVSMConnector(); + xvsm.initSpaceCommunication(); + xvsm.useGroupsContainer(); + + } } public static GroupAgent getInstance() { @@ -55,10 +69,14 @@ public class GroupAgent { } public void onGroupsCreated(List newGroups) { - List groupData = new ArrayList<>(); - for (Group group : newGroups) { - groupData.add(group.getGroupData()); + if(!Util.useJMS) { + List groupData = new ArrayList<>(); + + for (Group group : newGroups) { + groupData.add(group.getGroupData()); + } + xvsm.createGroups(groupData); } - xvsm.createGroups(groupData); + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java index f2690d0..9393982 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java @@ -20,8 +20,12 @@ import javax.swing.ScrollPaneConstants; import javax.swing.SpinnerNumberModel; import javax.swing.border.TitledBorder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import at.ac.tuwien.sbc.valesriegler.group.Group; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame; import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; @@ -34,6 +38,8 @@ import at.ac.tuwien.sbc.valesriegler.types.PizzaType; */ @SuppressWarnings("serial") public class GroupFrame extends JFrame { + private static final Logger log = LoggerFactory.getLogger(GroupFrame.class); + public GroupFrame(GroupOverviewModel groupModel) { super("Groups"); JPanel wrapper = new JPanel(); @@ -149,7 +155,8 @@ public class GroupFrame extends JFrame { newGroups.add(group); } - GroupAgent.getInstance().getGroupModel().addItems(newGroups); + GroupOverviewModel groupModel = GroupAgent.getInstance().getGroupModel(); + groupModel.addItems(newGroups); GroupAgent.getInstance().onGroupsCreated(newGroups); } 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 4d7e7a8..3ebbfe1 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 @@ -1,5 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.group.gui; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Set; @@ -7,7 +9,9 @@ import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.DEP_Table; import at.ac.tuwien.sbc.valesriegler.common.TableModel; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.Group; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; @@ -23,7 +27,7 @@ public class GroupOverviewModel extends TableModel { public GroupOverviewModel() { super(); - super.items.add(new Group()); // TODO: remove, debuggroup! +// super.items.add(new Group()); // TODO: remove, debuggroup! } @Override @@ -32,20 +36,17 @@ public class GroupOverviewModel extends TableModel { } public List getItems() { - return items; + return new ArrayList<>(items.values()); } public Group getGroupByID(int id) { - for (Group group : items) { - if (group.getGroupData().getId() == id) - return group; - } - return null; + return items.get(id); } @Override public Object getValueAt(int rowIndex, int columnIndex) { - Group group = items.get(rowIndex); + List values = new ArrayList<>(items.values()); + Group group = values.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { case ID: @@ -105,7 +106,7 @@ public class GroupOverviewModel extends TableModel { log.info("addItems()"); super.addItems(newItems); for (Group g : newItems) { - g.goGrabSomeFood(); + if(Util.useJMS) g.goGrabSomeFood(); } } } 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 ffffec0..27de2de 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 @@ -11,6 +11,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.DEP_Table; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.GroupsOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewModel; @@ -27,11 +29,16 @@ import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; * @author Gregor Riegler * */ -public class PizzeriaAgent implements NotificationListener { +public class PizzeriaAgent { private static final Logger log = LoggerFactory.getLogger(PizzeriaAgent.class); private static final String USAGE = "Usage: java PizzeriaAgent XVSM|JMS"; + private static PizzeriaAgent pizzeriaAgent; + + + + private OrdersOverviewModel ordersModel; private GroupsOverviewModel groupModel; private TablesOverviewModel tablesModel; @@ -44,9 +51,9 @@ public class PizzeriaAgent implements NotificationListener { // if (args.length != 1) { // throw new IllegalArgumentException(USAGE); // } else { - String mom = "JMS";// args[0]; + String mom = "XVSM";// args[0]; log.info(mom); - PizzeriaAgent pizzeriaAgent = new PizzeriaAgent(); + pizzeriaAgent = new PizzeriaAgent(); switch (mom) { case "XVSM": pizzeriaAgent.createModels(); @@ -72,6 +79,10 @@ public class PizzeriaAgent implements NotificationListener { private void initXVSM() { xvsm = new XVSMConnector(); xvsm.initSpaceCommunication(); + xvsm.useTablesContainer(); + xvsm.useGroupsContainer(); + + xvsm.initPizzeriaNotifications(); } private void initGUI() { @@ -82,12 +93,14 @@ public class PizzeriaAgent implements NotificationListener { class PizzeriaGUI implements Runnable { @Override public void run() { - PizzeriaFrame frame = new PizzeriaFrame(ordersModel, groupModel, tablesModel, waitersModel, pizzasModel); + PizzeriaFrame frame = new PizzeriaFrame(); frame.setOnTablesCreatedHandler(new TablesCreatedHandler() { @Override public void freeTablesCreated(List tables) { - // xvsm.sendFreeTablesToSpace(tables); + if(!Util.useJMS) { + xvsm.sendTablesToSpace(tables); + } } }); @@ -104,13 +117,33 @@ public class PizzeriaAgent implements NotificationListener { waitersModel = new WaitersOfOrderModel(); pizzasModel = new PizzasOfOrderModel(); } + + public static PizzeriaAgent getInstance() { + return pizzeriaAgent; + } + + public OrdersOverviewModel getOrdersModel() { + return ordersModel; + } + + public GroupsOverviewModel getGroupModel() { + return groupModel; + } + + public TablesOverviewModel getTablesModel() { + return tablesModel; + } + public WaitersOfOrderModel getWaitersModel() { + return waitersModel; + } + + public PizzasOfOrderModel getPizzasModel() { + return pizzasModel; + } + public interface TablesCreatedHandler { public void freeTablesCreated(List tables); } - @Override - public void handleNotification(Notification notification, Object handback) { - // handle space notifications - } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java deleted file mode 100644 index d400184..0000000 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java +++ /dev/null @@ -1,39 +0,0 @@ -package at.ac.tuwien.sbc.valesriegler.pizzeria.gui; - -import java.io.Serializable; - -@Deprecated -public class DEP_Table implements Serializable { - - private Integer id; - private TableStatus status; - private Integer groupId; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public TableStatus getStatus() { - return status; - } - - public void setStatus(TableStatus status) { - this.status = status; - } - - public int getGroupId() { - return groupId; - } - - public void setGroupId(int groupId) { - this.groupId = groupId; - } - - public enum TableStatus { - FREE, OCCUPIED; - } -} 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 f9a7e63..6a2fcba 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 @@ -15,12 +15,16 @@ import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.ScrollPaneConstants; +import javax.swing.SwingUtilities; import javax.swing.border.TitledBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import at.ac.tuwien.sbc.valesriegler.DEP_Order; import at.ac.tuwien.sbc.valesriegler.DEP_Table; import at.ac.tuwien.sbc.valesriegler.common.TableModel; @@ -41,6 +45,8 @@ import at.ac.tuwien.sbc.valesriegler.types.PizzaType; * @author Gregor Riegler */ public class PizzeriaFrame extends JFrame { + private static final Logger log = LoggerFactory.getLogger(PizzeriaFrame.class); + private static final String WRONG_INPUT = "Wrong input"; private static final String THE_NUMBER_MUST_BE_GREATER_OR_EQUAL_TO_1 = "The number must be greater or equal to 1"; private static final String YOUR_INPUT_CANNOT_BE_PARSED_AS_A_NUMBER = "Your input cannot be parsed as a number!"; @@ -49,27 +55,14 @@ public class PizzeriaFrame extends JFrame { private static String PIZZAS_CAPTION = "Pizzas of order %d"; private static String FREE_TABLES = "There are currently %d free table(s)"; - private OrdersOverviewModel ordersModel; - private GroupsOverviewModel groupModel; - private TablesOverviewModel tablesModel; - private WaitersOfOrderModel waitersModel; - private PizzasOfOrderModel pizzasModel; - private TablesCreatedHandler onTablesCreatedHandler; - public PizzeriaFrame(OrdersOverviewModel ordersModel, GroupsOverviewModel groupModel, - TablesOverviewModel tablesModel, WaitersOfOrderModel waitersModel, PizzasOfOrderModel pizzasModel) { + public PizzeriaFrame() { super("Pizzeria"); - - this.ordersModel = ordersModel; - this.groupModel = groupModel; - this.tablesModel = tablesModel; - this.waitersModel = waitersModel; - this.pizzasModel = pizzasModel; } public void start() { - initModels(); + JPanel wrapper = new JPanel(); GridLayout wrapperLayout = new GridLayout(2, 2); @@ -89,6 +82,8 @@ public class PizzeriaFrame extends JFrame { setContentPane(wrapper); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + initModels(); } private int getNumberOfTables() { @@ -111,13 +106,13 @@ public class PizzeriaFrame extends JFrame { return numberOfTables; } - private void initModels() { + public void initModels() { List orders = getOrdersTestData(); - ordersModel.setItems(orders); + PizzeriaAgent.getInstance().getOrdersModel().setItems(orders); // When the tables get created in the first place, the handler is informed int numberOfTables = getNumberOfTables(); - List freeTablesCreated = tablesModel.createFreeTables(numberOfTables); + List freeTablesCreated = PizzeriaAgent.getInstance().getTablesModel().createFreeTables(numberOfTables); onTablesCreatedHandler.freeTablesCreated(freeTablesCreated); } @@ -129,8 +124,8 @@ public class PizzeriaFrame extends JFrame { final JPanel waitersPanel = new JPanel(); final JPanel pizzasPanel = new JPanel(); - createTableInTitledPanel(waitersPanel, waitersModel, ""); - createTableInTitledPanel(pizzasPanel, pizzasModel, ""); + createTableInTitledPanel(waitersPanel, PizzeriaAgent.getInstance().getWaitersModel(), ""); + createTableInTitledPanel(pizzasPanel, PizzeriaAgent.getInstance().getPizzasModel(), ""); /** * Update the displayed order in the waiter and pizza detail tables when an @@ -142,11 +137,11 @@ public class PizzeriaFrame extends JFrame { ListSelectionModel lsm = (ListSelectionModel) e.getSource(); int minIndex = lsm.getMinSelectionIndex(); - DEP_Order currentOrder = ordersModel.getOrderOfRow(minIndex); + DEP_Order currentOrder = PizzeriaAgent.getInstance().getOrdersModel().getOrderOfRow(minIndex); waitersPanel.setBorder(new TitledBorder(String.format(WAITER_IDS_CAPTION, currentOrder.getId()))); pizzasPanel.setBorder(new TitledBorder(String.format(PIZZAS_CAPTION, currentOrder.getId()))); - waitersModel.setCurrentOrder(currentOrder); - pizzasModel.setCurrentOrder(currentOrder); + PizzeriaAgent.getInstance().getWaitersModel().setCurrentOrder(currentOrder); + PizzeriaAgent.getInstance().getPizzasModel().setCurrentOrder(currentOrder); waitersPanel.setVisible(true); pizzasPanel.setVisible(true); @@ -164,7 +159,7 @@ public class PizzeriaFrame extends JFrame { private JTable initOrdersOverview(JPanel wrapper) { JPanel tablePanel = new JPanel(); - JTable orderTable = createTableInTitledPanel(tablePanel, ordersModel, "Orders"); + JTable orderTable = createTableInTitledPanel(tablePanel, PizzeriaAgent.getInstance().getOrdersModel(), "Orders"); wrapper.add(tablePanel); @@ -173,7 +168,7 @@ public class PizzeriaFrame extends JFrame { private void initWaitingGroupsOverview(JPanel wrapper) { JPanel tablePanel = new JPanel(); - createTableInTitledPanel(tablePanel, groupModel, "Groups"); + createTableInTitledPanel(tablePanel, PizzeriaAgent.getInstance().getGroupModel(), "Groups"); wrapper.add(tablePanel); } @@ -182,14 +177,21 @@ public class PizzeriaFrame extends JFrame { JPanel scrollPanePanel = new JPanel(new GridBagLayout()); final JLabel freeTablesLabel = new JLabel(getNumberOfFreeTables()); scrollPanePanel.add(freeTablesLabel, new GridBagConstraints()); - createTableInTitledPanel(scrollPanePanel, tablesModel, "Tables"); + createTableInTitledPanel(scrollPanePanel, PizzeriaAgent.getInstance().getTablesModel(), "Tables"); - tablesModel.addTableModelListener(new TableModelListener() { - @Override - public void tableChanged(TableModelEvent e) { - freeTablesLabel.setText(getNumberOfFreeTables()); - } - }); + PizzeriaAgent.getInstance().getTablesModel() + .addTableModelListener(new TableModelListener() { + @Override + public void tableChanged(TableModelEvent e) { + String numberOfFreeTables = getNumberOfFreeTables(); + log.info("Number of free tables changed: {}", + numberOfFreeTables); + + freeTablesLabel.setText(numberOfFreeTables); + + } + }); + wrapper.add(scrollPanePanel); } @@ -244,7 +246,7 @@ public class PizzeriaFrame extends JFrame { } private String getNumberOfFreeTables() { - return String.format(FREE_TABLES, tablesModel.getNumberOfFreeTables()); + return String.format(FREE_TABLES, PizzeriaAgent.getInstance().getTablesModel().getNumberOfFreeTables()); } public void setOnTablesCreatedHandler(PizzeriaAgent.TablesCreatedHandler tablesCreatedHandler) { 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 498e820..f226fe8 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 @@ -1,6 +1,10 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; +import java.util.ArrayList; +import java.util.List; + import at.ac.tuwien.sbc.valesriegler.DEP_GroupDataMin; +import at.ac.tuwien.sbc.valesriegler.DEP_Order; import at.ac.tuwien.sbc.valesriegler.common.TableModel; public class GroupsOverviewModel extends TableModel { @@ -18,7 +22,8 @@ public class GroupsOverviewModel extends TableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { - DEP_GroupDataMin group = items.get(rowIndex); + List values = new ArrayList<>(items.values()); + DEP_GroupDataMin group = values.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch(wantedColumn) { case ID : return group.getId(); 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 b16b2c3..a55128e 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,6 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import at.ac.tuwien.sbc.valesriegler.DEP_Order; @@ -18,7 +19,8 @@ public class OrdersOverviewModel extends TableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { - DEP_Order order = items.get(rowIndex); + List values = new ArrayList<>(items.values()); + DEP_Order order = values.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch(wantedColumn) { case ID : return order.getId(); @@ -30,7 +32,7 @@ public class OrdersOverviewModel extends TableModel { } public DEP_Order getOrderOfRow(int rowIndex) { - return items.get(rowIndex); + return new ArrayList<>(items.values()).get(rowIndex); } @Override diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java index 498eece..16102ed 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java @@ -3,6 +3,7 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; import java.util.ArrayList; import java.util.List; +import at.ac.tuwien.sbc.valesriegler.DEP_GroupDataMin; import at.ac.tuwien.sbc.valesriegler.DEP_Table; import at.ac.tuwien.sbc.valesriegler.DEP_Table.TableStatus; import at.ac.tuwien.sbc.valesriegler.common.TableModel; @@ -23,7 +24,8 @@ public class TablesOverviewModel extends TableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { - DEP_Table table = items.get(rowIndex); + List values = new ArrayList<>(items.values()); + DEP_Table table = values.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch(wantedColumn) { case TABLE_ID : return table.getId(); @@ -41,7 +43,7 @@ public class TablesOverviewModel extends TableModel { List ids = new ArrayList<>(); synchronized(items) { - for(DEP_Table table : items) { + for(DEP_Table table : items.values()) { if(table.getStatus() == TableStatus.FREE) { ids.add(table.getId()); } @@ -58,7 +60,6 @@ public class TablesOverviewModel extends TableModel { DEP_Table table = createFreeTable(); tables.add(table); } - addItems(tables); return tables; } @@ -72,4 +73,11 @@ public class TablesOverviewModel extends TableModel { return table; } + public void mergeTables(List tables) { + for (DEP_Table table : tables) { + items.put(table.getId(), table); + } + fireTableDataChanged(); + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java index 69bb664..e90fec7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java @@ -4,6 +4,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import org.mozartspaces.capi3.Queryable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,6 +15,7 @@ import org.slf4j.LoggerFactory; * @author jan * */ +@Queryable(autoindex = true) public class GroupData implements Serializable { private static final Logger log = LoggerFactory.getLogger(GroupData.class); 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 3685bb6..2c5bcaa 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 @@ -3,8 +3,11 @@ package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; import java.util.List; +import org.mozartspaces.capi3.Queryable; + import at.ac.tuwien.sbc.valesriegler.group.Group; +@Queryable(autoindex = true) public class Order implements Serializable { private static int idNext = 0; private final int id; 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 e5a102c..d1b9fba 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 @@ -2,7 +2,9 @@ package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; -public class PizzaOrder implements Serializable { +import at.ac.tuwien.sbc.valesriegler.common.HasId; + +public class PizzaOrder implements Serializable, HasId { private static int nextID = 0; final private int id; 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 0b48bcc..5c890b2 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 @@ -2,6 +2,9 @@ package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; +import org.mozartspaces.capi3.Queryable; + +@Queryable(autoindex = true) public class Table implements Serializable { private final int id; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java index e4d14c4..c681dbe 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java @@ -1,10 +1,13 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; +import java.io.Serializable; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import javax.swing.SwingUtilities; + import org.mozartspaces.capi3.AnyCoordinator; import org.mozartspaces.capi3.Coordinator; import org.mozartspaces.capi3.FifoCoordinator; @@ -18,11 +21,16 @@ import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCore; import org.mozartspaces.core.MzsCoreException; import org.mozartspaces.core.TransactionReference; +import org.mozartspaces.notifications.Notification; +import org.mozartspaces.notifications.NotificationListener; +import org.mozartspaces.notifications.NotificationManager; +import org.mozartspaces.notifications.Operation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.DEP_Table; -import at.ac.tuwien.sbc.valesriegler.common.SpaceUtil; +import at.ac.tuwien.sbc.valesriegler.common.Util; +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.Order; @@ -42,12 +50,14 @@ public class XVSMConnector { private ContainerReference groupsContainer; // private ContainerReference notificationContainer; private Capi capi; + private NotificationManager notificationMgr; public void initSpaceCommunication() { try { MzsCore core = DefaultMzsCore.newInstanceWithoutSpace(); capi = new Capi(core); + notificationMgr = new NotificationManager(core); } catch (Exception e) { log.error("Space connection could not be established! Have you started the Space Server?"); handleSpaceErrorAndTerminate(e); @@ -56,7 +66,7 @@ public class XVSMConnector { public void useTablesContainer() { try { - tablesContainer = SpaceUtil.getOrCreateNamedContainer(SpaceUtil.SERVER_ADDR, SpaceUtil.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator())); + tablesContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator())); } catch (MzsCoreException e) { handleSpaceErrorAndTerminate(e); } @@ -64,7 +74,7 @@ public class XVSMConnector { public void useGroupsContainer() { try { - groupsContainer = SpaceUtil.getOrCreateNamedContainer(SpaceUtil.SERVER_ADDR, SpaceUtil.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator())); + groupsContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false))); } catch (MzsCoreException e) { handleSpaceErrorAndTerminate(e); } @@ -93,18 +103,6 @@ public class XVSMConnector { } } - public List readTables() { - ArrayList tables = new ArrayList<>(); - try { - tables = capi.take(tablesContainer); - } catch (MzsCoreException e) { - log.error(e.getMessage()); - e.printStackTrace(); - System.exit(1); - } - return tables; - } - public void createGroups(List newGroups) { try { List entries = new ArrayList<>(); @@ -116,12 +114,13 @@ public class XVSMConnector { log.info("Wrote new Groups to Space!"); } catch (MzsCoreException e) { log.info(e.getMessage()); + e.printStackTrace(); } } public void handleWaitingGroup(int id) { try { - TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(SpaceUtil.SERVER_ADDR)); + TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR)); DEP_Table table = null; try { ArrayList tables = capi.take(tablesContainer, FifoCoordinator.newSelector(), RequestTimeout.ZERO, tx); @@ -174,7 +173,7 @@ public class XVSMConnector { public void handleOrderRequest(int id) { try { - TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(SpaceUtil.SERVER_ADDR)); + TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR)); GroupData groupData = new GroupData(null); groupData.setState(GroupState.SITTING); @@ -208,13 +207,64 @@ public class XVSMConnector { } } + public void initPizzeriaNotifications() { + // When tables are written to the space the Pizzeria gets informed and sets the new tables in the tables model! + NotificationListener tablesListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + + final List tables = new ArrayList<>(); + List entryList = (List) entries; + for (Entry entry : entryList) { + tables.add((DEP_Table) entry.getValue()); + } + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + PizzeriaAgent.getInstance().getTablesModel().mergeTables(tables); + } + }); + } + }; + + NotificationListener groupsListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + List tables = (List) entries; +// problem at the moment: Item type of groupoverviewmodel is Group and not GroupData + } + }; + + try { + notificationMgr.createNotification(tablesContainer, groupsListener, Operation.WRITE); + notificationMgr.createNotification(tablesContainer, tablesListener, Operation.WRITE); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void initGroupNotifications() { + NotificationListener notifListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + List groups = (List) entries; + // when a groupdata is changed, call setItems on the groupoverviewmodel. problem at the moment: Item type of groupoverviewmodel is Group and not GroupData + } - public void handlePizzaDistribution() { + }; + try { + notificationMgr.createNotification(groupsContainer, notifListener, Operation.WRITE); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); + } } public void handlePaymentRequest() { } + public void handlePizzaDistribution() { + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java index 05f07b2..59f7308 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java @@ -1,13 +1,9 @@ package at.ac.tuwien.sbc.valesriegler.xvsm.cook; -import java.util.List; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Table; import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; -import at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter; public class Cook { private static final String USAGE = "Cook needs exactly one parameter: ID of type Integer"; -- 2.43.0