</exclusion>
</exclusions>
</dependency>
+ <dependency>
+ <groupId>org.mozartspaces</groupId>
+ <artifactId>mozartspaces-notifications</artifactId>
+ <version>2.2-SNAPSHOT</version>
+ </dependency>
</dependencies>
<build>
<plugins>
- <!-- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>exec-maven-plugin</artifactId>
- <version>1.2.1</version>
- </plugin> -->
+ <!-- <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId>
+ <version>1.2.1</version> </plugin> -->
</plugins>
<pluginManagement>
<plugins>
</plugins>
</build>
</profile>
-
+
<profile>
<id>pizzeria</id>
<build>
<argument>-classpath</argument>
<classpath />
<argument>at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent</argument>
- <!-- <argument>xvsm://localhost:4242</argument> -->
+ <!-- <argument>xvsm://localhost:4242</argument> -->
</arguments>
</configuration>
</plugin>
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 <gregor DOT riegler AT gmail DOT com>
*/
@Deprecated
-public class DEP_GroupDataMin implements Serializable {
+public class DEP_GroupDataMin implements Serializable, HasId {
private int id;
private int size;
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;
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;
this.status = status;
}
- public int getGroupId() {
+ public Integer getGroupId() {
return groupId;
}
--- /dev/null
+package at.ac.tuwien.sbc.valesriegler.common;
+
+public interface HasId {
+ public int getId();
+}
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;
* @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
*
*/
-public abstract class TableModel<Item> extends AbstractTableModel {
+public abstract class TableModel<Item extends HasId> extends AbstractTableModel {
protected static final String UNHANDLEDCOLUMN = "Unhandled column";
- protected final List<Item> items = Collections.synchronizedList(new ArrayList<Item>());
+ protected final Map<Integer, Item> items = new ConcurrentSkipListMap<Integer, Item>();
public void addItems(List<Item> newItems) {
- items.addAll(newItems);
+ for (Item item : newItems) {
+ items.put(item.getId(), item);
+ }
fireTableDataChanged();
}
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";
public static ContainerReference getOrCreateNamedContainer(final String spaceUri, final String containerName, final Capi capi, final List<Coordinator> coordinators)
throws MzsCoreException {
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.PayRequest;
import at.ac.tuwien.sbc.valesriegler.group.actions.TableFree;
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!
log.debug("bringOrderedPizzas(): " + this);
}
+ @Override
+ public int getId() {
+ return groupData.getId();
+ }
+
// pay for the pizzas and leave
public void payForPizzas(int waiter) {
groupData.setState(GroupState.PAY);
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;
+import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector;
/**
* The Main class of the Group component.
*
*/
public class GroupAgent {
+
+
public static GroupAgent groupAgent;
private GroupOverviewModel groupModel;
private AbstractGroupConnector groupconn;
+ private XVSMConnector xvsm;
public static void main(String[] args) {
// TODO: use jms and xvsm manager here.
groupAgent = new GroupAgent(new JMSGroupConnector());
+
SwingUtilities.invokeLater(new GroupGUI());
}
public GroupAgent(JMSGroupConnector groupconn) {
groupModel = new GroupOverviewModel();
- this.groupconn = groupconn;
- groupconn.init();
+ if(Util.useJMS) {
+ this.groupconn = groupconn;
+ groupconn.init();
+ }
+ else {
+ xvsm = new XVSMConnector();
+ xvsm.initSpaceCommunication();
+ xvsm.useGroupsContainer();
+
+ }
}
public static GroupAgent getInstance() {
public GroupOverviewModel getGroupModel() {
return groupModel;
}
+
+ public void onGroupsCreated(List<Group> newGroups) {
+ if(!Util.useJMS) {
+ List<GroupData> groupData = new ArrayList<>();
+
+ for (Group group : newGroups) {
+ groupData.add(group.getGroupData());
+ }
+ xvsm.createGroups(groupData);
+ }
+
+ }
}
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;
*/
@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();
newGroups.add(group);
}
- GroupAgent.getInstance().getGroupModel().addItems(newGroups);
+ GroupOverviewModel groupModel = GroupAgent.getInstance().getGroupModel();
+ groupModel.addItems(newGroups);
+ GroupAgent.getInstance().onGroupsCreated(newGroups);
}
public void handleCancel() {
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;
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;
public GroupOverviewModel() {
super();
- super.items.add(new Group()); // TODO: remove, debuggroup!
+// super.items.add(new Group()); // TODO: remove, debuggroup!
}
@Override
}
public List<Group> 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<Group> values = new ArrayList<>(items.values());
+ Group group = values.get(rowIndex);
String wantedColumn = COLUMNS[columnIndex];
switch (wantedColumn) {
case ID:
log.info("addItems()");
super.addItems(newItems);
for (Group g : newItems) {
- g.goGrabSomeFood();
+ if(Util.useJMS) g.goGrabSomeFood();
}
}
}
import java.util.List;
+import javax.management.Notification;
+import javax.management.NotificationListener;
import javax.swing.SwingUtilities;
import org.mozartspaces.core.MzsCoreException;
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;
private static final String USAGE = "Usage: java PizzeriaAgent XVSM|JMS";
+ private static PizzeriaAgent pizzeriaAgent;
+
+
+
+
private OrdersOverviewModel ordersModel;
private GroupsOverviewModel groupModel;
private TablesOverviewModel tablesModel;
// if (args.length != 1) {
// throw new IllegalArgumentException(USAGE);
// } else {
- String mom = "JMS";// args[0];
+// String mom = "XVSM";// args[0];
+ String mom = Util.useJMS ? "JMS" : "XVSM";
log.info(mom);
- PizzeriaAgent pizzeriaAgent = new PizzeriaAgent();
+ pizzeriaAgent = new PizzeriaAgent();
switch (mom) {
case "XVSM":
pizzeriaAgent.createModels();
}
private void initXVSM() {
- try {
xvsm = new XVSMConnector();
xvsm.initSpaceCommunication();
-
- } catch (MzsCoreException e) {
- log.error(e.getMessage());
- log.error("The Pizzeria has no Space connection! Have you started the Space Server?");
- System.exit(1);
- }
+ xvsm.useTablesContainer();
+ xvsm.useGroupsContainer();
+
+ xvsm.initPizzeriaNotifications();
}
private void initGUI() {
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<DEP_Table> tables) {
- // xvsm.sendFreeTablesToSpace(tables);
+ if(!Util.useJMS) {
+ xvsm.sendTablesToSpace(tables);
+ }
}
});
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<DEP_Table> tables);
}
+
}
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;
* @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
*/
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!";
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);
setContentPane(wrapper);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ initModels();
}
private int getNumberOfTables() {
return numberOfTables;
}
- private void initModels() {
+ public void initModels() {
List<DEP_Order> 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<DEP_Table> freeTablesCreated = tablesModel.createFreeTables(numberOfTables);
+ List<DEP_Table> freeTablesCreated = PizzeriaAgent.getInstance().getTablesModel().createFreeTables(numberOfTables);
onTablesCreatedHandler.freeTablesCreated(freeTablesCreated);
}
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
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);
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);
private void initWaitingGroupsOverview(JPanel wrapper) {
JPanel tablePanel = new JPanel();
- createTableInTitledPanel(tablePanel, groupModel, "Groups");
+ createTableInTitledPanel(tablePanel, PizzeriaAgent.getInstance().getGroupModel(), "Groups");
wrapper.add(tablePanel);
}
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);
}
}
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) {
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<DEP_GroupDataMin> {
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
- DEP_GroupDataMin group = items.get(rowIndex);
+ List<DEP_GroupDataMin> values = new ArrayList<>(items.values());
+ DEP_GroupDataMin group = values.get(rowIndex);
String wantedColumn = COLUMNS[columnIndex];
switch(wantedColumn) {
case ID : return group.getId();
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;
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
- DEP_Order order = items.get(rowIndex);
+ List<DEP_Order> values = new ArrayList<>(items.values());
+ DEP_Order order = values.get(rowIndex);
String wantedColumn = COLUMNS[columnIndex];
switch(wantedColumn) {
case ID : return order.getId();
}
public DEP_Order getOrderOfRow(int rowIndex) {
- return items.get(rowIndex);
+ return new ArrayList<>(items.values()).get(rowIndex);
}
@Override
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;
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
- DEP_Table table = items.get(rowIndex);
+ List<DEP_Table> values = new ArrayList<>(items.values());
+ DEP_Table table = values.get(rowIndex);
String wantedColumn = COLUMNS[columnIndex];
switch(wantedColumn) {
case TABLE_ID : return table.getId();
List<Integer> 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());
}
DEP_Table table = createFreeTable();
tables.add(table);
}
- addItems(tables);
return tables;
}
return table;
}
+ public void mergeTables(List<DEP_Table> tables) {
+ for (DEP_Table table : tables) {
+ items.put(table.getId(), table);
+ }
+ fireTableDataChanged();
+ }
+
}
import java.util.ArrayList;
import java.util.List;
+import org.mozartspaces.capi3.Queryable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* @author jan
*
*/
+@Queryable(autoindex = true)
public class GroupData implements Serializable {
private static final Logger log = LoggerFactory.getLogger(GroupData.class);
- final private int id;
+ final private Integer id;
private GroupState state = GroupState.NEW;
- private int size;
+ private Integer size;
private Table table;
-
- private int tableWaiter;
+ private Integer tableWaiter;
private Order order;
- private int orderWaiter;
- private int servingWaiter;
- private int payingWaiter;
+ private Integer orderWaiter;
+ private Integer servingWaiter;
+ private Integer payingWaiter;
@Deprecated
private List<PizzaType> pizzas = new ArrayList<PizzaType>();
- public GroupData(int id) {
+ public GroupData(Integer id) {
this.id = id;
}
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;
@Override
public String toString() {
- return "Pizza [id=" + id + ", type=" + type + ", cookId=" + cookId + ", deliveryAgent=" + deliveryAgent + "]";
+ return "Pizza [type=" + type + ", id=" + id + ", cookId=" + cookId + ", deliveryAgent=" + deliveryAgent + "]";
}
}
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;
import java.io.Serializable;
+import org.mozartspaces.capi3.Queryable;
+
+@Queryable(autoindex = true)
public class Table implements Serializable {
private final int id;
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;
+import org.mozartspaces.capi3.LindaCoordinator;
+import org.mozartspaces.capi3.LindaCoordinator.LindaSelector;
import org.mozartspaces.core.Capi;
import org.mozartspaces.core.ContainerReference;
import org.mozartspaces.core.DefaultMzsCore;
import org.mozartspaces.core.Entry;
+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;
+import at.ac.tuwien.sbc.valesriegler.types.Table;
+
/**
* Responsible for XVSM Communication
private static final Logger log = LoggerFactory.getLogger(XVSMConnector.class);
private ContainerReference tablesContainer;
+ private ContainerReference groupsContainer;
+// private ContainerReference notificationContainer;
private Capi capi;
+ private NotificationManager notificationMgr;
- public void initSpaceCommunication() throws MzsCoreException {
- MzsCore core = DefaultMzsCore.newInstanceWithoutSpace();
- capi = new Capi(core);
- tablesContainer = SpaceUtil.getOrCreateNamedContainer(SpaceUtil.SERVER_ADDR, SpaceUtil.TABLES_CONTAINER, capi, Arrays.asList((Coordinator) new AnyCoordinator()));
+ 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);
+ }
+ }
+
+ public void useTablesContainer() {
+ try {
+ tablesContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator()));
+ } catch (MzsCoreException e) {
+ handleSpaceErrorAndTerminate(e);
+ }
+ }
+
+ public void useGroupsContainer() {
+ try {
+ groupsContainer = Util.getOrCreateNamedContainer(Util.SERVER_ADDR, Util.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator(false)));
+ } catch (MzsCoreException e) {
+ handleSpaceErrorAndTerminate(e);
+ }
+ }
+
+ private List<Coordinator> createCoordinators(Coordinator... coordinator) {
+ return Arrays.asList(coordinator);
+ }
+
+ private void handleSpaceErrorAndTerminate(Exception e) {
+ log.error(e.getMessage());
+ System.exit(1);
}
- public void sendFreeTablesToSpace(List<DEP_Table> tables) {
+ public void sendTablesToSpace(List<DEP_Table> tables) {
try {
List<Entry> entries = new ArrayList<>();
for (DEP_Table table : tables) {
}
capi.write(entries, tablesContainer);
- log.info("Wrote Free tables to Space!");
+ log.info("Wrote tables to Space!");
} catch (MzsCoreException e) {
+ log.info(e.getMessage());
+ }
+ }
+
+ public void createGroups(List<GroupData> newGroups) {
+ try {
+ List<Entry> entries = new ArrayList<>();
+ for (GroupData group : newGroups) {
+ entries.add(new Entry(group));
+ }
+ capi.write(entries, groupsContainer);
+
+ log.info("Wrote new Groups to Space!");
+ } catch (MzsCoreException e) {
+ log.info(e.getMessage());
e.printStackTrace();
}
}
- public List<DEP_Table> readTables() {
- ArrayList<DEP_Table> tables = new ArrayList<>();
+ public void handleWaitingGroup(int id) {
try {
- tables = capi.take(tablesContainer);
+ TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
+ DEP_Table table = null;
+ try {
+ ArrayList<DEP_Table> tables = capi.take(tablesContainer, FifoCoordinator.newSelector(), RequestTimeout.ZERO, tx);
+ table = tables.get(0);
+ } catch (Exception e) {
+ log.info("There is no free table");
+ capi.rollbackTransaction(tx);
+ return;
+ }
+
+ GroupData groupData = new GroupData(null);
+ groupData.setState(GroupState.WAITING);
+ LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
+ ArrayList<GroupData> waitingGroups = null;
+
+ try {
+ waitingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
+ } catch (Exception e) {
+ log.info("There is no waiting group");
+ capi.rollbackTransaction(tx);
+ return;
+ }
+
+ GroupData group = waitingGroups.get(0);
+ group.setTable(new Table(table.getGroupId()));
+ group.setTableWaiter(id);
+ group.setState(GroupState.SITTING);
+
+ try {
+ capi.write(groupsContainer, RequestTimeout.TRY_ONCE, tx, new Entry(group));
+ } catch (Exception e) {
+ e.printStackTrace();
+ capi.rollbackTransaction(tx);
+ return;
+ }
+
+ try {
+ capi.write(new Entry(table), tablesContainer, RequestTimeout.TRY_ONCE, tx);
+ } catch (Exception e) {
+ e.printStackTrace();
+ capi.rollbackTransaction(tx);
+ return;
+ }
+
+ capi.commitTransaction(tx);
} catch (MzsCoreException e) {
- log.error(e.getMessage());
e.printStackTrace();
- System.exit(1);
}
- return tables;
}
+ public void handleOrderRequest(int id) {
+ try {
+ TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(Util.SERVER_ADDR));
+
+ GroupData groupData = new GroupData(null);
+ groupData.setState(GroupState.SITTING);
+ LindaSelector sel = LindaCoordinator.newSelector(groupData, 1);
+ ArrayList<GroupData> sittingGroups = null;
+
+ try {
+ sittingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx);
+ } catch (Exception e) {
+ log.info("There is no sitting group");
+ capi.rollbackTransaction(tx);
+ return;
+ }
+ GroupData sittingGroup = sittingGroups.get(0);
+ Order order = sittingGroup.getOrder();
+ sittingGroup.setOrderWaiter(id);
+ sittingGroup.setState(GroupState.ORDERED);
+
+ try {
+ capi.write(new Entry(sittingGroup), groupsContainer, RequestTimeout.TRY_ONCE, tx);
+ } catch (Exception e) {
+ e.printStackTrace();
+ capi.rollbackTransaction(tx);
+ return;
+ }
+
+ capi.commitTransaction(tx);
+
+ } catch (MzsCoreException e) {
+ e.printStackTrace();
+ }
+ }
+
+ 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<? extends Serializable> entries) {
+
+ final List<DEP_Table> tables = new ArrayList<>();
+ List<Entry> entryList = (List<Entry>) 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<? extends Serializable> entries) {
+ List<GroupData> tables = (List<GroupData>) entries;
+// TODO 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<? extends Serializable> entries) {
+ List<GroupData> groups = (List<GroupData>) entries;
+
+ // when a groupdata is changed, call setItems on the groupoverviewmodel. TODO problem at the moment: Item type of groupoverviewmodel is Group and not GroupData
+ }
+
+ };
+ try {
+ notificationMgr.createNotification(groupsContainer, notifListener, Operation.WRITE);
+ } catch (Exception e) {
+ handleSpaceErrorAndTerminate(e);
+ }
+ }
+
+ public void handlePaymentRequest() {
+ }
+
+ public void handlePizzaDistribution() {
+ }
+
+
}
--- /dev/null
+package at.ac.tuwien.sbc.valesriegler.xvsm.cook;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector;
+
+public class Cook {
+ private static final String USAGE = "Cook needs exactly one parameter: ID of type Integer";
+ private static final Logger log = LoggerFactory.getLogger(Cook.class);
+
+ private int id;
+ private XVSMConnector xvsm;
+
+ public static void main(String[] args) {
+ if(args.length != 1) {
+ throw new IllegalArgumentException(USAGE);
+ }
+
+ int parsedId = 0;
+ try {
+ parsedId = Integer.parseInt(args[0]);
+ } catch (NumberFormatException e) {
+ log.error(USAGE);
+ return;
+ }
+
+ Cook cook = new Cook(parsedId);
+ cook.start();
+ }
+
+ private void start() {
+ initSpaceCommunication();
+
+
+
+ }
+
+ private void initSpaceCommunication() {
+ xvsm = new XVSMConnector();
+ xvsm.initSpaceCommunication();
+
+ log.info("Space Connection established!");
+
+
+ }
+
+ public Cook(int id) {
+ this.id = id;
+ log.info("I AM A Cook WITH ID {}", id);
+ }
+
+}
private void start() {
initSpaceCommunication();
- List<DEP_Table> tables = xvsm.readTables();
-
- log.info("Number of free tables received: {}", tables.size());
-
+ while(true) {
+ xvsm.handleWaitingGroup(id);
+
+ xvsm.handleOrderRequest(id);
+
+ xvsm.handlePaymentRequest();
+
+ xvsm.handlePizzaDistribution();
+ }
}
+
private void initSpaceCommunication() {
- try {
- xvsm = new XVSMConnector();
- xvsm.initSpaceCommunication();
- } catch (MzsCoreException e) {
- log.error(e.getMessage());
- log.error("The Waiter has no Space connection! Have you started the Space Server?");
- System.exit(1);
- }
-
+ xvsm = new XVSMConnector();
+ xvsm.initSpaceCommunication();
+ xvsm.useTablesContainer();
+ xvsm.useGroupsContainer();
+
log.info("Space Connection established!");